Advertisement
Guest User

Smart Middle Click

a guest
Jul 5th, 2011
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. gBrowser = document.getElementById("content");
  2. gBrowser.addEventListener("click", SMConClick, true);
  3.  
  4. function fixupAURL(aURL) {
  5.     var str = '^(https?\:|ftp\:|news\:|mailto\:|smb\:' +
  6.              'gopher\:|about\:|file\:|cache\:|javascript\:|' +
  7.              'x\-jsd\:|jar\:|resource\:|data\:)'
  8.     var re = new RegExp(str, 'i');
  9.     if (re.test(aURL)) {
  10.         return aURL;
  11.     }
  12.     else {
  13.         var urifix  = Components.classes['@mozilla.org/docshell/urifixup;1'].getService(Components.interfaces.nsIURIFixup);
  14.         var loc = urifix.createFixupURI(getBrowser().currentURI.spec, 0);
  15.         var host = loc.host, path = loc.path, proto = loc.scheme;
  16.         if (aURL[0] == '/') {
  17.             path = '';
  18.         }
  19.         else {
  20.             path = path.replace(/(.+?\/?)[^\/]+?$/, "$1");
  21.             while (/\.\.\//i.test(aURL)) {
  22.                 aURL = aURL.replace(/\.\.\//i, '');
  23.                 path = path.replace(/(.+?\/?)[^\/]+?\/$|([^\/]+?\/?)$/, "$1");
  24.             }
  25.             if (path[0] != '/') {
  26.                 path = '/' + path;
  27.             }
  28.         }
  29.         return proto + '://' + host + path + aURL;
  30.     }
  31. }
  32.  
  33. function SMConClick(aEvent) {
  34.     if ( (aEvent.button == 1 || (aEvent.button == 0 && (aEvent.ctrlKey || aEvent.shiftKey)))
  35.                 && contentAreaClick2(aEvent) )
  36.         aEvent.stopPropagation();
  37. }
  38.  
  39. function contentAreaClick2(event)
  40. {
  41.     var target = event.target;
  42.     var linkNode;
  43.  
  44.     if (target instanceof HTMLAnchorElement ||
  45.             target instanceof HTMLAreaElement ||
  46.             target instanceof HTMLLinkElement)
  47.     {
  48.         if (target.hasAttribute("href"))
  49.             linkNode = target;
  50.     }
  51.     else {
  52.         linkNode = event.originalTarget;
  53.         while (linkNode && !(linkNode instanceof HTMLAnchorElement))
  54.             linkNode = linkNode.parentNode;
  55.         // <a> cannot be nested.  So if we find an anchor without an
  56.         // href, there is no useful <a> around the target
  57.         if (linkNode && !linkNode.hasAttribute("href"))
  58.             linkNode = null;
  59.     }
  60.     var wrapper = null;
  61.  
  62.     if (linkNode) {
  63.         wrapper = linkNode;
  64.  
  65.         var is_js = wrapper.href.indexOf('javascript:');
  66.         var pound = wrapper.href.charAt(wrapper.href.length-1);
  67.  
  68.         // Check if URL has 'javascript:' in it
  69.         if (is_js != -1) {
  70.             var jsParts = wrapper.href.split("'");
  71.             rawHref = jsParts[1];
  72.  
  73.             if (rawHref) {
  74.                 newHref = unescape(fixupAURL(rawHref));
  75.                 handleLinkClick(event, newHref, linkNode);
  76.             }
  77.             // If extraction fails, try the onclick attribute
  78.             else if (wrapper.getAttribute("onclick")) {
  79.                 var ocParts = wrapper.getAttribute("onclick").split("'");
  80.                 rawHref = ocParts[1];
  81.  
  82.                 if (rawHref) {
  83.                     newHref = unescape(fixupAURL(rawHref));
  84.                     handleLinkClick(event, newHref, linkNode);
  85.                 }
  86.             }
  87.  
  88.             return true;
  89.         }
  90.         // Check if URL ends in '#'
  91.         else if (pound == '#') {
  92.             var ocParts = wrapper.getAttribute("onclick").split("'");
  93.             rawHref = ocParts[1];
  94.  
  95.             if (rawHref) {
  96.                 newHref = unescape(fixupAURL(rawHref));
  97.                 handleLinkClick(event, newHref, linkNode);
  98.             }
  99.  
  100.             return true;
  101.         }
  102.     }
  103.  
  104.     return false;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement