Facebook
From Bitty Zebra, 8 Months ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 80
  1. chrome.runtime.onMessage.addListener(
  2.     function(request, sender, sendResponse){
  3.         if(request.message === "clicked_browser_action"){
  4.             console.log('DOM status: ' + document.readyState);
  5.             getAllCss();
  6.             darkenCss();
  7.         }
  8.     }
  9. );
  10.  
  11. var css = [];
  12.  
  13. function getAllCss(){
  14.     console.log('Start');
  15.     if(document.styleSheets) getExternalCss();
  16.  
  17.     var cssInline = css.join('\n') + '\n';
  18.  
  19.     console.log(cssInline);
  20.  
  21.     console.log('End');
  22. }
  23.  
  24. function getExternalCss(){
  25.     for(var i = 0; i < document.styleSheets.length; i++){
  26.         var sheet = document.styleSheets[i];
  27.         var rules = ('cssRules' in sheet)? sheet.cssRules : sheet.rules;
  28.  
  29.         console.log('Sheet ' + i + ': ' + rules);
  30.  
  31.         if(rules){
  32.             css.push('\n/* Stylesheet : ' + (sheet.href || '[inline styles]') + ' */');
  33.             for(var j = 0; j < rules.length; j++){
  34.                 var rule = rules[j];
  35.                 if('cssText' in rule){
  36.                     if(rule.cssText.includes('color'))
  37.                         css.push(rule.cssText);
  38.                         // console.log(rule.cssText);
  39.                 }
  40.                 else{
  41.                     // console.log('No cssText in rule');
  42.                     css.push(rule.selectorText + ' {\n' + rule.style.cssText + '\n}\n');
  43.                 }  
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. var darkCss = [];
  50.  
  51. function darkenCss(){
  52.     for(var i = 0; i < css.length; i++){
  53.         var startIndex = css[i].search('color');
  54.         var cutLine = css[i].substr(startIndex + 7);
  55.         var endIndex = cutLine.search(';');
  56.         var colorText = cutLine.substr(0,endIndex);
  57.  
  58.         var parsedColor = parseColor(colorText);
  59.         console.log('Extracted color:' + colorText);
  60.         console.log('Parsed color: ');
  61.         console.dir(parsedColor);
  62.     }
  63. }
  64.  
  65. function parseColor(input) {
  66.     var mm;
  67.     var m;
  68.     mm = input.match(/^#?([0-9a-f]{3})$/i);
  69.     if (mm) {
  70.         m = mm[1];
  71.         return [
  72.             parseInt(m.charAt(0), 16) * 0x11,
  73.             parseInt(m.charAt(1), 16) * 0x11,
  74.             parseInt(m.charAt(2), 16) * 0x11
  75.         ];
  76.     }
  77.     mm = input.match(/^#?([0-9a-f]{6})$/i);
  78.     if (mm) {
  79.         m = mm[1];
  80.         return [
  81.             parseInt(m.substr(0, 2), 16),
  82.             parseInt(m.substr(2, 2), 16),
  83.             parseInt(m.substr(4, 2), 16)
  84.         ];
  85.     }
  86.     var mm = input.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
  87.     if (mm) {
  88.         return [mm[1], mm[2], mm[3]];
  89.     }
  90.  
  91.     var mm = input.match(/^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+.*\d*)\s*\)$/i) || input.match(/^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
  92.     if( mm ) {
  93.         return [mm[1],mm[2],mm[3], mm[4]===undefined?1:mm[4]];
  94.     }
  95.  
  96.     var webColors = {
  97.         "AliceBlue": "#F0F8FF",
  98.         "AntiqueWhite": "#FAEBD7",
  99.         "Aqua": "#00FFFF",
  100.         "Aquamarine": "#7FFFD4",
  101.         "Azure": "#F0FFFF",
  102.         "Beige": "#F5F5DC",
  103.         "Bisque": "#FFE4C4",
  104.         "Black": "#000000",
  105.         "BlanchedAlmond": "#FFEBCD",
  106.         "Blue": "#0000FF",
  107.         "BlueViolet": "#8A2BE2",
  108.         "Brown": "#A52A2A",
  109.         "BurlyWood": "#DEB887",
  110.         "CadetBlue": "#5F9EA0",
  111.         "Chartreuse": "#7FFF00",
  112.         "Chocolate": "#D2691E",
  113.         "Coral": "#FF7F50",
  114.         "CornflowerBlue": "#6495ED",
  115.         "Cornsilk": "#FFF8DC",
  116.         "Crimson": "#DC143C",
  117.         "Cyan": "#00FFFF",
  118.         "DarkBlue": "#00008B",
  119.         "DarkCyan": "#008B8B",
  120.         "DarkGoldenRod": "#B8860B",
  121.         "DarkGray": "#A9A9A9",
  122.         "DarkGrey": "#A9A9A9",
  123.         "DarkGreen": "#006400",
  124.         "DarkKhaki": "#BDB76B",
  125.         "DarkMagenta": "#8B008B",
  126.         "DarkOliveGreen": "#556B2F",
  127.         "DarkOrange": "#FF8C00",
  128.         "DarkOrchid": "#9932CC",
  129.         "DarkRed": "#8B0000",
  130.         "DarkSalmon": "#E9967A",
  131.         "DarkSeaGreen": "#8FBC8F",
  132.         "DarkSlateBlue": "#483D8B",
  133.         "DarkSlateGray": "#2F4F4F",
  134.         "DarkSlateGrey": "#2F4F4F",
  135.         "DarkTurquoise": "#00CED1",
  136.         "DarkViolet": "#9400D3",
  137.         "DeepPink": "#FF1493",
  138.         "DeepSkyBlue": "#00BFFF",
  139.         "DimGray": "#696969",
  140.         "DimGrey": "#696969",
  141.         "DodgerBlue": "#1E90FF",
  142.         "FireBrick": "#B22222",
  143.         "FloralWhite": "#FFFAF0",
  144.         "ForestGreen": "#228B22",
  145.         "Fuchsia": "#FF00FF",
  146.         "Gainsboro": "#DCDCDC",
  147.         "GhostWhite": "#F8F8FF",
  148.         "Gold": "#FFD700",
  149.         "GoldenRod": "#DAA520",
  150.         "Gray": "#808080",
  151.         "Grey": "#808080",
  152.         "Green": "#008000",
  153.         "GreenYellow": "#ADFF2F",
  154.         "HoneyDew": "#F0FFF0",
  155.         "HotPink": "#FF69B4",
  156.         "IndianRed ": "#CD5C5C",
  157.         "Indigo ": "#4B0082",
  158.         "Ivory": "#FFFFF0",
  159.         "Khaki": "#F0E68C",
  160.         "Lavender": "#E6E6FA",
  161.         "LavenderBlush": "#FFF0F5",
  162.         "LawnGreen": "#7CFC00",
  163.         "LemonChiffon": "#FFFACD",
  164.         "LightBlue": "#ADD8E6",
  165.         "LightCoral": "#F08080",
  166.         "LightCyan": "#E0FFFF",
  167.         "LightGoldenRodYellow": "#FAFAD2",
  168.         "LightGray": "#D3D3D3",
  169.         "LightGrey": "#D3D3D3",
  170.         "LightGreen": "#90EE90",
  171.         "LightPink": "#FFB6C1",
  172.         "LightSalmon": "#FFA07A",
  173.         "LightSeaGreen": "#20B2AA",
  174.         "LightSkyBlue": "#87CEFA",
  175.         "LightSlateGray": "#778899",
  176.         "LightSlateGrey": "#778899",
  177.         "LightSteelBlue": "#B0C4DE",
  178.         "LightYellow": "#FFFFE0",
  179.         "Lime": "#00FF00",
  180.         "LimeGreen": "#32CD32",
  181.         "Linen": "#FAF0E6",
  182.         "Magenta": "#FF00FF",
  183.         "Maroon": "#800000",
  184.         "MediumAquaMarine": "#66CDAA",
  185.         "MediumBlue": "#0000CD",
  186.         "MediumOrchid": "#BA55D3",
  187.         "MediumPurple": "#9370DB",
  188.         "MediumSeaGreen": "#3CB371",
  189.         "MediumSlateBlue": "#7B68EE",
  190.         "MediumSpringGreen": "#00FA9A",
  191.         "MediumTurquoise": "#48D1CC",
  192.         "MediumVioletRed": "#C71585",
  193.         "MidnightBlue": "#191970",
  194.         "MintCream": "#F5FFFA",
  195.         "MistyRose": "#FFE4E1",
  196.         "Moccasin": "#FFE4B5",
  197.         "NavajoWhite": "#FFDEAD",
  198.         "Navy": "#000080",
  199.         "OldLace": "#FDF5E6",
  200.         "Olive": "#808000",
  201.         "OliveDrab": "#6B8E23",
  202.         "Orange": "#FFA500",
  203.         "OrangeRed": "#FF4500",
  204.         "Orchid": "#DA70D6",
  205.         "PaleGoldenRod": "#EEE8AA",
  206.         "PaleGreen": "#98FB98",
  207.         "PaleTurquoise": "#AFEEEE",
  208.         "PaleVioletRed": "#DB7093",
  209.         "PapayaWhip": "#FFEFD5",
  210.         "PeachPuff": "#FFDAB9",
  211.         "Peru": "#CD853F",
  212.         "Pink": "#FFC0CB",
  213.         "Plum": "#DDA0DD",
  214.         "PowderBlue": "#B0E0E6",
  215.         "Purple": "#800080",
  216.         "RebeccaPurple": "#663399",
  217.         "Red": "#FF0000",
  218.         "RosyBrown": "#BC8F8F",
  219.         "RoyalBlue": "#4169E1",
  220.         "SaddleBrown": "#8B4513",
  221.         "Salmon": "#FA8072",
  222.         "SandyBrown": "#F4A460",
  223.         "SeaGreen": "#2E8B57",
  224.         "SeaShell": "#FFF5EE",
  225.         "Sienna": "#A0522D",
  226.         "Silver": "#C0C0C0",
  227.         "SkyBlue": "#87CEEB",
  228.         "SlateBlue": "#6A5ACD",
  229.         "SlateGray": "#708090",
  230.         "SlateGrey": "#708090",
  231.         "Snow": "#FFFAFA",
  232.         "SpringGreen": "#00FF7F",
  233.         "SteelBlue": "#4682B4",
  234.         "Tan": "#D2B48C",
  235.         "Teal": "#008080",
  236.         "Thistle": "#D8BFD8",
  237.         "Tomato": "#FF6347",
  238.         "Turquoise": "#40E0D0",
  239.         "Violet": "#EE82EE",
  240.         "Wheat": "#F5DEB3",
  241.         "White": "#FFFFFF",
  242.         "WhiteSmoke": "#F5F5F5",
  243.         "Yellow": "#FFFF00",
  244.         "YellowGreen": "#9ACD32"
  245.     };
  246.     for (var p in webColors) {
  247.         webColors[p.toLowerCase()] = webColors[p];
  248.     }
  249.     var wc = webColors[input];
  250.     if (wc != null)
  251.         return parseColor(wc);
  252.     console.log(input);
  253.     console.log("'" + input + "' is not a valid color...");
  254.  
  255.     return -1;
  256. }