Index: lib/cssInjection.js |
=================================================================== |
--- a/lib/cssInjection.js |
+++ b/lib/cssInjection.js |
@@ -25,33 +25,48 @@ |
const {checkWhitelisted} = require("whitelisting"); |
const {extractHostFromFrame} = require("url"); |
const {port} = require("messaging"); |
const devtools = require("devtools"); |
const userStyleSheetsSupported = "extensionTypes" in chrome && |
"CSSOrigin" in chrome.extensionTypes; |
-function hideElements(tabId, frameId, selectors) |
+function convertSelectors(selectors) |
+{ |
+ return selectors.join(", ") + "{display: none !important;}"; |
+} |
+ |
+function addStyleSheet(tabId, frameId, selectors) |
{ |
chrome.tabs.insertCSS(tabId, { |
- code: selectors.join(", ") + "{display: none !important;}", |
+ code: convertSelectors(selectors), |
cssOrigin: "user", |
frameId, |
matchAboutBlank: true, |
runAt: "document_start" |
}); |
} |
-port.on("elemhide.getSelectors", (msg, sender) => |
+function removeStyleSheet(tabId, frameId, selectors) |
+{ |
+ chrome.tabs.removeCSS(tabId, { |
+ code: convertSelectors(selectors), |
+ cssOrigin: "user", |
+ frameId, |
+ matchAboutBlank: true |
+ }); |
+} |
+ |
+port.on("elemhide.getSelectors", (message, sender) => |
{ |
let selectors = []; |
let emulatedPatterns = []; |
let trace = devtools && devtools.hasPanel(sender.page); |
- let inject = !userStyleSheetsSupported; |
+ let inline = !userStyleSheetsSupported; |
if (!checkWhitelisted(sender.page, sender.frame, |
RegExpFilter.typeMap.DOCUMENT | |
RegExpFilter.typeMap.ELEMHIDE)) |
{ |
let hostname = extractHostFromFrame(sender.frame); |
let specificOnly = checkWhitelisted(sender.page, sender.frame, |
RegExpFilter.typeMap.GENERICHIDE); |
@@ -60,22 +75,26 @@ |
hostname, |
specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
); |
for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) |
emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
} |
- if (!inject && selectors.length > 0) |
- hideElements(sender.page.id, sender.frame.id, selectors); |
+ if (!inline && selectors.length > 0) |
+ addStyleSheet(sender.page.id, sender.frame.id, selectors); |
- let response = {trace, inject, emulatedPatterns}; |
- if (trace || inject) |
+ let response = {trace, inline, emulatedPatterns}; |
+ if (trace || inline) |
response.selectors = selectors; |
return response; |
}); |
-port.on("elemhide.injectSelectors", (msg, sender) => |
+port.on("elemhide.injectSelectors", (message, sender) => |
{ |
- hideElements(sender.page.id, sender.frame.id, msg.selectors); |
+ if (message.oldSelectors.length > 0) |
+ removeStyleSheet(sender.page.id, sender.frame.id, message.oldSelectors); |
+ |
+ if (message.selectors.length > 0) |
+ addStyleSheet(sender.page.id, sender.frame.id, message.selectors); |
}); |