Left: | ||
Right: |
OLD | NEW |
---|---|
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** @module cssInjection */ | 18 /** @module cssInjection */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const {RegExpFilter} = require("filterClasses"); | 22 const {RegExpFilter} = require("filterClasses"); |
23 const {ElemHide} = require("elemHide"); | 23 const {ElemHide} = require("elemHide"); |
24 const {checkWhitelisted} = require("whitelisting"); | 24 const {checkWhitelisted} = require("whitelisting"); |
25 const {extractHostFromFrame} = require("url"); | 25 const {extractHostFromFrame} = require("url"); |
26 const {port} = require("messaging"); | 26 const {port} = require("messaging"); |
27 const devtools = require("devtools"); | 27 const devtools = require("devtools"); |
28 | 28 |
29 let userStylesheetsSupported = true; | 29 let userStyleSheetsSupported = "extensionTypes" in chrome && |
Manish Jethani
2017/09/15 13:02:42
Note: "style sheet" is actually two words, I've ch
Sebastian Noack
2017/09/15 18:46:37
In which case, does chrome.extensionTypes does not
Manish Jethani
2017/09/15 21:26:08
It doesn't exist on Chrome.
Sebastian Noack
2017/09/15 22:52:44
Indeed. The MDN compatibility information are misl
| |
30 "CSSOrigin" in chrome.extensionTypes; | |
30 | 31 |
31 function hideElements(tabId, frameId, selectors) | 32 function hideElements(tabId, frameId, selectors) |
32 { | 33 { |
33 let code = selectors.join(", ") + "{display: none !important;}"; | 34 chrome.tabs.insertCSS(tabId, |
Sebastian Noack
2017/09/15 18:46:37
Nit: The additional indentation (and one additiona
Manish Jethani
2017/09/15 21:26:09
Done.
| |
34 | 35 { |
35 try | 36 code: selectors.join(", ") + "{display: none !important;}", |
36 { | 37 cssOrigin: "user", |
37 chrome.tabs.insertCSS(tabId, | 38 frameId, |
38 { | 39 matchAboutBlank: true, |
39 code, | 40 runAt: "document_start" |
40 cssOrigin: "user", | 41 } |
41 frameId, | 42 ); |
42 matchAboutBlank: true, | |
43 runAt: "document_start" | |
44 } | |
45 ); | |
46 return true; | |
47 } | |
48 catch (error) | |
49 { | |
50 if (/\bError processing cssOrigin\b/.test(error.message) == -1) | |
51 throw error; | |
52 | |
53 userStylesheetsSupported = false; | |
54 return false; | |
55 } | |
56 } | 43 } |
57 | 44 |
58 port.on("elemhide.getSelectors", (msg, sender) => | 45 port.on("elemhide.getSelectors", (msg, sender) => |
59 { | 46 { |
60 let selectors; | 47 let selectors; |
61 let trace = devtools && devtools.hasPanel(sender.page); | 48 let trace = devtools && devtools.hasPanel(sender.page); |
62 | 49 |
63 if (!checkWhitelisted(sender.page, sender.frame, | 50 if (!checkWhitelisted(sender.page, sender.frame, |
64 RegExpFilter.typeMap.DOCUMENT | | 51 RegExpFilter.typeMap.DOCUMENT | |
65 RegExpFilter.typeMap.ELEMHIDE)) | 52 RegExpFilter.typeMap.ELEMHIDE)) |
66 { | 53 { |
67 let specificOnly = checkWhitelisted(sender.page, sender.frame, | 54 let specificOnly = checkWhitelisted(sender.page, sender.frame, |
68 RegExpFilter.typeMap.GENERICHIDE); | 55 RegExpFilter.typeMap.GENERICHIDE); |
69 selectors = ElemHide.getSelectorsForDomain( | 56 selectors = ElemHide.getSelectorsForDomain( |
70 extractHostFromFrame(sender.frame), | 57 extractHostFromFrame(sender.frame), |
71 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING | 58 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
72 ); | 59 ); |
73 } | 60 } |
74 else | 61 else |
75 { | 62 { |
76 selectors = []; | 63 selectors = []; |
77 } | 64 } |
78 | 65 |
79 if (selectors.length == 0 || userStylesheetsSupported && | 66 if (selectors.length == 0 || userStyleSheetsSupported) |
Sebastian Noack
2017/09/15 18:46:37
I had a go before. But you were quicker, getting y
Manish Jethani
2017/09/15 21:26:08
I've incorporated the simplifications and made som
| |
80 hideElements(sender.page.id, sender.frame.id, selectors)) | |
81 { | 67 { |
68 if (userStyleSheetsSupported && selectors.length > 0) | |
69 hideElements(sender.page.id, sender.frame.id, selectors); | |
Manish Jethani
2017/09/15 13:02:42
If userStyleSheetsSupported is true then a call to
| |
70 | |
82 if (trace) | 71 if (trace) |
83 return {selectors, trace: true, inject: false}; | 72 return {selectors, trace: true, inject: false}; |
84 | 73 |
85 return {trace: false, inject: false}; | 74 return {trace: false, inject: false}; |
86 } | 75 } |
87 | 76 |
88 return {selectors, trace, inject: true}; | 77 return {selectors, trace, inject: true}; |
89 }); | 78 }); |
90 | 79 |
91 port.on("elemhide.injectSelectors", (msg, sender) => | 80 port.on("elemhide.injectSelectors", (msg, sender) => |
92 { | 81 { |
93 return hideElements(sender.page.id, sender.frame.id, msg.selectors); | 82 if (userStyleSheetsSupported) |
Manish Jethani
2017/09/15 13:02:42
Since there's no try...catch in hideElements now,
Sebastian Noack
2017/09/15 18:46:37
It seems no message of this type is send by the co
Manish Jethani
2017/09/15 21:26:09
Done.
| |
83 hideElements(sender.page.id, sender.frame.id, msg.selectors); | |
94 }); | 84 }); |
OLD | NEW |