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 "use strict"; |
| 19 |
| 20 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); |
| 21 const {Snippets} = require("../adblockpluscore/lib/snippets"); |
| 22 const {verifySignature} = require("../adblockpluscore/lib/rsa"); |
| 23 const {extractHostFromFrame} = require("./url"); |
| 24 const {checkWhitelisted} = require("./whitelisting"); |
| 25 const info = require("../buildtools/info"); |
| 26 |
| 27 const {typeMap} = RegExpFilter; |
| 28 const {getScriptsForDomain, compileScript} = Snippets; |
| 29 |
| 30 const remoteURL = "https://easylist-downloads.adblockplus.org/snippets.js"; |
| 31 |
| 32 let libraries = {local: "", remote: ""}; |
| 33 let executableCode = new Map(); |
| 34 |
| 35 function fetchText(url) |
| 36 { |
| 37 return fetch(url, {cache: "no-cache"}).then( |
| 38 response => response.ok ? response.text() : "" |
| 39 ); |
| 40 } |
| 41 |
| 42 function updateLibrary(name, text) |
| 43 { |
| 44 libraries[name] = text; |
| 45 |
| 46 executableCode.clear(); |
| 47 } |
| 48 |
| 49 function checkLibrarySignature(url, text) |
| 50 { |
| 51 return fetchText(`${url}.sig`).then( |
| 52 signature => fetchText(browser.extension.getURL("/key")).then( |
| 53 key => verifySignature(key.replace(/=/g, ""), signature, text) |
| 54 ) |
| 55 ); |
| 56 } |
| 57 |
| 58 function loadLibrary(name, url, {verify = true} = {}) |
| 59 { |
| 60 fetchText(url).then(text => |
| 61 { |
| 62 if (text != libraries[name]) |
| 63 { |
| 64 let check = verify ? checkLibrarySignature(url, text) : |
| 65 Promise.resolve(true); |
| 66 check.then(ok => |
| 67 { |
| 68 if (ok) |
| 69 updateLibrary(name, text); |
| 70 }); |
| 71 } |
| 72 }); |
| 73 } |
| 74 |
| 75 function loadLocalLibrary() |
| 76 { |
| 77 loadLibrary("local", browser.extension.getURL("/snippets.js"), |
| 78 {verify: false}); |
| 79 } |
| 80 |
| 81 function loadRemoteLibrary() |
| 82 { |
| 83 loadLibrary("remote", remoteURL); |
| 84 } |
| 85 |
| 86 function getExecutableCode(script) |
| 87 { |
| 88 let code = executableCode.get(script); |
| 89 if (code) |
| 90 return code; |
| 91 |
| 92 code = compileScript(script, [libraries.local, libraries.remote]); |
| 93 |
| 94 executableCode.set(script, code); |
| 95 return code; |
| 96 } |
| 97 |
| 98 function injectCode(script, tabId, frameId) |
| 99 { |
| 100 try |
| 101 { |
| 102 browser.tabs.executeScript(tabId, { |
| 103 code: getExecutableCode(script), |
| 104 frameId, |
| 105 matchAboutBlank: true, |
| 106 runAt: "document_start" |
| 107 }); |
| 108 } |
| 109 catch (error) |
| 110 { |
| 111 } |
| 112 } |
| 113 |
| 114 loadLocalLibrary(); |
| 115 |
| 116 // Only Chrome supports dynamic loading of JS. |
| 117 if (info.platform == "chromium") |
| 118 { |
| 119 loadRemoteLibrary(); |
| 120 |
| 121 // Download every 24 hours. |
| 122 setInterval(loadRemoteLibrary, 24 * 60 * 60 * 1000); |
| 123 } |
| 124 |
| 125 browser.webNavigation.onCommitted.addListener(({tabId, frameId, url}) => |
| 126 { |
| 127 // There's a bug in Chrome that causes webNavigation.onCommitted to get |
| 128 // dispatched twice if there's a URL filter present, therefore we must listen |
| 129 // for all URLs and do an explicit check here. |
| 130 // https://crbug.com/827855 |
| 131 if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(url)) |
| 132 return; |
| 133 |
| 134 let page = new ext.Page({id: tabId, url}); |
| 135 let frame = ext.getFrame(tabId, frameId); |
| 136 |
| 137 if (checkWhitelisted(page, frame, null, typeMap.DOCUMENT | typeMap.ELEMHIDE)) |
| 138 return; |
| 139 |
| 140 let hostname = extractHostFromFrame(frame); |
| 141 |
| 142 for (let {code} of getScriptsForDomain(hostname)) |
| 143 injectCode(code, tabId, frameId); |
| 144 }); |
OLD | NEW |