OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 /** @module elemHideHelper */ |
| 19 |
| 20 "use strict"; |
| 21 |
| 22 const {RegExpFilter} = require("filterClasses"); |
| 23 const {ElemHide} = require("elemHide"); |
| 24 const {checkWhitelisted} = require("whitelisting"); |
| 25 const {extractHostFromFrame} = require("url"); |
| 26 const {port} = require("messaging"); |
| 27 const devtools = require("devtools"); |
| 28 |
| 29 let userStylesheetsSupported = true; |
| 30 |
| 31 function hideElements(tabId, frameId, selectors) |
| 32 { |
| 33 return new Promise(resolve => |
| 34 { |
| 35 let code = selectors.join(", ") + "{display: none !important;}"; |
| 36 |
| 37 try |
| 38 { |
| 39 chrome.tabs.insertCSS(tabId, |
| 40 { |
| 41 code, |
| 42 cssOrigin: "user", |
| 43 frameId, |
| 44 matchAboutBlank: true |
| 45 }, |
| 46 () => |
| 47 { |
| 48 resolve(!chrome.runtime.lastError); |
| 49 } |
| 50 ); |
| 51 } |
| 52 catch (error) |
| 53 { |
| 54 if (/\bError processing cssOrigin\b/.test(error.message) == -1) |
| 55 throw error; |
| 56 |
| 57 userStylesheetsSupported = false; |
| 58 resolve(false); |
| 59 } |
| 60 }); |
| 61 } |
| 62 |
| 63 port.on("get-selectors", (msg, sender) => |
| 64 { |
| 65 let selectors; |
| 66 let trace = devtools && devtools.hasPanel(sender.page); |
| 67 |
| 68 if (!checkWhitelisted(sender.page, sender.frame, |
| 69 RegExpFilter.typeMap.DOCUMENT | |
| 70 RegExpFilter.typeMap.ELEMHIDE)) |
| 71 { |
| 72 let specificOnly = checkWhitelisted(sender.page, sender.frame, |
| 73 RegExpFilter.typeMap.GENERICHIDE); |
| 74 selectors = ElemHide.getSelectorsForDomain( |
| 75 extractHostFromFrame(sender.frame), |
| 76 specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
| 77 ); |
| 78 } |
| 79 else |
| 80 { |
| 81 selectors = []; |
| 82 } |
| 83 |
| 84 if (!userStylesheetsSupported) |
| 85 return {selectors, trace, inject: true}; |
| 86 |
| 87 if (selectors.length == 0) |
| 88 { |
| 89 if (trace) |
| 90 return {selectors, trace, inject: false}; |
| 91 |
| 92 return {trace, inject: false}; |
| 93 } |
| 94 |
| 95 return hideElements(sender.page.id, sender.frame.id, selectors) |
| 96 .then(success => |
| 97 { |
| 98 let response = {trace, inject: !success}; |
| 99 |
| 100 if (trace || !success) |
| 101 response.selectors = selectors; |
| 102 |
| 103 return response; |
| 104 }); |
| 105 }); |
| 106 |
| 107 port.on("hide-elements", (msg, sender) => |
| 108 { |
| 109 if (!msg.selectors || msg.selectors.length == 0) |
| 110 return true; |
| 111 |
| 112 return hideElements(sender.page.id, sender.frame.id, msg.selectors); |
| 113 }); |
OLD | NEW |