[Greasemonkey] Linkify script debugment

Bill Donnelly donnelly at snowcrest.net
Thu Jul 21 21:47:53 EDT 2005


Found and fixed another bug in Linkify:
(while watching _Junkyard Mega-Wars/the Movies_)
-----------------------------------------------------

// ==UserScript==
// @name          Linkify (New)
// @namespace     http://youngpup.net/userscripts
// @description   Looks for things in the page that look like URLs but 
aren't hyperlinked, and converts them to clickable links.
//                   Debugments by Bill Donnelly: 
http://www.snowcrest.net/donnelly/gmscripts/
// @include       *
// ==/UserScript==

(function () {
   const urlRegex = 
/\b(https?:\/\/(?:[^\s+\"\\'<\>\(\),:.?]|\.(?!$|\.))+(?:\?(?:[^\s+\"\<\>\(\):.]|\.(?!$|\.))+)*)/ig;
   // /\b(https?:\/\/(?:[^\s+\"\<\>\(\),:.]|\.(?!$|\.))+)/ig;
   // failed on: 
http://www.imdb.com/gallery/granitz/3168/Events/3168/JonathanSi_Pimen_5175078_400.jpg?path=pgallery&path_key=Finnigan,%20Jennifer
   // failed on: "<img src='http://image2.sina.com.cn/pfp/iweb/vivi.gif'>"
   //                       -------------------------------------------- 
trailing single quote bug

   // tags we will scan looking for un-hyperlinked urls
   var allowedParents = [
      "abbr", "acronym", "address", "applet", "b", "bdo", "big", 
"blockquote", "body",
      "caption", "center", "cite", "code", "dd", "del", "div", "dfn", 
"dt", "em",
      "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6", 
"i", "iframe",
      "ins", "kdb", "li", "object", "pre", "p", "q", "samp", "small", 
"span", "strike",
      "s", "strong", "sub", "sup", "td", "th", "tt", "u", "var"
      ];

   var xpath = "//text()[(parent::" + allowedParents.join (" or 
parent::") + ") and " +
      "contains (translate (., 'HTTP', 'http'), 'http')]";

   var candidates = document.evaluate (xpath, document, null,
      XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

   for (var cand = null, ii = 0;  (cand = candidates.snapshotItem 
(ii));  ii++) {

      if (urlRegex.test (cand.nodeValue)) {
         var span = document.createElement ("span");
         var source = cand.nodeValue;

         cand.parentNode.replaceChild (span, cand);

         urlRegex.lastIndex = 0;

         for (var match = null, lastLastIndex = 0;  (match = 
urlRegex.exec (source)); ) {

            span.appendChild (document.createTextNode (source.substring 
(lastLastIndex, match.index)));

            var a = document.createElement ("a");
            a.setAttribute ("href", match[0]);
            a.appendChild (document.createTextNode (match[0]));
            span.appendChild (a);

            lastLastIndex = urlRegex.lastIndex;
         }

         span.appendChild (document.createTextNode (source.substring 
(lastLastIndex)));
         span.normalize();
      }
   }

})();

-- 
Jack the Ripper may have been quite insane,
but he had beautiful penmanship.
btw -- Don't look back!
The lemmings are gaining on you.


More information about the Greasemonkey mailing list