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

Unified Diff: lib/cssInjection.js

Issue 29545645: Issue 5695 - Use tabs.insertCSS if extensionTypes.CSSOrigin exists (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Flatten out logic and remove blank line Created Sept. 20, 2017, 2:14 a.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
@@ -21,74 +21,54 @@
const {RegExpFilter} = require("filterClasses");
const {ElemHide} = require("elemHide");
const {checkWhitelisted} = require("whitelisting");
const {extractHostFromFrame} = require("url");
const {port} = require("messaging");
const devtools = require("devtools");
-let userStylesheetsSupported = true;
+const userStyleSheetsSupported = "extensionTypes" in chrome &&
+ "CSSOrigin" in chrome.extensionTypes;
function hideElements(tabId, frameId, selectors)
{
- let code = selectors.join(", ") + "{display: none !important;}";
-
- try
- {
- chrome.tabs.insertCSS(tabId,
- {
- code,
- cssOrigin: "user",
- frameId,
- matchAboutBlank: true,
- runAt: "document_start"
- }
- );
- return true;
- }
- catch (error)
- {
- if (/\bError processing cssOrigin\b/.test(error.message) == -1)
- throw error;
-
- userStylesheetsSupported = false;
- return false;
- }
+ chrome.tabs.insertCSS(tabId, {
+ code: selectors.join(", ") + "{display: none !important;}",
+ cssOrigin: "user",
+ frameId,
+ matchAboutBlank: true,
+ runAt: "document_start"
+ });
}
port.on("elemhide.getSelectors", (msg, sender) =>
{
- let selectors;
+ let selectors = [];
let trace = devtools && devtools.hasPanel(sender.page);
+ let inject = !userStyleSheetsSupported;
if (!checkWhitelisted(sender.page, sender.frame,
RegExpFilter.typeMap.DOCUMENT |
RegExpFilter.typeMap.ELEMHIDE))
{
let specificOnly = checkWhitelisted(sender.page, sender.frame,
RegExpFilter.typeMap.GENERICHIDE);
selectors = ElemHide.getSelectorsForDomain(
extractHostFromFrame(sender.frame),
specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
);
}
- else
- {
- selectors = [];
- }
+
+ if (!inject && selectors.length > 0)
+ hideElements(sender.page.id, sender.frame.id, selectors);
- if (selectors.length == 0 || userStylesheetsSupported &&
- hideElements(sender.page.id, sender.frame.id, selectors))
- {
- if (trace)
- return {selectors, trace: true, inject: false};
+ let response = {trace, inject};
+ if (trace || inject)
+ response.selectors = selectors;
- return {trace: false, inject: false};
- }
-
- return {selectors, trace, inject: true};
+ return response;
});
port.on("elemhide.injectSelectors", (msg, sender) =>
{
- return hideElements(sender.page.id, sender.frame.id, msg.selectors);
+ hideElements(sender.page.id, sender.frame.id, msg.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