vapi-client.js (7978B) - View raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248/******************************************************************************* uBlock Origin - a comprehensive, efficient content blocker Copyright (C) 2014-2015 The uBlock Origin authors Copyright (C) 2014-present Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 this program. If not, see {http://www.gnu.org/licenses/}. Home: https://github.com/gorhill/uBlock */ // For non-background page /******************************************************************************/ // https://github.com/chrisaljoudi/uBlock/issues/456 // Skip if already injected. // >>>>>>>> start of HUGE-IF-BLOCK if ( typeof vAPI === 'object' && vAPI.randomToken instanceof Function === false ) { /******************************************************************************/ /******************************************************************************/ vAPI.randomToken = function() { const n = Math.random(); return String.fromCharCode(n * 25 + 97) + Math.floor( (0.25 + n * 0.75) * Number.MAX_SAFE_INTEGER ).toString(36).slice(-8); }; vAPI.sessionId = vAPI.randomToken(); vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self); /******************************************************************************/ vAPI.shutdown = { jobs: [], add: function(job) { this.jobs.push(job); }, exec: function() { // Shutdown asynchronously, to ensure shutdown jobs are called from // the top context. self.requestIdleCallback(( ) => { const jobs = this.jobs.slice(); this.jobs.length = 0; while ( jobs.length !== 0 ) { (jobs.pop())(); } }); }, remove: function(job) { let pos; while ( (pos = this.jobs.indexOf(job)) !== -1 ) { this.jobs.splice(pos, 1); } } }; /******************************************************************************/ vAPI.messaging = { port: null, portTimer: null, portTimerDelay: 10000, msgIdGenerator: 1, pending: new Map(), waitStartTime: 0, shuttingDown: false, shutdown: function() { this.shuttingDown = true; this.destroyPort(); }, // https://github.com/uBlockOrigin/uBlock-issues/issues/403 // Spurious disconnection can happen, so do not consider such events // as world-ending, i.e. stay around. Except for embedded frames. disconnectListener: function() { void browser.runtime.lastError; this.port = null; if ( window !== window.top ) { vAPI.shutdown.exec(); } else { this.destroyPort(); } }, disconnectListenerBound: null, // 2020-09-01: // In Firefox, `details instanceof Object` resolves to `false` despite // `details` being a valid object. Consequently, falling back to use // `typeof details`. // This is an issue which surfaced when the element picker code was // revisited to isolate the picker dialog DOM from the page DOM. messageListener: function(details) { if ( typeof details !== 'object' || details === null ) { return; } if ( details.msgId === undefined ) { return; } const resolver = this.pending.get(details.msgId); if ( resolver === undefined ) { return; } this.pending.delete(details.msgId); resolver(details.msg); }, messageListenerBound: null, canDestroyPort: function() { return this.pending.size === 0; }, portPoller: function() { this.portTimer = null; if ( this.port !== null && this.canDestroyPort() ) { return this.destroyPort(); } this.portTimer = vAPI.setTimeout(this.portPollerBound, this.portTimerDelay); this.portTimerDelay = Math.min(this.portTimerDelay * 2, 60 * 60 * 1000); }, portPollerBound: null, destroyPort: function() { if ( this.portTimer !== null ) { clearTimeout(this.portTimer); this.portTimer = null; } const port = this.port; if ( port !== null ) { port.disconnect(); port.onMessage.removeListener(this.messageListenerBound); port.onDisconnect.removeListener(this.disconnectListenerBound); this.port = null; } // service pending callbacks if ( this.pending.size !== 0 ) { const pending = this.pending; this.pending = new Map(); for ( const resolver of pending.values() ) { resolver(); } } }, createPort: function() { if ( this.shuttingDown ) { return null; } if ( this.messageListenerBound === null ) { this.messageListenerBound = this.messageListener.bind(this); this.disconnectListenerBound = this.disconnectListener.bind(this); this.portPollerBound = this.portPoller.bind(this); } try { this.port = browser.runtime.connect({name: vAPI.sessionId}) || null; } catch { this.port = null; } // Not having a valid port at this point means the main process is // not available: no point keeping the content scripts alive. if ( this.port === null ) { vAPI.shutdown.exec(); return null; } this.port.onMessage.addListener(this.messageListenerBound); this.port.onDisconnect.addListener(this.disconnectListenerBound); this.portTimerDelay = 10000; if ( this.portTimer === null ) { this.portTimer = vAPI.setTimeout( this.portPollerBound, this.portTimerDelay ); } return this.port; }, getPort: function() { return this.port !== null ? this.port : this.createPort(); }, send: function(channel, msg) { // Too large a gap between the last request and the last response means // the main process is no longer reachable: memory leaks and bad // performance become a risk -- especially for long-lived, dynamic // pages. Guard against this. if ( this.pending.size > 64 ) { if ( (Date.now() - this.waitStartTime) > 60000 ) { vAPI.shutdown.exec(); } } const port = this.getPort(); if ( port === null ) { return Promise.resolve(); } if ( this.pending.size === 0 ) { this.waitStartTime = Date.now(); } const msgId = this.msgIdGenerator++; const promise = new Promise(resolve => { this.pending.set(msgId, resolve); }); port.postMessage({ channel, msgId, msg }); return promise; }, }; vAPI.shutdown.add(( ) => { vAPI.messaging.shutdown(); window.vAPI = undefined; }); /******************************************************************************/ /******************************************************************************/ } // <<<<<<<< end of HUGE-IF-BLOCK /******************************************************************************* DO NOT: - Remove the following code - Add code beyond the following code Reason: - https://github.com/gorhill/uBlock/pull/3721 - uBO never uses the return value from injected content scripts **/ void 0;