From 50aff81e575d92fc3ac4a6194fffa7ea89344e2c Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sat, 22 Jun 2019 00:34:29 +0200 Subject: Move some commonly used functions out of vapi-background --- js/vapi-core.js | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 js/vapi-core.js (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js new file mode 100644 index 0000000..bc7bcb9 --- /dev/null +++ b/js/vapi-core.js @@ -0,0 +1,109 @@ +/******************************************************************************* + + ηMatrix - a browser extension to black/white list requests. + Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors + Copyright (C) 2019 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 + 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://gitlab.com/vannilla/ematrix + uMatrix Home: https://github.com/gorhill/uMatrix +*/ + +/* global self, Components */ + +// For background page (important functions) + +'use strict'; + +/******************************************************************************/ + +(function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + + let vAPI = self.vAPI = self.vAPI || {}; + + // List of things that needs to be destroyed when disabling the extension + // Only functions should be added to it + // eMatrix: taken care by vAPI.addCleanUpTask --- use that function + let cleanupTasks = []; + + // This must be updated manually, every time a new task is added/removed + // eMatrix: do we? + let expectedNumberOfCleanups = 7; + + vAPI.addCleanUpTask = function (task) { + if (typeof task !== 'function') { + return; + } + + cleanupTasks.push(task); + }; + + vAPI.deferUntil = function (testFn, mainFn, details) { + let dtls = (typeof details !== 'object') ? {} : details; + let now = 0; + let next = dtls.next || 200; + let until = dtls.until || 2000; + + let check = function () { + if (testFn() === true || now >= until) { + mainFn(); + return; + } + now += next; + vAPI.setTimeout(check, next); + }; + + if ('sync' in dtls && dtls.sync === true) { + check(); + } else { + vAPI.setTimeout(check, 1); + } + }; + + window.addEventListener('unload', function () { + // if (typeof vAPI.app.onShutdown === 'function') { + // vAPI.app.onShutdown(); + // } + + // IMPORTANT: cleanup tasks must be executed using LIFO order. + for (let i=cleanupTasks.length-1; i>=0; --i) { + try { + cleanupTasks[i](); + } catch (e) { + // Just in case a clean up task ends up throwing for + // no reason + console.error(e); + } + } + + // eMatrix: temporarily disabled + // if (cleanupTasks.length < expectedNumberOfCleanups) { + // console.error + // ('eMatrix> Cleanup tasks performed: %s (out of %s)', + // cleanupTasks.length, + // expectedNumberOfCleanups); + // } + + // frameModule needs to be cleared too + let frameModuleURL = vAPI.getURL('frameModule.js'); + let frameModule = {}; + + Cu.import(frameModuleURL, frameModule); + frameModule.contentObserver.unregister(); + Cu.unload(frameModuleURL); + }); +})(); -- cgit v1.2.3 From 5befab2b81270d55e5cb45ed39756dd9c069cf1f Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sat, 22 Jun 2019 00:43:52 +0200 Subject: Move more entities to core --- js/vapi-core.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index bc7bcb9..4ab5216 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -34,6 +34,27 @@ const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); let vAPI = self.vAPI = self.vAPI || {}; + + vAPI.modernFirefox = + Services.appinfo.ID === '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}' + && Services.vc.compare(Services.appinfo.version, '44') > 0; + + vAPI.app = { + name: 'eMatrix', + version: location.hash.slice(1), + + start: function () { + return; + }, + stop: function () { + return; + }, + restart: function () { + Cc['@mozilla.org/childprocessmessagemanager;1'] + .getService(Ci.nsIMessageSender) + .sendAsyncMessage(location.host + '-restart'); + }, + }; // List of things that needs to be destroyed when disabling the extension // Only functions should be added to it @@ -106,4 +127,14 @@ frameModule.contentObserver.unregister(); Cu.unload(frameModuleURL); }); + + vAPI.noTabId = '-1'; + + vAPI.isBehindTheSceneTabId = function (tabId) { + return tabId.toString() === '-1'; + }; + + vAPI.lastError = function () { + return null; + }; })(); -- cgit v1.2.3 From 194b9f768b7e8ea57217fa6cf7b501727e65b662 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 13:36:44 +0200 Subject: Remove some comments While they are technically informative, the splitting makes things easier to follow already (somewhat) and there's not really a need to list each global variable (there aren't many anyway.) --- js/vapi-core.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index 4ab5216..419955b 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -21,10 +21,6 @@ uMatrix Home: https://github.com/gorhill/uMatrix */ -/* global self, Components */ - -// For background page (important functions) - 'use strict'; /******************************************************************************/ -- cgit v1.2.3 From 9d87f8f864b28d182af9659a3095433adb4b0126 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 17:33:06 +0200 Subject: Change how modules are imported I can't really find a reason why the returned value is preferred over the normal importing process. Additionally, there's a good chance importing Services.jsm can be done only once at the start of everything, instead of binding each object to a separate closure. --- js/vapi-core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index 419955b..c904ff2 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -27,7 +27,7 @@ (function () { const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + Cu.import('resource://gre/modules/Services.jsm'); let vAPI = self.vAPI = self.vAPI || {}; -- cgit v1.2.3 From 51f5e899fff9e804d9c91e4fefdd57ea5a85e99c Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:06:54 +0200 Subject: Make components and services global Given that they are used a lot, at least in vAPI, let's just define/import them only once. --- js/vapi-core.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index c904ff2..3901c1c 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -23,12 +23,12 @@ 'use strict'; +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; +Cu.import('resource://gre/modules/Services.jsm'); + /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); - let vAPI = self.vAPI = self.vAPI || {}; vAPI.modernFirefox = -- cgit v1.2.3 From f1f66637b814155c95a2bfddfdd9cd2973b86659 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:20:08 +0200 Subject: Revert "Make components and services global" This reverts commit 51f5e899fff9e804d9c91e4fefdd57ea5a85e99c. It seems to cause issues with the popup menu. --- js/vapi-core.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index 3901c1c..c904ff2 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -23,12 +23,12 @@ 'use strict'; -const {classes: Cc, interfaces: Ci, utils: Cu} = Components; -Cu.import('resource://gre/modules/Services.jsm'); - /******************************************************************************/ (function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + Cu.import('resource://gre/modules/Services.jsm'); + let vAPI = self.vAPI = self.vAPI || {}; vAPI.modernFirefox = -- cgit v1.2.3 From 6908a6c89f9f44380faa1831ec13cff4042b671a Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:41:18 +0200 Subject: Make vAPI definitely global At least for background.html, it can be defined once at the start of vapi-core and then populated. --- js/vapi-core.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index c904ff2..e2afa4f 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -23,14 +23,14 @@ 'use strict'; +var vAPI = {}; + /******************************************************************************/ -(function () { +(function (self) { const {classes: Cc, interfaces: Ci, utils: Cu} = Components; Cu.import('resource://gre/modules/Services.jsm'); - let vAPI = self.vAPI = self.vAPI || {}; - vAPI.modernFirefox = Services.appinfo.ID === '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}' && Services.vc.compare(Services.appinfo.version, '44') > 0; @@ -133,4 +133,4 @@ vAPI.lastError = function () { return null; }; -})(); +})(this); -- cgit v1.2.3 From 8c05a1711951f3cc9179eec2c086b0e88d42e5f0 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Fri, 19 Jul 2019 15:54:34 +0200 Subject: Make vAPI definitely global for non-background pages too --- js/vapi-core.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index e2afa4f..620faf2 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -23,8 +23,6 @@ 'use strict'; -var vAPI = {}; - /******************************************************************************/ (function (self) { -- cgit v1.2.3 From acd097e4733c106a15815c90e21c00d0c545e042 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Fri, 19 Jul 2019 16:00:08 +0200 Subject: Make components and Services.jsm global Once again, but this time it works. --- js/vapi-core.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'js/vapi-core.js') diff --git a/js/vapi-core.js b/js/vapi-core.js index 620faf2..0c907f7 100644 --- a/js/vapi-core.js +++ b/js/vapi-core.js @@ -26,9 +26,6 @@ /******************************************************************************/ (function (self) { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); - vAPI.modernFirefox = Services.appinfo.ID === '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}' && Services.vc.compare(Services.appinfo.version, '44') > 0; -- cgit v1.2.3