| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  * This file is part of Adblock Plus <https://adblockplus.org/>, |    2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 
|    3  * Copyright (C) 2006-present eyeo GmbH |    3  * Copyright (C) 2006-present eyeo GmbH | 
|    4  * |    4  * | 
|    5  * Adblock Plus is free software: you can redistribute it and/or modify |    5  * Adblock Plus is free software: you can redistribute it and/or modify | 
|    6  * it under the terms of the GNU General Public License version 3 as |    6  * it under the terms of the GNU General Public License version 3 as | 
|    7  * published by the Free Software Foundation. |    7  * published by the Free Software Foundation. | 
|    8  * |    8  * | 
|    9  * Adblock Plus is distributed in the hope that it will be useful, |    9  * Adblock Plus is distributed in the hope that it will be useful, | 
|   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of |   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
|   22 const {RegExpFilter} = require("filterClasses"); |   22 const {RegExpFilter} = require("filterClasses"); | 
|   23 const {ElemHide} = require("elemHide"); |   23 const {ElemHide} = require("elemHide"); | 
|   24 const {ElemHideEmulation} = require("elemHideEmulation"); |   24 const {ElemHideEmulation} = require("elemHideEmulation"); | 
|   25 const {checkWhitelisted} = require("whitelisting"); |   25 const {checkWhitelisted} = require("whitelisting"); | 
|   26 const {extractHostFromFrame} = require("url"); |   26 const {extractHostFromFrame} = require("url"); | 
|   27 const {port} = require("messaging"); |   27 const {port} = require("messaging"); | 
|   28 const devtools = require("devtools"); |   28 const devtools = require("devtools"); | 
|   29  |   29  | 
|   30 const userStyleSheetsSupported = "extensionTypes" in browser && |   30 const userStyleSheetsSupported = "extensionTypes" in browser && | 
|   31                                  "CSSOrigin" in browser.extensionTypes; |   31                                  "CSSOrigin" in browser.extensionTypes; | 
 |   32 const selectorGroupSize = 1024; | 
 |   33  | 
 |   34 function* splitSelectors(selectors) | 
 |   35 { | 
 |   36   // Chromium's Blink engine supports only up to 8,192 selectors; more | 
 |   37   // specifically, it ignores any selectors that start at index 8192 or beyond | 
 |   38   // in the list of plain selectors. In order to avoid spilling outside of this | 
 |   39   // range, we simply add multiple rules in groups of up to 1,024 selectors | 
 |   40   // each. | 
 |   41   // See issue #6298 and https://crbug.com/804179 | 
 |   42   for (let i = 0; i < selectors.length; i += selectorGroupSize) | 
 |   43     yield selectors.slice(i, i + selectorGroupSize); | 
 |   44 } | 
 |   45  | 
 |   46 function* createRules(selectors) | 
 |   47 { | 
 |   48   for (let selectorGroup of splitSelectors(selectors)) | 
 |   49     yield selectorGroup.join(", ") + " {display: none !important;}"; | 
 |   50 } | 
 |   51  | 
 |   52 function createStyleSheet(selectors) | 
 |   53 { | 
 |   54   return Array.from(createRules(selectors)).join("\n"); | 
 |   55 } | 
|   32  |   56  | 
|   33 function addStyleSheet(tabId, frameId, styleSheet) |   57 function addStyleSheet(tabId, frameId, styleSheet) | 
|   34 { |   58 { | 
|   35   browser.tabs.insertCSS(tabId, { |   59   browser.tabs.insertCSS(tabId, { | 
|   36     code: styleSheet, |   60     code: styleSheet, | 
|   37     cssOrigin: "user", |   61     cssOrigin: "user", | 
|   38     frameId, |   62     frameId, | 
|   39     matchAboutBlank: true, |   63     matchAboutBlank: true, | 
|   40     runAt: "document_start" |   64     runAt: "document_start" | 
|   41   }); |   65   }); | 
|   42 } |   66 } | 
|   43  |   67  | 
|   44 function removeStyleSheet(tabId, frameId, styleSheet) |   68 function removeStyleSheet(tabId, frameId, styleSheet) | 
|   45 { |   69 { | 
|   46   browser.tabs.removeCSS(tabId, { |   70   browser.tabs.removeCSS(tabId, { | 
|   47     code: styleSheet, |   71     code: styleSheet, | 
|   48     cssOrigin: "user", |   72     cssOrigin: "user", | 
|   49     frameId, |   73     frameId, | 
|   50     matchAboutBlank: true |   74     matchAboutBlank: true | 
|   51   }); |   75   }); | 
|   52 } |   76 } | 
|   53  |   77  | 
|   54 function updateFrameStyles(tabId, frameId, selectors, groupName) |   78 function updateFrameStyles(tabId, frameId, selectors, groupName) | 
|   55 { |   79 { | 
|   56   let styleSheet = null; |   80   let styleSheet = null; | 
|   57   if (selectors.length > 0) |   81   if (selectors.length > 0) | 
|   58     styleSheet = selectors.join(", ") + "{display: none !important;}"; |   82     styleSheet = createStyleSheet(selectors); | 
|   59  |   83  | 
|   60   let frame = ext.getFrame(tabId, frameId); |   84   let frame = ext.getFrame(tabId, frameId); | 
|   61   if (!frame.injectedStyleSheets) |   85   if (!frame.injectedStyleSheets) | 
|   62     frame.injectedStyleSheets = new Map(); |   86     frame.injectedStyleSheets = new Map(); | 
|   63  |   87  | 
|   64   let oldStyleSheet = frame.injectedStyleSheets.get(groupName); |   88   let oldStyleSheet = frame.injectedStyleSheets.get(groupName); | 
|   65  |   89  | 
|   66   // Ideally we would compare the old and new style sheets and skip this code |   90   // Ideally we would compare the old and new style sheets and skip this code | 
|   67   // if they're the same, but the old style sheet can be a leftover from a |   91   // if they're the same, but the old style sheet can be a leftover from a | 
|   68   // previous instance of the frame. We must add the new style sheet |   92   // previous instance of the frame. We must add the new style sheet | 
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  114     response.selectors = selectors; |  138     response.selectors = selectors; | 
|  115  |  139  | 
|  116   return response; |  140   return response; | 
|  117 }); |  141 }); | 
|  118  |  142  | 
|  119 port.on("elemhide.injectSelectors", (message, sender) => |  143 port.on("elemhide.injectSelectors", (message, sender) => | 
|  120 { |  144 { | 
|  121   updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |  145   updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, | 
|  122                     message.groupName); |  146                     message.groupName); | 
|  123 }); |  147 }); | 
| OLD | NEW |