OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present 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 scriptInjection */ |
| 19 |
| 20 "use strict"; |
| 21 |
| 22 const {defaultMatcher} = require("matcher"); |
| 23 const {RegExpFilter} = require("filterClasses"); |
| 24 const {extractHostFromFrame, isThirdParty, stringifyURL} = require("url"); |
| 25 |
| 26 let {typeMap} = RegExpFilter; |
| 27 |
| 28 function injectScript(tabId, frameId) |
| 29 { |
| 30 try |
| 31 { |
| 32 browser.tabs.executeScript(tabId, { |
| 33 file: "/inject.preload.js", |
| 34 frameId, |
| 35 matchAboutBlank: true, |
| 36 runAt: "document_start" |
| 37 }); |
| 38 } |
| 39 catch (error) |
| 40 { |
| 41 } |
| 42 } |
| 43 |
| 44 browser.webNavigation.onCommitted.addListener(({tabId, frameId, url}) => |
| 45 { |
| 46 // There's a bug in Chrome that causes webNavigation.onCommitted to get |
| 47 // dispatched twice if there's a URL filter present, therefore we must listen |
| 48 // for all URLs and do an explicit check here. |
| 49 // https://crbug.com/827855 |
| 50 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(url)) |
| 51 return; |
| 52 |
| 53 let urlObject = new URL(url); |
| 54 let urlString = stringifyURL(urlObject); |
| 55 |
| 56 let frame = ext.getFrame(tabId, frameId); |
| 57 if (!frame) |
| 58 return; |
| 59 |
| 60 let hostname = extractHostFromFrame(frame); |
| 61 let thirdParty = isThirdParty(urlObject, hostname); |
| 62 |
| 63 let filter = defaultMatcher.matchesAny(urlString, typeMap.WEBRTC, hostname, |
| 64 thirdParty, null, false); |
| 65 if (!filter) |
| 66 return; |
| 67 |
| 68 // As long as there's a matching filter, inject the API wrappers. We could |
| 69 // check if the URL is whitelisted or if the filter is a whitelisting filter, |
| 70 // but it's not worth it (i.e. false positives are OK). |
| 71 injectScript(tabId, frameId); |
| 72 }); |
OLD | NEW |