diff options
author | Alessio Vanni <vannilla@firemail.cc> | 2019-07-19 16:11:45 +0200 |
---|---|---|
committer | Alessio Vanni <vannilla@firemail.cc> | 2019-07-19 16:11:45 +0200 |
commit | 1ec6e70fc1df5f804f8ec254055fce3775123785 (patch) | |
tree | 03910671df22785ea18be4415eb6c14d8553d329 /js/vapi-common.js | |
parent | a835d88e7fb3b5f01614985958bc68dd7f9e78cc (diff) | |
parent | acd097e4733c106a15815c90e21c00d0c545e042 (diff) | |
download | ematrix-1ec6e70fc1df5f804f8ec254055fce3775123785.tar.lz ematrix-1ec6e70fc1df5f804f8ec254055fce3775123785.tar.xz ematrix-1ec6e70fc1df5f804f8ec254055fce3775123785.zip |
Merge branch 'rewrite-vapi'
Diffstat (limited to 'js/vapi-common.js')
-rw-r--r-- | js/vapi-common.js | 282 |
1 files changed, 127 insertions, 155 deletions
diff --git a/js/vapi-common.js b/js/vapi-common.js index 6946a9c..b7c5635 100644 --- a/js/vapi-common.js +++ b/js/vapi-common.js @@ -21,173 +21,145 @@ uMatrix Home: https://github.com/gorhill/uMatrix */ -/* global sendAsyncMessage */ - // For background page or non-background pages 'use strict'; -/******************************************************************************/ - -(function(self) { - -/******************************************************************************/ - -const {Services} = Components.utils.import( - 'resource://gre/modules/Services.jsm', - null -); - -// https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10 -if ( self.vAPI === undefined || self.vAPI.eMatrix !== true ) { - self.vAPI = { eMatrix: true }; -} - -var vAPI = self.vAPI; - -/******************************************************************************/ - -vAPI.setTimeout = vAPI.setTimeout || function(callback, delay, extra) { - return setTimeout(function(a) { callback(a); }, delay, extra); -}; - -/******************************************************************************/ - -// http://www.w3.org/International/questions/qa-scripts#directions - -var setScriptDirection = function(language) { - document.body.setAttribute( - 'dir', - ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 ? 'rtl' : 'ltr' - ); -}; +let vAPI = {}; -/******************************************************************************/ +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; +Cu.import('resource://gre/modules/Services.jsm'); -vAPI.download = function(details) { - if ( !details.url ) { - return; - } - - var a = document.createElement('a'); - a.href = details.url; - a.setAttribute('download', details.filename || ''); - a.dispatchEvent(new MouseEvent('click')); -}; - -/******************************************************************************/ - -vAPI.insertHTML = (function() { - const parser = Components.classes['@mozilla.org/parserutils;1'] - .getService(Components.interfaces.nsIParserUtils); - - // https://github.com/gorhill/uBlock/issues/845 - // Apparently dashboard pages execute with `about:blank` principal. - - return function(node, html) { - while ( node.firstChild ) { - node.removeChild(node.firstChild); - } - - node.appendChild(parser.parseFragment( - html, - parser.SanitizerAllowStyle, - false, - Services.io.newURI('about:blank', null, null), - document.documentElement - )); +(function (self) { + vAPI.setTimeout = vAPI.setTimeout || function (callback, delay, extra) { + return setTimeout(function (a) { + callback(a); + }, delay, extra); }; -})(); -/******************************************************************************/ + // http://www.w3.org/International/questions/qa-scripts#directions + let setScriptDirection = function(language) { + let dir = + ['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 + ? 'rtl' + : 'ltr'; + + document.body.setAttribute('dir', dir); + }; -vAPI.getURL = function(path) { - return 'chrome://' + location.host + '/content/' + path.replace(/^\/+/, ''); -}; + vAPI.download = function (details) { + if (!details.url) { + return; + } -/******************************************************************************/ + let a = document.createElement('a'); + a.href = details.url; + a.setAttribute('download', details.filename || ''); + a.dispatchEvent(new MouseEvent('click')); + }; -vAPI.i18n = (function() { - var stringBundle = Services.strings.createBundle( - 'chrome://' + location.host + '/locale/messages.properties' - ); + vAPI.insertHTML = (function () { + const parser = Cc['@mozilla.org/parserutils;1'] + .getService(Ci.nsIParserUtils); + + // https://github.com/gorhill/uBlock/issues/845 + // Apparently dashboard pages execute with `about:blank` principal. + + return function (node, html) { + while (node.firstChild) { + node.removeChild(node.firstChild); + } + + let parsed = + parser.parseFragment(html, + parser.SanitizerAllowStyle, + false, + Services.io.newURI('about:blank', + null, null), + document.documentElement); + + node.appendChild(parsed); + }; + })(); + + vAPI.getURL = function (path) { + return 'chrome://' + + location.host + + '/content/' + + path.replace(/^\/+/, ''); + }; + + vAPI.i18n = (function () { + let stringBundle = + Services.strings.createBundle('chrome://' + + location.host + + '/locale/messages.properties'); + + return function (s) { + try { + return stringBundle.GetStringFromName(s); + } catch (ex) { + return ''; + } + }; + })(); + + setScriptDirection(navigator.language); + + vAPI.closePopup = function() { + sendAsyncMessage(location.host + ':closePopup'); + }; - return function(s) { - try { - return stringBundle.GetStringFromName(s); - } catch (ex) { - return ''; - } + // A localStorage-like object which should be accessible from the + // background page or auxiliary pages. + // This storage is optional, but it is nice to have, for a more polished user + // experience. + vAPI.localStorage = { + pbName: '', + pb: null, + str: Cc['@mozilla.org/supports-string;1'] + .createInstance(Ci.nsISupportsString), + + init: function (pbName) { + this.pbName = pbName; + this.pb = Services.prefs.getBranch(pbName); + }, + getItem: function (key) { + try { + return this.pb + .getComplexValue(key, + Ci.nsISupportsString).data; + } catch (ex) { + return null; + } + }, + setItem: function (key, value) { + this.str.data = value; + this.pb.setComplexValue(key, + Ci.nsISupportsString, + this.str); + }, + getBool: function (key) { + try { + return this.pb.getBoolPref(key); + } catch (ex) { + return null; + } + }, + setBool: function (key, value) { + this.pb.setBoolPref(key, value); + }, + setDefaultBool: function (key, defaultValue) { + Services.prefs.getDefaultBranch(this.pbName) + .setBoolPref(key, defaultValue); + }, + removeItem: function (key) { + this.pb.clearUserPref(key); + }, + clear: function () { + this.pb.deleteBranch(''); + } }; -})(); - -setScriptDirection(navigator.language); - -/******************************************************************************/ - -vAPI.closePopup = function() { - sendAsyncMessage(location.host + ':closePopup'); -}; - -/******************************************************************************/ - -// A localStorage-like object which should be accessible from the -// background page or auxiliary pages. -// This storage is optional, but it is nice to have, for a more polished user -// experience. - -vAPI.localStorage = { - pbName: '', - pb: null, - str: Components.classes['@mozilla.org/supports-string;1'] - .createInstance(Components.interfaces.nsISupportsString), - init: function(pbName) { - this.pbName = pbName; - this.pb = Services.prefs.getBranch(pbName); - }, - getItem: function(key) { - try { - return this.pb.getComplexValue( - key, - Components.interfaces.nsISupportsString - ).data; - } catch (ex) { - return null; - } - }, - setItem: function(key, value) { - this.str.data = value; - this.pb.setComplexValue( - key, - Components.interfaces.nsISupportsString, - this.str - ); - }, - getBool: function(key) { - try { - return this.pb.getBoolPref(key); - } catch (ex) { - return null; - } - }, - setBool: function(key, value) { - this.pb.setBoolPref(key, value); - }, - setDefaultBool: function(key, defaultValue) { - Services.prefs.getDefaultBranch(this.pbName).setBoolPref(key, defaultValue); - }, - removeItem: function(key) { - this.pb.clearUserPref(key); - }, - clear: function() { - this.pb.deleteBranch(''); - } -}; - -vAPI.localStorage.init('extensions.' + location.host + '.'); - -/******************************************************************************/ + vAPI.localStorage.init('extensions.' + location.host + '.'); })(this); - -/******************************************************************************/ |