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 /* eslint-env webextensions */ |
18 /* eslint no-console: "off" */ | 19 /* eslint no-console: "off" */ |
19 | 20 |
20 "use strict"; | 21 "use strict"; |
21 | 22 |
| 23 function injectCode(code, dependencies = []) |
| 24 { |
| 25 for (let dependency of dependencies) |
| 26 code += dependency; |
| 27 |
| 28 let script = document.createElement("script"); |
| 29 |
| 30 script.type = "application/javascript"; |
| 31 script.async = false; |
| 32 |
| 33 // Firefox 58 only bypasses site CSPs when assigning to 'src', |
| 34 // while Chrome 67 only bypasses site CSPs when using 'textContent'. |
| 35 if (browser.runtime.getURL("").startsWith("chrome-extension://")) |
| 36 { |
| 37 script.textContent = code; |
| 38 document.documentElement.appendChild(script); |
| 39 } |
| 40 else |
| 41 { |
| 42 let url = URL.createObjectURL(new Blob([code])); |
| 43 script.src = url; |
| 44 document.documentElement.appendChild(script); |
| 45 URL.revokeObjectURL(url); |
| 46 } |
| 47 |
| 48 document.documentElement.removeChild(script); |
| 49 } |
| 50 |
| 51 function stringifyFunctionCall(func, ...params) |
| 52 { |
| 53 return `"use strict";(${func})(${params.map(JSON.stringify).join(",")});`; |
| 54 } |
| 55 |
| 56 function makeInjector(injectable, ...dependencies) |
| 57 { |
| 58 return (...args) => injectCode(stringifyFunctionCall(injectable, ...args), |
| 59 dependencies); |
| 60 } |
| 61 |
22 function log(...args) | 62 function log(...args) |
23 { | 63 { |
24 console.log(...args); | 64 console.log(...args); |
25 } | 65 } |
26 | 66 |
27 exports.log = log; | 67 exports.log = log; |
OLD | NEW |