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: Maintain all style sheets in lib/cssInjection.js Created Oct. 18, 2017, 1:05 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
« include.preload.js ('K') | « 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,77 @@
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 addStyleSheet(tabId, frameId, styleSheet)
{
chrome.tabs.insertCSS(tabId, {
- code: selectors.join(", ") + "{display: none !important;}",
+ code: styleSheet,
cssOrigin: "user",
frameId,
matchAboutBlank: true,
runAt: "document_start"
});
}
-port.on("elemhide.getSelectors", (msg, sender) =>
+function removeStyleSheet(tabId, frameId, styleSheet)
+{
+ chrome.tabs.removeCSS(tabId, {
+ code: styleSheet,
+ cssOrigin: "user",
+ frameId,
+ matchAboutBlank: true
+ });
+}
+
+function replaceStyleSheet(tabId, frameId, oldStyleSheet, newStyleSheet)
+{
+ // Add the new style sheet first to avoid previously hidden elements from
+ // reappearing momentarily.
+ if (newStyleSheet)
+ addStyleSheet(tabId, frameId, newStyleSheet);
+
+ // Sometimes the old and new style sheets can be exactly the same. In such a
+ // case, do not remove the "old" style sheet, because it is in fact the new
+ // style sheet now.
+ if (oldStyleSheet && oldStyleSheet != newStyleSheet)
+ removeStyleSheet(tabId, frameId, oldStyleSheet);
+}
+
+function updateFrameStyles(pageId, frameId, selectors, groupName)
+{
+ let styleSheet = null;
+ if (selectors.length > 0)
+ styleSheet = selectors.join(", ") + "{display: none !important;}";
+
+ let frame = ext.getFrame(pageId, frameId);
+ if (!frame.injectedStyleSheets)
+ frame.injectedStyleSheets = new Map();
+
+ let oldStyleSheet = frame.injectedStyleSheets.get(groupName);
+
+ // Ideally we would compare the old and new style sheets and skip this call
+ // if they're the same, but the old style sheet can be a leftover from a
+ // previous instance of the frame. We must add the new style sheet
+ // regardless.
+ replaceStyleSheet(pageId, frameId, oldStyleSheet, styleSheet);
+ frame.injectedStyleSheets.set(groupName, styleSheet);
+}
+
+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 +104,23 @@
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)
+ updateFrameStyles(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) =>
Manish Jethani 2017/10/18 13:16:45 This is now again "injectSelectors"
{
- hideElements(sender.page.id, sender.frame.id, msg.selectors);
+ updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
+ message.groupName);
});
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld