Advertisement
decembre

JAVASCRIPT LIbrary for E4X - Greasemonkey script

Dec 16th, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        JAVASCRIPT LIbrary for E4X
  3. // @namespace   decembre
  4. // @include     *
  5. // @version     1
  6. // @grant       none
  7. // ==/UserScript==
  8. /*
  9.  * e4x.js
  10.  *
  11.  * A JavaScript library that implements the optional E4X features described in
  12.  * ECMA-357 2nd Edition Annex A if they are not already implemented.
  13.  *
  14.  * 2010-06-30
  15.  *
  16.  * By Eli Grey, http://eligrey.com
  17.  * License: The X11/MIT license (see LICENSE.md)
  18.  */
  19.  
  20. /*global document, XML, XMLList, XMLSerializer, XPathResult */
  21.  
  22. /*jslint undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true,
  23.   newcap: true, immed: true, maxerr: 1000, maxlen: 90 */
  24.  
  25.  // Solution given by Paul Sweatte (StackOverflow):
  26.  //<a href="http://stackoverflow.com/users/1113772/paul-sweatte">Paul Sweatte</a>
  27.  //In <a href="http://stackoverflow.com/questions/17651655/convert-a-greasemonkey-script-after-e4x-is-not-supported">How to convert this script which use deprecadted E4X ? :</a>
  28.  
  29.   //The JavaScript implementation uses XMLSerializer, which is unsafe in Greasemonkey.
  30.   //As a workaround, copy and paste the code into your script,
  31.   //then replace XMLSerializer with unsafeWindow.XMLSerializer to access that API via the target page.
  32.   //Be aware that it is possible for scripts on the target page to follow unsafeWindow usage back to the Greasemonkey script reference and thus gain elevated privileges
  33.  
  34. /*! @source http://purl.eligrey.com/github/e4x.js/blob/master/e4x.js*/
  35.  
  36. "use strict";
  37.  
  38. (function (XML) {
  39.     var hostDoc   = document,
  40.     unsafeWindow.XMLSerializer = new unsafeWindow.XMLSerializer,
  41.     piName        = /^[\w\-]+\s*/,
  42.     NULL          = null,
  43.     createDoc     = function (docElem) {
  44.         var domDoc = hostDoc.implementation.createDocument(NULL, NULL, NULL);
  45.         if (docElem) {
  46.             domDoc.appendChild(docElem);
  47.         }
  48.         return domDoc;
  49.     },
  50.     xmlDoc        = createDoc(),
  51.    
  52.     xmlToDomNode = function (xml) {
  53.         var node;
  54.        
  55.         switch (xml.nodeKind()) {
  56.             case "element":
  57.                 var attributes = xml.attributes(),
  58.                     children   = xml.children(),
  59.                     childLen   = children.length(),
  60.                     attLen     = attributes.length(),
  61.                     i, attribute;
  62.                
  63.                 node = xmlDoc.createElementNS(
  64.                     xml.namespace().uri,
  65.                     xml.localName()
  66.                 );
  67.                
  68.                 if (attLen !== 0) {
  69.                     for (i = 0; i < attLen; i++) {
  70.                         attribute = attributes[i];
  71.                         node.setAttributeNS(
  72.                             attribute.namespace().uri,
  73.                             attribute.localName(),
  74.                             attribute.toString()
  75.                         );
  76.                     }
  77.                 }
  78.                 if (childLen !== 0) {
  79.                     for (i = 0; i < childLen; i++) {
  80.                         node.appendChild(xmlToDomNode(children[i]));
  81.                     }
  82.                 }
  83.                
  84.                 return node;
  85.            
  86.             case "text":
  87.                 return xmlDoc.createTextNode(xml.toString());
  88.            
  89.             case "comment":
  90.                 return xmlDoc.createComment(xml.toString().slice(4, -3));
  91.            
  92.             case "processing-instruction":
  93.                 return xmlDoc.createProcessingInstruction(
  94.                     xml.localName(),
  95.                     xml.toString().slice(2, -2).replace(piName, "")
  96.                 );
  97.            
  98.             case "attribute":
  99.                 (node = xmlDoc.createAttributeNS(
  100.                     xml.namespace().uri,
  101.                     xml.localName()
  102.                 )).nodeValue = xml.toString();
  103.                 return node;
  104.         }
  105.     },
  106.     xmlMethods = {
  107.         domNode: function () {
  108.             if (this.length() === 1) {
  109.                 return hostDoc.adoptNode(xmlToDomNode(this));
  110.             }
  111.         },
  112.         domNodeList: function () {
  113.             var fragment = hostDoc.createDocumentFragment();
  114.        
  115.             for (var i = 0, len = this.length(); i < len; i++) {
  116.                 fragment.appendChild(this[i].domNode());
  117.             }
  118.        
  119.             return hostDoc.adoptNode(fragment).childNodes;
  120.         },
  121.         xpath: function (xpathExp) {
  122.             var res = new XMLList,
  123.                 len = this.length();
  124.    
  125.             if (len !== 1) {
  126.                 for (var i = 0, len = len; i < len; i++) {
  127.                     res += this[i].xpath(xpathExp);
  128.                 }
  129.                 return res;
  130.             }
  131.    
  132.             var domDoc = createDoc(this.domNode()),
  133.             xpr = domDoc.evaluate(
  134.                 xpathExp,
  135.                 domDoc.documentElement,
  136.                 domDoc.createNSResolver(domDoc.documentElement),
  137.                 XPathResult.ORDERED_NODE_ITERATOR_TYPE,
  138.                 NULL
  139.             ),
  140.             node;
  141.    
  142.             while (node = xpr.iterateNext()) {
  143.                 // Unfortunately, there's no efficient native XML.fromDomNode(node) method
  144.                 res += new XML(unsafeWindow.XMLSerializer.serializeToString(node));
  145.             }
  146.    
  147.             return res;
  148.         }
  149.     },
  150.     method;
  151.    
  152.     for (method in xmlMethods) {
  153.         if (xmlMethods.hasOwnProperty(method) && !XML.prototype.function::[method]) {
  154.             XML.prototype.function::[method] = xmlMethods[method];
  155.         }
  156.     }
  157. }(XML));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement