From b769343d0511aa444a6bf9fd601f0f97dea27cab Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Mon, 24 Aug 2020 14:34:54 +0200 Subject: Rewrite messaging.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before, it was a series of switch statements that did stuff based on the content of a field of the request object; the new implementation does dynamic dispatching of "methods" (just some functions in a table) instead. Signed-off-by: Jesús --- js/messaging.js | 1540 ++++++++++++++++++++++++++----------------------------- 1 file changed, 738 insertions(+), 802 deletions(-) diff --git a/js/messaging.js b/js/messaging.js index 90c5c5f..63e1c92 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -17,849 +17,785 @@ 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://notabug.org/heckyel/ematrix uMatrix Home: https://github.com/gorhill/uMatrix */ 'use strict'; -// Default handler (function () { + Cu.import('chrome://ematrix/content/lib/UriTools.jsm'); Cu.import('chrome://ematrix/content/lib/Tools.jsm'); + Cu.import('chrome://ematrix/content/lib/RowSnapshot.jsm'); let ηm = ηMatrix; - // Default is for commonly used message. - function onMessage(request, sender, callback) { - // Async - switch (request.what) { - case 'getAssetContent': - ηm.assets.get(request.url, { - dontCache: true, - }, callback); - return; - case 'selectHostsFiles': - ηm.selectHostsFiles(request, callback); - return; - default: - break; - } - - // Sync - let response; - - switch (request.what) { - case 'forceReloadTab': - ηm.forceReload(request.tabId, request.bypassCache); - break; - case 'forceUpdateAssets': - ηm.scheduleAssetUpdater(0); - ηm.assets.updateStart({ - delay: 2000, - }); - break; - case 'getUserSettings': - response = { - userSettings: ηm.userSettings, - matrixSwitches: { - 'https-strict': ηm.pMatrix.evaluateSwitch('https-strict', '*') === 1, - 'referrer-spoof': ηm.pMatrix.evaluateSwitch('referrer-spoof', '*') === 1, - 'noscript-spoof': ηm.pMatrix.evaluateSwitch('noscript-spoof', '*') === 1, - }, - }; - break; - case 'gotoExtensionURL': - Tools.gotoExtensionURL({ - api: vAPI, - details: request, - matrix: ηm, - }); - break; - case 'gotoURL': - Tools.gotoURL({ - api: vAPI, - details: request, - }); - break; - case 'mustBlock': - response = ηm.mustBlock(request.scope, - request.hostname,request.type); - break; - case 'readRawSettings': - response = ηm.stringFromRawSettings(); - break; - case 'reloadHostsFiles': - ηm.reloadHostsFiles(); - break; - case 'setMatrixSwitch': - ηm.tMatrix.setSwitch(request.switchName, '*', request.state); - if (ηm.pMatrix.setSwitch(request.switchName, '*', request.state)) { - ηm.saveMatrix(); - } - break; - case 'userSettings': - if (request.hasOwnProperty('value') === false) { - request.value = undefined; - } - response = ηm.changeUserSettings(request.name, request.value); - break; - case 'writeRawSettings': - ηm.rawSettingsFromString(request.content); - break; - default: - return vAPI.messaging.UNHANDLED; - } - callback(response); - } - - vAPI.messaging.setup(onMessage); -})(); - -(function () { - // popup.js - - let ηm = ηMatrix; - - // Constructor is faster than object literal - // ηMatrix: really? I'll take your word for it... - let RowSnapshot = function(srcHostname, desHostname, desDomain) { - this.domain = desDomain; - this.temporary = ηm.tMatrix.evaluateRowZXY(srcHostname, desHostname); - this.permanent = ηm.pMatrix.evaluateRowZXY(srcHostname, desHostname); - this.counts = RowSnapshot.counts.slice(); - this.totals = RowSnapshot.counts.slice(); - }; - - RowSnapshot.counts = (function () { - let aa = []; - let n = ηm.Matrix.columnHeaderIndices.size - - for (let i=0; i