Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/cssInjection.js

Issue 29575739: Issue 5864 - Remove previous style sheet before adding one (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Remove some code duplication Created Oct. 14, 2017, 9:52 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
});
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld