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

Side by Side 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 style sheet if selectors list is empty Created Oct. 14, 2017, 1:34 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 12 matching lines...) Expand all
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 chrome && 30 const userStyleSheetsSupported = "extensionTypes" in chrome &&
31 "CSSOrigin" in chrome.extensionTypes; 31 "CSSOrigin" in chrome.extensionTypes;
32 32
33 function hideElements(tabId, frameId, selectors) 33 function convertSelectors(selectors)
34 {
35 return selectors.join(", ") + "{display: none !important;}";
36 }
37
38 function addStyleSheet(tabId, frameId, selectors)
34 { 39 {
35 chrome.tabs.insertCSS(tabId, { 40 chrome.tabs.insertCSS(tabId, {
36 code: selectors.join(", ") + "{display: none !important;}", 41 code: convertSelectors(selectors),
37 cssOrigin: "user", 42 cssOrigin: "user",
38 frameId, 43 frameId,
39 matchAboutBlank: true, 44 matchAboutBlank: true,
40 runAt: "document_start" 45 runAt: "document_start"
41 }); 46 });
42 } 47 }
43 48
49 function removeStyleSheet(tabId, frameId, selectors)
50 {
51 chrome.tabs.removeCSS(tabId, {
52 code: convertSelectors(selectors),
53 cssOrigin: "user",
54 frameId,
55 matchAboutBlank: true
56 });
57 }
58
44 port.on("elemhide.getSelectors", (msg, sender) => 59 port.on("elemhide.getSelectors", (msg, sender) =>
45 { 60 {
46 let selectors = []; 61 let selectors = [];
47 let emulatedPatterns = []; 62 let emulatedPatterns = [];
48 let trace = devtools && devtools.hasPanel(sender.page); 63 let trace = devtools && devtools.hasPanel(sender.page);
49 let inject = !userStyleSheetsSupported; 64 let inject = !userStyleSheetsSupported;
50 65
51 if (!checkWhitelisted(sender.page, sender.frame, 66 if (!checkWhitelisted(sender.page, sender.frame,
52 RegExpFilter.typeMap.DOCUMENT | 67 RegExpFilter.typeMap.DOCUMENT |
53 RegExpFilter.typeMap.ELEMHIDE)) 68 RegExpFilter.typeMap.ELEMHIDE))
54 { 69 {
55 let hostname = extractHostFromFrame(sender.frame); 70 let hostname = extractHostFromFrame(sender.frame);
56 let specificOnly = checkWhitelisted(sender.page, sender.frame, 71 let specificOnly = checkWhitelisted(sender.page, sender.frame,
57 RegExpFilter.typeMap.GENERICHIDE); 72 RegExpFilter.typeMap.GENERICHIDE);
58 73
59 selectors = ElemHide.getSelectorsForDomain( 74 selectors = ElemHide.getSelectorsForDomain(
60 hostname, 75 hostname,
61 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING 76 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING
62 ); 77 );
63 78
64 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 79 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
65 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 80 emulatedPatterns.push({selector: filter.selector, text: filter.text});
66 } 81 }
67 82
68 if (!inject && selectors.length > 0) 83 if (!inject && selectors.length > 0)
69 hideElements(sender.page.id, sender.frame.id, selectors); 84 addStyleSheet(sender.page.id, sender.frame.id, selectors);
70 85
71 let response = {trace, inject, emulatedPatterns}; 86 let response = {trace, inject, emulatedPatterns};
72 if (trace || inject) 87 if (trace || inject)
73 response.selectors = selectors; 88 response.selectors = selectors;
74 89
75 return response; 90 return response;
76 }); 91 });
77 92
78 port.on("elemhide.injectSelectors", (msg, sender) => 93 port.on("elemhide.injectSelectors", (msg, sender) =>
79 { 94 {
80 hideElements(sender.page.id, sender.frame.id, msg.selectors); 95 if (msg.previousSelectors)
96 removeStyleSheet(sender.page.id, sender.frame.id, msg.previousSelectors);
97
98 if (msg.selectors)
99 addStyleSheet(sender.page.id, sender.frame.id, msg.selectors);
81 }); 100 });
OLDNEW
« include.preload.js ('K') | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld