| Index: lib/scriptInjection.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/lib/scriptInjection.js |
| @@ -0,0 +1,72 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-present eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +/** @module scriptInjection */ |
| + |
| +"use strict"; |
| + |
| +const {defaultMatcher} = require("matcher"); |
| +const {RegExpFilter} = require("filterClasses"); |
| +const {extractHostFromFrame, isThirdParty, stringifyURL} = require("url"); |
| + |
| +let {typeMap} = RegExpFilter; |
| + |
| +function injectScript(tabId, frameId) |
| +{ |
| + try |
| + { |
| + browser.tabs.executeScript(tabId, { |
| + file: "/inject.preload.js", |
| + frameId, |
| + matchAboutBlank: true, |
| + runAt: "document_start" |
| + }); |
| + } |
| + catch (error) |
| + { |
| + } |
| +} |
| + |
| +browser.webNavigation.onCommitted.addListener(({tabId, frameId, url}) => |
| +{ |
| + // There's a bug in Chrome that causes webNavigation.onCommitted to get |
| + // dispatched twice if there's a URL filter present, therefore we must listen |
| + // for all URLs and do an explicit check here. |
| + // https://crbug.com/827855 |
| + if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(url)) |
| + return; |
| + |
| + let urlObject = new URL(url); |
| + let urlString = stringifyURL(urlObject); |
| + |
| + let frame = ext.getFrame(tabId, frameId); |
| + if (!frame) |
| + return; |
| + |
| + let hostname = extractHostFromFrame(frame); |
| + let thirdParty = isThirdParty(urlObject, hostname); |
| + |
| + let filter = defaultMatcher.matchesAny(urlString, typeMap.WEBRTC, hostname, |
| + thirdParty, null, false); |
| + if (!filter) |
| + return; |
| + |
| + // As long as there's a matching filter, inject the API wrappers. We could |
| + // check if the URL is whitelisted or if the filter is a whitelisting filter, |
| + // but it's not worth it (i.e. false positives are OK). |
| + injectScript(tabId, frameId); |
| +}); |