|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + uBlock Origin - a comprehensive, efficient content blocker |
| 4 | + Copyright (C) 2026-present Raymond Hill |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see {http://www.gnu.org/licenses/}. |
| 18 | +
|
| 19 | + Home: https://github.com/gorhill/uBlock |
| 20 | +*/ |
| 21 | + |
| 22 | +/* global browser */ |
| 23 | + |
| 24 | +/******************************************************************************/ |
| 25 | + |
| 26 | +function uBO_isolatedCode(mainCode) { |
| 27 | + if ( self.vapiScripting ) { return 0; } |
| 28 | + self.vapiScripting = true; |
| 29 | + |
| 30 | + (function isolatedCode(){})(); |
| 31 | + |
| 32 | + if ( mainCode ) { |
| 33 | + let script; |
| 34 | + try { |
| 35 | + const doc = document; |
| 36 | + script = doc.createElement('script'); |
| 37 | + script.appendChild(doc.createTextNode(mainCode)); |
| 38 | + (doc.head || doc.documentElement).appendChild(script); |
| 39 | + } catch { |
| 40 | + } |
| 41 | + if ( script ) { |
| 42 | + script.remove(); |
| 43 | + script.textContent = ''; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +function uBO_assembleIsolatedCode(details) { |
| 51 | + const { isolatedCode = '', mainCode = '' } = details; |
| 52 | + if ( Boolean(isolatedCode) === false ) { |
| 53 | + if ( Boolean(mainCode) === false ) { return; } |
| 54 | + } |
| 55 | + let code = [ '(function uBO_isolatedWorldWrapper() {' ]; |
| 56 | + if ( details.earlyBailoutCode ) { |
| 57 | + code.push(details.earlyBailoutCode); |
| 58 | + } |
| 59 | + code.push(`(${uBO_isolatedCode.toString()})(`, JSON.stringify(mainCode), `);`); |
| 60 | + code.push('})();'); |
| 61 | + code = code.join('\n'); |
| 62 | + const match = /\(function isolatedCode\(\)\{\}\)\(\);/.exec(code); |
| 63 | + return code.slice(0, match.index) + |
| 64 | + isolatedCode + |
| 65 | + code.slice(match.index + match[0].length); |
| 66 | +} |
| 67 | + |
| 68 | +/******************************************************************************/ |
| 69 | + |
| 70 | +let contentScriptCode; |
| 71 | + |
| 72 | +let onceFn = async ( ) => { |
| 73 | + if ( Boolean(contentScriptCode) === false ) { return; } |
| 74 | + const tabs = await vAPI.tabs.query({ url: '' }); |
| 75 | + for ( const tab of tabs ) { |
| 76 | + if ( tab.discarded === true ) { continue; } |
| 77 | + if ( tab.status === 'unloaded' ) { continue; } |
| 78 | + browser.tabs.executeScript(tab.id, { |
| 79 | + allFrames: true, |
| 80 | + matchAboutBlank: true, |
| 81 | + runAt: 'document_start', |
| 82 | + code: contentScriptCode, |
| 83 | + }).catch(( ) => { |
| 84 | + }); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +export function registerContentScripts(details) { |
| 89 | + contentScriptCode = uBO_assembleIsolatedCode(details); |
| 90 | + if ( onceFn === undefined ) { return; } |
| 91 | + onceFn(); |
| 92 | + onceFn = undefined; |
| 93 | +} |
| 94 | + |
| 95 | +export function unregisterContentScripts() { |
| 96 | + contentScriptCode = undefined; |
| 97 | +} |
| 98 | + |
| 99 | +/******************************************************************************/ |
| 100 | + |
| 101 | +browser.webNavigation.onCommitted.addListener(details => { |
| 102 | + if ( /^https?:|^about:/.test(details.url) === false ) { return; } |
| 103 | + if ( Boolean(contentScriptCode) === false ) { return; } |
| 104 | + browser.tabs.executeScript(details.tabId, { |
| 105 | + frameId: details.frameId, |
| 106 | + matchAboutBlank: true, |
| 107 | + runAt: 'document_start', |
| 108 | + code: contentScriptCode, |
| 109 | + }).catch(( ) => { |
| 110 | + }); |
| 111 | +}); |
0 commit comments