diff options
author | Jesús <heckyel@hyperbola.info> | 2019-08-11 19:58:26 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-08-11 19:58:26 -0500 |
commit | 9ec39f09621c9975582a2b6d9a6fa0313b308086 (patch) | |
tree | 918786fdf371606f80bc9cdfbc2966909703e9bf /js/storage.js | |
parent | 144581a54b8bb1808e23a3ea5c81e619e36a459f (diff) | |
download | ematrix-9ec39f09621c9975582a2b6d9a6fa0313b308086.tar.lz ematrix-9ec39f09621c9975582a2b6d9a6fa0313b308086.tar.xz ematrix-9ec39f09621c9975582a2b6d9a6fa0313b308086.zip |
remove tabs in javascript files
Diffstat (limited to 'js/storage.js')
-rw-r--r-- | js/storage.js | 325 |
1 files changed, 183 insertions, 142 deletions
diff --git a/js/storage.js b/js/storage.js index 3302de0..a96caf3 100644 --- a/js/storage.js +++ b/js/storage.js @@ -27,13 +27,13 @@ /******************************************************************************/ -ηMatrix.getBytesInUse = function() { +ηMatrix.getBytesInUse = function () { var ηm = this; - var getBytesInUseHandler = function(bytesInUse) { + var getBytesInUseHandler = function (bytesInUse) { ηm.storageUsed = bytesInUse; }; // Not all WebExtension implementations support getBytesInUse(). - if ( typeof vAPI.storage.getBytesInUse === 'function' ) { + if (typeof vAPI.storage.getBytesInUse === 'function') { vAPI.storage.getBytesInUse(null, getBytesInUseHandler); } else { ηm.storageUsed = undefined; @@ -42,21 +42,21 @@ /******************************************************************************/ -ηMatrix.saveUserSettings = function() { +ηMatrix.saveUserSettings = function () { this.XAL.keyvalSetMany( this.userSettings, this.getBytesInUse.bind(this) ); }; -ηMatrix.loadUserSettings = function(callback) { +ηMatrix.loadUserSettings = function (callback) { var ηm = this; - if ( typeof callback !== 'function' ) { + if (typeof callback !== 'function') { callback = this.noopFunc; } - var settingsLoaded = function(store) { + var settingsLoaded = function (store) { // console.log('storage.js > loaded user settings'); ηm.userSettings = store; @@ -69,12 +69,14 @@ /******************************************************************************/ -ηMatrix.loadRawSettings = function() { +ηMatrix.loadRawSettings = function () { var ηm = this; - var onLoaded = function(bin) { - if ( !bin || bin.rawSettings instanceof Object === false ) { return; } - for ( var key of Object.keys(bin.rawSettings) ) { + var onLoaded = function (bin) { + if (!bin || bin.rawSettings instanceof Object === false) { + return; + } + for (var key of Object.keys(bin.rawSettings)) { if ( ηm.rawSettings.hasOwnProperty(key) === false || typeof bin.rawSettings[key] !== typeof ηm.rawSettings[key] @@ -89,15 +91,15 @@ vAPI.storage.get('rawSettings', onLoaded); }; -ηMatrix.saveRawSettings = function(rawSettings, callback) { +ηMatrix.saveRawSettings = function (rawSettings, callback) { var keys = Object.keys(rawSettings); - if ( keys.length === 0 ) { - if ( typeof callback === 'function' ) { + if (keys.length === 0) { + if (typeof callback === 'function') { callback(); } return; } - for ( var key of keys ) { + for (var key of keys) { if ( this.rawSettingsDefault.hasOwnProperty(key) && typeof rawSettings[key] === typeof this.rawSettingsDefault[key] @@ -105,57 +107,61 @@ this.rawSettings[key] = rawSettings[key]; } } - vAPI.storage.set({ rawSettings: this.rawSettings }, callback); + vAPI.storage.set({ + rawSettings: this.rawSettings + }, callback); this.rawSettingsWriteTime = Date.now(); }; -ηMatrix.rawSettingsFromString = function(raw) { +ηMatrix.rawSettingsFromString = function (raw) { var result = {}, lineIter = new this.LineIterator(raw), line, matches, name, value; - while ( lineIter.eot() === false ) { + while (lineIter.eot() === false) { line = lineIter.next().trim(); matches = /^(\S+)(\s+(.+))?$/.exec(line); - if ( matches === null ) { continue; } + if (matches === null) { + continue; + } name = matches[1]; - if ( this.rawSettingsDefault.hasOwnProperty(name) === false ) { + if (this.rawSettingsDefault.hasOwnProperty(name) === false) { continue; } value = (matches[2] || '').trim(); - switch ( typeof this.rawSettingsDefault[name] ) { - case 'boolean': - if ( value === 'true' ) { - value = true; - } else if ( value === 'false' ) { - value = false; - } else { - value = this.rawSettingsDefault[name]; - } - break; - case 'string': - if ( value === '' ) { - value = this.rawSettingsDefault[name]; - } - break; - case 'number': - value = parseInt(value, 10); - if ( isNaN(value) ) { - value = this.rawSettingsDefault[name]; - } - break; - default: - break; + switch (typeof this.rawSettingsDefault[name]) { + case 'boolean': + if (value === 'true') { + value = true; + } else if (value === 'false') { + value = false; + } else { + value = this.rawSettingsDefault[name]; + } + break; + case 'string': + if (value === '') { + value = this.rawSettingsDefault[name]; + } + break; + case 'number': + value = parseInt(value, 10); + if (isNaN(value)) { + value = this.rawSettingsDefault[name]; + } + break; + default: + break; } - if ( this.rawSettings[name] !== value ) { + if (this.rawSettings[name] !== value) { result[name] = value; } } this.saveRawSettings(result); }; -ηMatrix.stringFromRawSettings = function() { +ηMatrix.stringFromRawSettings = function () { var out = []; - for ( var key of Object.keys(this.rawSettings).sort() ) { + for (var key of Object.keys(this.rawSettings).sort()) { out.push(key + ' ' + this.rawSettings[key]); } return out.join('\n'); @@ -164,19 +170,19 @@ /******************************************************************************/ // save white/blacklist -ηMatrix.saveMatrix = function() { +ηMatrix.saveMatrix = function () { ηMatrix.XAL.keyvalSetOne('userMatrix', this.pMatrix.toString()); }; /******************************************************************************/ -ηMatrix.loadMatrix = function(callback) { - if ( typeof callback !== 'function' ) { +ηMatrix.loadMatrix = function (callback) { + if (typeof callback !== 'function') { callback = this.noopFunc; } var ηm = this; - var onLoaded = function(bin) { - if ( bin.hasOwnProperty('userMatrix') ) { + var onLoaded = function (bin) { + if (bin.hasOwnProperty('userMatrix')) { ηm.pMatrix.fromString(bin.userMatrix); ηm.tMatrix.assign(ηm.pMatrix); callback(); @@ -187,15 +193,17 @@ /******************************************************************************/ -ηMatrix.listKeysFromCustomHostsFiles = function(raw) { +ηMatrix.listKeysFromCustomHostsFiles = function (raw) { var out = new Set(), reIgnore = /^[!#]/, reValid = /^[a-z-]+:\/\/\S+/, lineIter = new this.LineIterator(raw), location; - while ( lineIter.eot() === false ) { + while (lineIter.eot() === false) { location = lineIter.next().trim(); - if ( reIgnore.test(location) || !reValid.test(location) ) { continue; } + if (reIgnore.test(location) || !reValid.test(location)) { + continue; + } out.add(location); } return this.setToArray(out); @@ -203,7 +211,7 @@ /******************************************************************************/ -ηMatrix.getAvailableHostsFiles = function(callback) { +ηMatrix.getAvailableHostsFiles = function (callback) { var ηm = this, availableHostsFiles = {}; @@ -211,7 +219,7 @@ var importedListKeys = this.listKeysFromCustomHostsFiles(ηm.userSettings.externalHostsFiles), i = importedListKeys.length, listKey, entry; - while ( i-- ) { + while (i--) { listKey = importedListKeys[i]; entry = { content: 'filters', @@ -225,31 +233,37 @@ } // selected lists - var onSelectedHostsFilesLoaded = function(bin) { + var onSelectedHostsFilesLoaded = function (bin) { // Now get user's selection of lists - for ( var assetKey in bin.liveHostsFiles ) { + for (var assetKey in bin.liveHostsFiles) { var availableEntry = availableHostsFiles[assetKey]; - if ( availableEntry === undefined ) { continue; } + if (availableEntry === undefined) { + continue; + } var liveEntry = bin.liveHostsFiles[assetKey]; availableEntry.off = liveEntry.off || false; - if ( liveEntry.entryCount !== undefined ) { + if (liveEntry.entryCount !== undefined) { availableEntry.entryCount = liveEntry.entryCount; } - if ( liveEntry.entryUsedCount !== undefined ) { + if (liveEntry.entryUsedCount !== undefined) { availableEntry.entryUsedCount = liveEntry.entryUsedCount; } // This may happen if the list name was pulled from the list content - if ( availableEntry.title === '' && liveEntry.title !== undefined ) { + if (availableEntry.title === '' && liveEntry.title !== undefined) { availableEntry.title = liveEntry.title; } } // Remove unreferenced imported filter lists. var dict = new Set(importedListKeys); - for ( assetKey in availableHostsFiles ) { + for (assetKey in availableHostsFiles) { var entry = availableHostsFiles[assetKey]; - if ( entry.submitter !== 'user' ) { continue; } - if ( dict.has(assetKey) ) { continue; } + if (entry.submitter !== 'user') { + continue; + } + if (dict.has(assetKey)) { + continue; + } delete availableHostsFiles[assetKey]; ηm.assets.unregisterAssetSource(assetKey); ηm.assets.remove(assetKey); @@ -259,18 +273,23 @@ }; // built-in lists - var onBuiltinHostsFilesLoaded = function(entries) { - for ( var assetKey in entries ) { - if ( entries.hasOwnProperty(assetKey) === false ) { continue; } + var onBuiltinHostsFilesLoaded = function (entries) { + for (var assetKey in entries) { + if (entries.hasOwnProperty(assetKey) === false) { + continue; + } entry = entries[assetKey]; - if ( entry.content !== 'filters' ) { continue; } + if (entry.content !== 'filters') { + continue; + } availableHostsFiles[assetKey] = objectAssign({}, entry); } // Now get user's selection of lists - vAPI.storage.get( - { 'liveHostsFiles': availableHostsFiles }, - onSelectedHostsFilesLoaded + vAPI.storage.get({ + 'liveHostsFiles': availableHostsFiles + }, + onSelectedHostsFilesLoaded ); }; @@ -279,31 +298,35 @@ /******************************************************************************/ -ηMatrix.loadHostsFiles = function(callback) { +ηMatrix.loadHostsFiles = function (callback) { var ηm = ηMatrix; var hostsFileLoadCount; - if ( typeof callback !== 'function' ) { + if (typeof callback !== 'function') { callback = this.noopFunc; } - var loadHostsFilesEnd = function() { + var loadHostsFilesEnd = function () { ηm.ubiquitousBlacklist.freeze(); - vAPI.storage.set({ 'liveHostsFiles': ηm.liveHostsFiles }); - vAPI.messaging.broadcast({ what: 'loadHostsFilesCompleted' }); + vAPI.storage.set({ + 'liveHostsFiles': ηm.liveHostsFiles + }); + vAPI.messaging.broadcast({ + what: 'loadHostsFilesCompleted' + }); ηm.getBytesInUse(); callback(); }; - var mergeHostsFile = function(details) { + var mergeHostsFile = function (details) { ηm.mergeHostsFile(details); hostsFileLoadCount -= 1; - if ( hostsFileLoadCount === 0 ) { + if (hostsFileLoadCount === 0) { loadHostsFilesEnd(); } }; - var loadHostsFilesStart = function(hostsFiles) { + var loadHostsFilesStart = function (hostsFiles) { ηm.liveHostsFiles = hostsFiles; ηm.ubiquitousBlacklist.reset(); var locations = Object.keys(hostsFiles); @@ -311,8 +334,8 @@ // Load all hosts file which are not disabled. var location; - while ( (location = locations.pop()) ) { - if ( hostsFiles[location].off ) { + while ((location = locations.pop())) { + if (hostsFiles[location].off) { hostsFileLoadCount -= 1; continue; } @@ -320,7 +343,7 @@ } // https://github.com/gorhill/uMatrix/issues/2 - if ( hostsFileLoadCount === 0 ) { + if (hostsFileLoadCount === 0) { loadHostsFilesEnd(); return; } @@ -331,7 +354,7 @@ /******************************************************************************/ -ηMatrix.mergeHostsFile = function(details) { +ηMatrix.mergeHostsFile = function (details) { var usedCount = this.ubiquitousBlacklist.count; var duplicateCount = this.ubiquitousBlacklist.duplicateCount; @@ -347,20 +370,21 @@ /******************************************************************************/ -ηMatrix.mergeHostsFileContent = function(rawText) { +ηMatrix.mergeHostsFileContent = function (rawText) { var rawEnd = rawText.length; var ubiquitousBlacklist = this.ubiquitousBlacklist; var reLocalhost = /(^|\s)(localhost\.localdomain|localhost|local|broadcasthost|0\.0\.0\.0|127\.0\.0\.1|::1|fe80::1%lo0)(?=\s|$)/g; var reAsciiSegment = /^[\x21-\x7e]+$/; var matches; - var lineBeg = 0, lineEnd; + var lineBeg = 0, + lineEnd; var line; - while ( lineBeg < rawEnd ) { + while (lineBeg < rawEnd) { lineEnd = rawText.indexOf('\n', lineBeg); - if ( lineEnd < 0 ) { + if (lineEnd < 0) { lineEnd = rawText.indexOf('\r', lineBeg); - if ( lineEnd < 0 ) { + if (lineEnd < 0) { lineEnd = rawEnd; } } @@ -382,19 +406,19 @@ // The filter is whatever sequence of printable ascii character without // whitespaces matches = reAsciiSegment.exec(line); - if ( !matches || matches.length === 0 ) { + if (!matches || matches.length === 0) { continue; } // Bypass anomalies // For example, when a filter contains whitespace characters, or // whatever else outside the range of printable ascii characters. - if ( matches[0] !== line ) { + if (matches[0] !== line) { continue; } line = matches[0]; - if ( line === '' ) { + if (line === '') { continue; } @@ -406,28 +430,28 @@ // `switches` contains the filter lists for which the switch must be revisited. -ηMatrix.selectHostsFiles = function(details, callback) { +ηMatrix.selectHostsFiles = function (details, callback) { var ηm = this, externalHostsFiles = this.userSettings.externalHostsFiles, i, n, assetKey; // Hosts file to select - if ( Array.isArray(details.toSelect) ) { - for ( assetKey in this.liveHostsFiles ) { - if ( this.liveHostsFiles.hasOwnProperty(assetKey) === false ) { + if (Array.isArray(details.toSelect)) { + for (assetKey in this.liveHostsFiles) { + if (this.liveHostsFiles.hasOwnProperty(assetKey) === false) { continue; } - if ( details.toSelect.indexOf(assetKey) !== -1 ) { + if (details.toSelect.indexOf(assetKey) !== -1) { this.liveHostsFiles[assetKey].off = false; - } else if ( details.merge !== true ) { + } else if (details.merge !== true) { this.liveHostsFiles[assetKey].off = true; } } } // Imported hosts files to remove - if ( Array.isArray(details.toRemove) ) { - var removeURLFromHaystack = function(haystack, needle) { + if (Array.isArray(details.toRemove)) { + var removeURLFromHaystack = function (haystack, needle) { return haystack.replace( new RegExp( '(^|\\n)' + @@ -436,7 +460,7 @@ '\n' ).trim(); }; - for ( i = 0, n = details.toRemove.length; i < n; i++ ) { + for (i = 0, n = details.toRemove.length; i < n; i++) { assetKey = details.toRemove[i]; delete this.liveHostsFiles[assetKey]; externalHostsFiles = removeURLFromHaystack(externalHostsFiles, assetKey); @@ -445,23 +469,30 @@ } // Hosts file to import - if ( typeof details.toImport === 'string' ) { + if (typeof details.toImport === 'string') { // https://github.com/gorhill/uBlock/issues/1181 // Try mapping the URL of an imported filter list to the assetKey of an // existing stock list. - var assetKeyFromURL = function(url) { + var assetKeyFromURL = function (url) { var needle = url.replace(/^https?:/, ''); - var assets = ηm.liveHostsFiles, asset; - for ( var assetKey in assets ) { + var assets = ηm.liveHostsFiles, + asset; + for (var assetKey in assets) { asset = assets[assetKey]; - if ( asset.content !== 'filters' ) { continue; } - if ( typeof asset.contentURL === 'string' ) { - if ( asset.contentURL.endsWith(needle) ) { return assetKey; } + if (asset.content !== 'filters') { continue; } - if ( Array.isArray(asset.contentURL) === false ) { continue; } - for ( i = 0, n = asset.contentURL.length; i < n; i++ ) { - if ( asset.contentURL[i].endsWith(needle) ) { + if (typeof asset.contentURL === 'string') { + if (asset.contentURL.endsWith(needle)) { + return assetKey; + } + continue; + } + if (Array.isArray(asset.contentURL) === false) { + continue; + } + for (i = 0, n = asset.contentURL.length; i < n; i++) { + if (asset.contentURL[i].endsWith(needle)) { return assetKey; } } @@ -473,28 +504,36 @@ iter = toImportSet.values(); for (;;) { var entry = iter.next(); - if ( entry.done ) { break; } - if ( importedSet.has(entry.value) ) { continue; } + if (entry.done) { + break; + } + if (importedSet.has(entry.value)) { + continue; + } assetKey = assetKeyFromURL(entry.value); - if ( assetKey === entry.value ) { + if (assetKey === entry.value) { importedSet.add(entry.value); } this.liveHostsFiles[assetKey] = { content: 'filters', - contentURL: [ assetKey ], + contentURL: [assetKey], title: assetKey }; } externalHostsFiles = this.setToArray(importedSet).sort().join('\n'); } - if ( externalHostsFiles !== this.userSettings.externalHostsFiles ) { + if (externalHostsFiles !== this.userSettings.externalHostsFiles) { this.userSettings.externalHostsFiles = externalHostsFiles; - vAPI.storage.set({ externalHostsFiles: externalHostsFiles }); + vAPI.storage.set({ + externalHostsFiles: externalHostsFiles + }); } - vAPI.storage.set({ 'liveHostsFiles': this.liveHostsFiles }); + vAPI.storage.set({ + 'liveHostsFiles': this.liveHostsFiles + }); - if ( typeof callback === 'function' ) { + if (typeof callback === 'function') { callback(); } }; @@ -504,19 +543,19 @@ // `switches` contains the preset blacklists for which the switch must be // revisited. -ηMatrix.reloadHostsFiles = function() { +ηMatrix.reloadHostsFiles = function () { this.loadHostsFiles(); }; /******************************************************************************/ -ηMatrix.loadPublicSuffixList = function(callback) { - if ( typeof callback !== 'function' ) { +ηMatrix.loadPublicSuffixList = function (callback) { + if (typeof callback !== 'function') { callback = this.noopFunc; } - var applyPublicSuffixList = function(details) { - if ( !details.error ) { + var applyPublicSuffixList = function (details) { + if (!details.error) { publicSuffixList.parse(details.content, punycode.toASCII); } callback(); @@ -527,37 +566,39 @@ /******************************************************************************/ -ηMatrix.scheduleAssetUpdater = (function() { +ηMatrix.scheduleAssetUpdater = (function () { var timer, next = 0; - return function(updateDelay) { - if ( timer ) { + return function (updateDelay) { + if (timer) { clearTimeout(timer); timer = undefined; } - if ( updateDelay === 0 ) { + if (updateDelay === 0) { next = 0; return; } var now = Date.now(); // Use the new schedule if and only if it is earlier than the previous // one. - if ( next !== 0 ) { + if (next !== 0) { updateDelay = Math.min(updateDelay, Math.max(next - now, 0)); } next = now + updateDelay; - timer = vAPI.setTimeout(function() { + timer = vAPI.setTimeout(function () { timer = undefined; next = 0; - ηMatrix.assets.updateStart({ delay: 120000 }); + ηMatrix.assets.updateStart({ + delay: 120000 + }); }, updateDelay); }; })(); /******************************************************************************/ -ηMatrix.assetObserver = function(topic, details) { +ηMatrix.assetObserver = function (topic, details) { // Do not update filter list if not in use. - if ( topic === 'before-asset-updated' ) { + if (topic === 'before-asset-updated') { if ( this.liveHostsFiles.hasOwnProperty(details.assetKey) === false || this.liveHostsFiles[details.assetKey].off === true @@ -567,7 +608,7 @@ return true; } - if ( topic === 'after-asset-updated' ) { + if (topic === 'after-asset-updated') { vAPI.messaging.broadcast({ what: 'assetUpdated', key: details.assetKey, @@ -577,7 +618,7 @@ } // Update failed. - if ( topic === 'asset-update-failed' ) { + if (topic === 'asset-update-failed') { vAPI.messaging.broadcast({ what: 'assetUpdated', key: details.assetKey, @@ -587,11 +628,11 @@ } // Reload all filter lists if needed. - if ( topic === 'after-assets-updated' ) { - if ( details.assetKeys.length !== 0 ) { + if (topic === 'after-assets-updated') { + if (details.assetKeys.length !== 0) { this.loadHostsFiles(); } - if ( this.userSettings.autoUpdate ) { + if (this.userSettings.autoUpdate) { this.scheduleAssetUpdater(25200000); } else { this.scheduleAssetUpdater(0); @@ -605,10 +646,10 @@ // New asset source became available, if it's a filter list, should we // auto-select it? - if ( topic === 'builtin-asset-source-added' ) { - if ( details.entry.content === 'filters' ) { - if ( details.entry.off !== true ) { - this.saveSelectedFilterLists([ details.assetKey ], true); + if (topic === 'builtin-asset-source-added') { + if (details.entry.content === 'filters') { + if (details.entry.off !== true) { + this.saveSelectedFilterLists([details.assetKey], true); } } return; |