1
0

interceptClickEvent.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //https://stackoverflow.com/a/33616981/7200161
  2. function interceptClickEvent(e) {
  3. var href;
  4. var target = e.target || e.srcElement;
  5. if (target.tagName === 'A') {
  6. href = target.getAttribute('href');
  7. }
  8. else{
  9. target = target.closest('a');
  10. if(target)
  11. href = target.getAttribute('href');
  12. }
  13. if(href){
  14. href = href.trim();
  15. const execPrefix = "exec:";
  16. if (href.startsWith(execPrefix)) {
  17. const qspcode = href.substring(execPrefix.length);
  18. const twineCode = setup.qsrc2tw(qspcode);
  19. console.log("EXEC:",twineCode);
  20. try{
  21. $(".passage").wiki(twineCode);
  22. }
  23. finally{
  24. e.preventDefault();
  25. }
  26. }
  27. }
  28. }
  29. //listen for link click events at the document level
  30. if (document.addEventListener) {
  31. document.addEventListener('click', interceptClickEvent);
  32. } else if (document.attachEvent) {
  33. document.attachEvent('onclick', interceptClickEvent);
  34. }