diff options
author | Alessio Vanni <vannilla@firemail.cc> | 2019-07-05 13:42:18 +0200 |
---|---|---|
committer | Alessio Vanni <vannilla@firemail.cc> | 2019-07-05 13:42:18 +0200 |
commit | e1c3d731eb012a802ac94b849f7764aba4108366 (patch) | |
tree | c06674e0975a0bfc25f38cd643eb2bd610de842a /js | |
parent | 4c1c37a54318d91d572180b45bda9db6d9de0389 (diff) | |
download | ematrix-e1c3d731eb012a802ac94b849f7764aba4108366.tar.lz ematrix-e1c3d731eb012a802ac94b849f7764aba4108366.tar.xz ematrix-e1c3d731eb012a802ac94b849f7764aba4108366.zip |
Style changes part 2
Diffstat (limited to 'js')
-rw-r--r-- | js/vapi-client.js | 351 |
1 files changed, 164 insertions, 187 deletions
diff --git a/js/vapi-client.js b/js/vapi-client.js index 337762d..8ef7805 100644 --- a/js/vapi-client.js +++ b/js/vapi-client.js @@ -21,210 +21,187 @@ uMatrix Home: https://github.com/gorhill/uMatrix */ -/* jshint esnext: true */ -/* global addMessageListener, removeMessageListener, sendAsyncMessage */ - // For non background pages 'use strict'; -/******************************************************************************/ - -(function(self) { - -/******************************************************************************/ - -// https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10 -// eMatrix: does this apply to us? -// In the meantime, let's remove the useless `eMatrix' property from vAPI -if (self.vAPI === undefined) { - self.vAPI = {}; -} - -var vAPI = self.vAPI; // This is also used when vapi-core is not available - -vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) + - Math.random().toString(36).slice(2); - -/******************************************************************************/ - -vAPI.setTimeout = vAPI.setTimeout || function(callback, delay) { - return setTimeout(function() { callback(); }, delay); -}; +(function (self) { + // https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10 + // eMatrix: does this apply to us? + // In the meantime, let's remove the useless `eMatrix' property from vAPI + if (self.vAPI === undefined) { + self.vAPI = {}; + } -/******************************************************************************/ + let vAPI = self.vAPI; // This is also used when vapi-core is not available -vAPI.shutdown = (function() { - var jobs = []; + vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) + + Math.random().toString(36).slice(2); - var add = function(job) { - jobs.push(job); + vAPI.setTimeout = vAPI.setTimeout || function (callback, delay) { + return setTimeout(function () { + callback(); + }, delay); }; - var exec = function() { - //console.debug('Shutting down...'); - var job; - while ( (job = jobs.pop()) ) { - job(); - } - }; + vAPI.shutdown = (function () { + var jobs = []; - return { - add: add, - exec: exec - }; -})(); - -/******************************************************************************/ - -vAPI.messaging = { - listeners: new Set(), - pending: new Map(), - requestId: 1, - connected: false, - - start: function() { - this.addListener(this.builtinListener); - if ( this.toggleListenerCallback === null ) { - this.toggleListenerCallback = this.toggleListener.bind(this); - } - window.addEventListener('pagehide', this.toggleListenerCallback, true); - window.addEventListener('pageshow', this.toggleListenerCallback, true); - }, - - shutdown: function() { - if ( this.toggleListenerCallback !== null ) { - window.removeEventListener('pagehide', this.toggleListenerCallback, true); - window.removeEventListener('pageshow', this.toggleListenerCallback, true); - } - this.removeAllListeners(); - //service pending callbacks - var pending = this.pending; - this.pending.clear(); - for ( var callback of pending.values() ) { - if ( typeof callback === 'function' ) { - callback(null); - } - } - }, + var add = function (job) { + jobs.push(job); + }; - connect: function() { - if ( !this.connected ) { - if ( this.messageListenerCallback === null ) { - this.messageListenerCallback = this.messageListener.bind(this); + var exec = function () { + //console.debug('Shutting down...'); + var job; + while ((job = jobs.pop())) { + job(); } - addMessageListener(this.messageListenerCallback); - this.connected = true; - } - }, - - disconnect: function() { - if ( this.connected ) { - removeMessageListener(); - this.connected = false; - } - }, - - messageListener: function(msg) { - var details = JSON.parse(msg); - if ( !details ) { - return; - } - - if ( details.broadcast ) { - this.sendToListeners(details.msg); - return; - } - - if ( details.requestId ) { - var listener = this.pending.get(details.requestId); - if ( listener !== undefined ) { - this.pending.delete(details.requestId); - listener(details.msg); - return; + }; + + return { + add: add, + exec: exec + }; + })(); + + vAPI.messaging = { + 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); } - } - }, - messageListenerCallback: null, - - builtinListener: function(msg) { - if ( typeof msg.cmd === 'string' && msg.cmd === 'injectScript' ) { - var details = msg.details; - if ( !details.allFrames && window !== window.top ) { - return; + + window.addEventListener('pagehide', + this.toggleListenerCallback, true); + window.addEventListener('pageshow', + this.toggleListenerCallback, true); + }, + shutdown: function () { + if (this.toggleListenerCallback !== null) { + window.removeEventListener('pagehide', + this.toggleListenerCallback, true); + window.removeEventListener('pageshow', + this.toggleListenerCallback, true); + } + this.removeAllListeners(); + + //service pending callbacks + var pending = this.pending; + this.pending.clear(); + for (let callback of pending.values()) { + if (typeof callback === 'function') { + callback(null); + } + } + }, + connect: function () { + if (!this.connected) { + if (this.messageListenerCallback === null) { + this.messageListenerCallback = + this.messageListener.bind(this); + } + addMessageListener(this.messageListenerCallback); + this.connected = true; + } + }, + disconnect: function () { + if (this.connected) { + removeMessageListener(); + this.connected = false; + } + }, + messageListener: function (msg) { + let details = JSON.parse(msg); + if (!details) { + return; } - self.injectScript(details.file); - } - }, - - send: function(channelName, message, callback) { - this.connect() - - message = { - channelName: self._sandboxId_ + '|' + channelName, - msg: message - }; - - if ( callback ) { - message.requestId = this.requestId++; - this.pending.set(message.requestId, callback); - } - - sendAsyncMessage('ematrix:background', message); - }, - - toggleListener: function({type, persisted}) { - if ( type === 'pagehide' && !persisted ) { - vAPI.shutdown.exec(); - this.shutdown(); - return; - } - - if ( type === 'pagehide' ) { - this.disconnect(); - } else /* if ( type === 'pageshow' ) */ { - this.connect(); - } - }, - toggleListenerCallback: null, - - sendToListeners: function(msg) { - for ( var listener of this.listeners ) { - listener(msg); - } - }, - - addListener: function(listener) { - this.listeners.add(listener); - this.connect() - }, - - removeListener: function(listener) { - this.listeners.delete(listener); - }, - - removeAllListeners: function() { - this.disconnect(); - this.listeners.clear();; - } -}; -vAPI.messaging.start() + if (details.broadcast) { + this.sendToListeners(details.msg); + return; + } -/******************************************************************************/ + if (details.requestId) { + let listener = this.pending.get(details.requestId); + if (listener !== undefined) { + this.pending.delete(details.requestId); + listener(details.msg); + return; + } + } + }, + builtinListener: function (msg) { + if (typeof msg.cmd === 'string' && msg.cmd === 'injectScript') { + let details = msg.details; + if (!details.allFrames && window !== window.top) { + return; + } + self.injectScript(details.file); + } + }, + send: function (channelName, message, callback) { + this.connect() + + message = { + channelName: self._sandboxId_ + '|' + channelName, + msg: message + }; + + if (callback) { + message.requestId = this.requestId++; + this.pending.set(message.requestId, callback); + } -// No need to have vAPI client linger around after shutdown if -// we are not a top window (because element picker can still -// be injected in top window). -// Needs more investigating -/*if ( window !== window.top ) { - vAPI.shutdown.add(function() { - vAPI = null; - }); -}*/ + sendAsyncMessage('ematrix:background', message); + }, + toggleListener: function ({type, persisted}) { + if (type === 'pagehide' && !persisted) { + vAPI.shutdown.exec(); + this.shutdown(); + return; + } -/******************************************************************************/ + if (type === 'pagehide') { + this.disconnect(); + } else { + this.connect(); + } + }, + sendToListeners: function (msg) { + for (let listener of this.listeners) { + listener(msg); + } + }, + addListener: function (listener) { + this.listeners.add(listener); + this.connect() + }, + removeListener: function (listener) { + this.listeners.delete(listener); + }, + removeAllListeners: function () { + this.disconnect(); + this.listeners.clear(); + } + }; + vAPI.messaging.start() + + // No need to have vAPI client linger around after shutdown if + // we are not a top window (because element picker can still + // be injected in top window). + // Needs more investigating + // if ( window !== window.top ) { + // vAPI.shutdown.add(function() { + // vAPI = null; + // }); + // } })(this); - -/******************************************************************************/ |