diff options
Diffstat (limited to 'js/vapi-client.js')
-rw-r--r-- | js/vapi-client.js | 170 |
1 files changed, 85 insertions, 85 deletions
diff --git a/js/vapi-client.js b/js/vapi-client.js index 40598b8..4635853 100644 --- a/js/vapi-client.js +++ b/js/vapi-client.js @@ -2,7 +2,7 @@ ηMatrix - a browser extension to black/white list requests. Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors - Copyright (C) 2019-2020 Alessio Vanni + Copyright (C) 2019-2022 Alessio Vanni 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 @@ -17,7 +17,7 @@ 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://libregit.spks.xyz/heckyel/ematrix + Home: https://gitlab.com/vannilla/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ @@ -27,66 +27,66 @@ (function (self) { if (self.vAPI === undefined) { - self.vAPI = {}; + self.vAPI = {}; } let vAPI = self.vAPI; vAPI.setTimeout = vAPI.setTimeout || function (callback, delay, extra) { - return setTimeout(function (a) { - callback(a); - }, delay, extra); + return setTimeout(function (a) { + callback(a); + }, delay, extra); }; vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) + - Math.random().toString(36).slice(2); + Math.random().toString(36).slice(2); vAPI.shutdown = (function () { - let jobs = []; + let jobs = []; - let add = function (job) { + let add = function (job) { jobs.push(job); - }; + }; - let exec = function () { + let exec = function () { //console.debug('Shutting down...'); let job; while ((job = jobs.pop())) { - job(); + job(); } - }; + }; - return { + return { add: add, exec: exec - }; + }; })(); vAPI.messaging = { - listeners: new Set(), - pending: new Map(), - requestId: 1, - connected: false, - messageListenerCallback: null, - toggleListenerCallback: null, - - start: function () { + listeners: new Set(), + pending: new Map(), + requestId: 1, + connected: false, + messageListenerCallback: null, + toggleListenerCallback: null, + + start: function () { this.addListener(this.builtinListener); if (this.toggleListenerCallback === null) { - this.toggleListenerCallback = this.toggleListener.bind(this); + this.toggleListenerCallback = this.toggleListener.bind(this); } window.addEventListener('pagehide', - this.toggleListenerCallback, true); + this.toggleListenerCallback, true); window.addEventListener('pageshow', - this.toggleListenerCallback, true); - }, - shutdown: function () { + this.toggleListenerCallback, true); + }, + shutdown: function () { if (this.toggleListenerCallback !== null) { - window.removeEventListener('pagehide', - this.toggleListenerCallback, true); - window.removeEventListener('pageshow', - this.toggleListenerCallback, true); + window.removeEventListener('pagehide', + this.toggleListenerCallback, true); + window.removeEventListener('pageshow', + this.toggleListenerCallback, true); } this.removeAllListeners(); @@ -94,100 +94,100 @@ var pending = this.pending; this.pending.clear(); for (let callback of pending.values()) { - if (typeof callback === 'function') { + if (typeof callback === 'function') { callback(null); - } + } } - }, - connect: function () { + }, + connect: function () { if (!this.connected) { - if (this.messageListenerCallback === null) { + if (this.messageListenerCallback === null) { this.messageListenerCallback = - this.messageListener.bind(this); - } - addMessageListener(this.messageListenerCallback); - this.connected = true; + this.messageListener.bind(this); + } + addMessageListener(this.messageListenerCallback); + this.connected = true; } - }, - disconnect: function () { + }, + disconnect: function () { if (this.connected) { - removeMessageListener(); - this.connected = false; + removeMessageListener(); + this.connected = false; } - }, - messageListener: function (msg) { + }, + messageListener: function (msg) { let details = JSON.parse(msg); if (!details) { - return; + return; } if (details.broadcast) { - this.sendToListeners(details.msg); - return; + this.sendToListeners(details.msg); + return; } if (details.requestId) { - let listener = this.pending.get(details.requestId); - if (listener !== undefined) { + let listener = this.pending.get(details.requestId); + if (listener !== undefined) { this.pending.delete(details.requestId); listener(details.msg); return; - } + } } - }, - builtinListener: function (msg) { + }, + builtinListener: function (msg) { if (typeof msg.cmd === 'string' && msg.cmd === 'injectScript') { - let details = msg.details; - if (!details.allFrames && window !== window.top) { + let details = msg.details; + if (!details.allFrames && window !== window.top) { return; - } - self.injectScript(details.file); + } + self.injectScript(details.file); } - }, - send: function (channelName, message, callback) { + }, + send: function (channelName, message, callback) { this.connect() message = { - channelName: self._sandboxId_ + '|' + channelName, - msg: message + channelName: self._sandboxId_ + '|' + channelName, + msg: message }; if (callback) { - message.requestId = this.requestId++; - this.pending.set(message.requestId, callback); + message.requestId = this.requestId++; + this.pending.set(message.requestId, callback); } sendAsyncMessage('ematrix:background', message); - }, - toggleListener: function ({type, persisted}) { + }, + toggleListener: function ({type, persisted}) { if (type === 'pagehide' && !persisted) { - vAPI.shutdown.exec(); - this.shutdown(); - return; + vAPI.shutdown.exec(); + this.shutdown(); + return; } if (type === 'pagehide') { - this.disconnect(); + this.disconnect(); } else { - this.connect(); + this.connect(); } - }, - sendToListeners: function (msg) { + }, + sendToListeners: function (msg) { for (let listener of this.listeners) { - listener(msg); + listener(msg); } - }, - addListener: function (listener) { + }, + addListener: function (listener) { this.listeners.add(listener); this.connect() - }, - removeListener: function (listener) { + }, + removeListener: function (listener) { this.listeners.delete(listener); - }, - removeAllListeners: function () { + }, + removeAllListeners: function () { this.disconnect(); this.listeners.clear(); - } + } }; vAPI.messaging.start() @@ -197,8 +197,8 @@ // be injected in top window). // Needs more investigating // if ( window !== window.top ) { - // vAPI.shutdown.add(function() { - // vAPI = null; - // }); + // vAPI.shutdown.add(function() { + // vAPI = null; + // }); // } })(this); |