[Greasemonkey] seeing alternate content

Nic Ferrier nferrier at tapsellferrier.co.uk
Tue Jul 12 00:43:33 EDT 2005


I liked the technoproxy half visible thing so much I adapted it to
make an RSS viewer.

This is very basic and has lots of bugs... but release early, release
often.

Here it is:

  // Copyright (C) 2005 by Tapsell-Ferrier Limited
  // Released under the GPL license
  // http://www.gnu.org/copyleft/gpl.html

  // ==UserScript==
  // @name          syn
  // @namespace     http://www.tapsellferrier.co.uk/downloads
  // @description   make a linked RSS appear on the page as a true alternative
  // @include       http://*
  // ==/UserScript==

  // Shameless rip offs from technoproxy
  var closeIcon = "data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%0F%00%00%00%0F%08%06%00%00%00%3B%D6%95J%00%00%00%06bKGD%00%FF%00%FF%00%FF%A0%BD%A7%93%00%00%00%09pHYs%00%00%0B%13%00%00%0B%13%01%00%9A%9C%18%00%00%00%07tIME%07%D5%07%05%0B%1B%04%A9%D7%EB%7C%00%00%00%1DtEXtComment%00Created%20with%20The%20GIMP%EFd%25n%00%00%00%B6IDAT%28%CF%9D%D3%3BnB1%10%85%E1%CFW%20H%AA4%B4%B0%01%9A%AC%C1%EB%F6%1A%B2%85%40OA%22%24%1E%22%A4%C0%89%AC%8B%7D%03%99%F2%CC%F9%8F%3D%D68%40b%81w%F7%D7Kd%1B%12c%1C%3D%5E%AF%A3%02%9Cb%14%D9%B5%DC%89%E7%EC%3F%E1%AD%2Bz%27L%12%97%06x%C1%19_%3F%DA%2F%1C%AF%E2%A60%F6A%D8%C7%1A%9C%03B%1F%28%83%CA%FE%0D%DC%0A%A8%81U%B8f%AC%81Mx%60%E6a%B8u%D5Z%40%F7%178%14%10%0Aa%96%17%60%5B%9B1%FB%C6x%C2G%FF%E4O%9C%5B%8F%93%F5N%B1%81%211%C7%EA%1F%BB%3D%ED%22k%D7_%F5H-%23%87od-9%C2L%10%A8%1A%00%00%00%00IEND%AEB%60%82";
  var bg = "url(data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%1E%00%00%00%1E%08%06%00%00%00%3B0%AE%A2%00%00%00%06bKGD%00%FF%00%FF%00%FF%A0%BD%A7%93%00%00%00%09pHYs%00%00%0B%13%00%00%0B%13%01%00%9A%9C%18%00%00%00%07tIME%07%D5%07%05%0C%0C%2C%9C%AE%D1%95%00%00%00%1DtEXtComment%00Created%20with%20The%20GIMP%EFd%25n%00%00%00-IDATH%C7%ED%CD1%01%000%08%030%98%7FoH%2A%12v%F2%24%06%D2I%A6%0E%BC%3A%22%16%8B%C5b%B1X%2C%16%8B%C5%E2%BF%05%88%3D%04%08%29%DD%FEc%00%00%00%00IEND%AEB%60%82)";

  // Find alternate content.
  var rss = document.evaluate("//link[@rel='alternate'][contains(@type, 'xml')]",
                              document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

  if (rss.snapshotLength < 1)
    GM_log("no RSS found for " + document.URL);
   else
     {
       GM_log("RSS found for " + document.URL);
       for (var i = 0; i < 1; i++)
         {
           try
             {
               var link = rss.snapshotItem(i);
               var src = link.href;

               GM_xmlhttpRequest({
                 url: src,
                     method: "GET",
                     onload: function (response) {
                     // Handle the response
                     //  GM_log("status was: " + response.status + " data was: " + response.responseText);
                     try
                       {
                         var domparser = new DOMParser();
                         var proc = new XSLTProcessor();
                         var xslt_str = "<?xml version='1.0'?>"
                           + "<xsl:stylesheet "
                           + "    xmlns:rss='http://purl.org/rss/1.0/'"
                           + "    xmlns:dc='http://purl.org/dc/elements/1.1/'"
                           + "    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"
                           + "    version='1.0'>"
                           + "<xsl:output method='html'/>"
                           + "<xsl:template match='/rss/channel'>"
                           + "<xsl:for-each select='//item | //rss:item'>"
                           + "<div class='item'>"
                           + "<div class='title'><a href='{link | rss:link}'><xsl:value-of select='title | rss:title'/></a></div>"
                           + "<div class='text'><xsl:value-of select='description | rss:description'/></div>"
                           + "</div>"
                           + "</xsl:for-each>"
                           + "</xsl:template>"
                           + "</xsl:stylesheet>";
                         var xslt_doc = domparser.parseFromString(xslt_str, 'text/xml');
                         proc.importStylesheet(xslt_doc);

                         var src_doc = domparser.parseFromString(response.responseText, 'text/xml');
                         var result = proc.transformToFragment(src_doc, document);

                         if (result.childNodes.length > 1)
                           {
                             // Setup the containing element.
                             var div = document.createElement("div");
                             div.style.background = bg;
                             div.style.width = "30%";
                             div.style.height = "100%";
                             div.style.position = "fixed";
                             div.style.right = "0";
                             div.style.left = "auto";
                             div.style.font = "smaller sans-serif";
                             div.style.paddingLeft = "2%";
                             div.style.paddingRight = "2%";
                             div.style.textAlign = "right";

                             closeButton = document.createElement("img");
                             closeButton.id = "close";
                             closeButton.src = closeIcon;
                             closeButton.style.paddingTop = "10px";
                             closeButton.style['float'] = "right";
                             closeButton.onclick = function () { div.style.display = "none"; };

                             div.appendChild(closeButton);

                             // Setup CSS for RSS display
                             document.styleSheets[0].insertRule("div.item { padding-bottom: 1em; }", 0);
                             document.styleSheets[0].insertRule("#close { padding-bottom: 1em; ; }", 0);

                             for (var i = 0; i < result.childNodes.length; i++)
                               {
                                 GM_log(result.childNodes[i].innerHTML);
                                 div.appendChild(result.childNodes[i]);
                               }

                             document.body.insertBefore(div, document.body.firstChild);
                           }
                       }
                     catch (e)
                       {
                         GM_log("exception: " + e);
                         return;
                       }
                   }
                 });

             }
           catch (e)
             {
               GM_log("failed with " + e);
             }
         }
     }

  // End


More information about the Greasemonkey mailing list