From 10fe9fe6569375d03d72b42e38a4f7558e935934 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 18 Jul 2019 10:53:08 -0400 Subject: [PATCH] Allow setting `assetsBootstrapLocation` from admin settings Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/666 --- src/js/assets.js | 98 ++++++++++++++++++++------------------------ src/js/background.js | 2 +- src/js/storage.js | 13 +++--- 3 files changed, 53 insertions(+), 60 deletions(-) diff --git a/src/js/assets.js b/src/js/assets.js index 8e1c0dbb3..f4aa76d99 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -23,7 +23,7 @@ /******************************************************************************/ -µBlock.assets = (function() { +µBlock.assets = (( ) => { /******************************************************************************/ @@ -36,7 +36,7 @@ const api = {}; /******************************************************************************/ -var observers = []; +const observers = []; api.addObserver = function(observer) { if ( observers.indexOf(observer) === -1 ) { @@ -45,16 +45,16 @@ api.addObserver = function(observer) { }; api.removeObserver = function(observer) { - var pos; + let pos; while ( (pos = observers.indexOf(observer)) !== -1 ) { observers.splice(pos, 1); } }; -var fireNotification = function(topic, details) { - var result, r; - for ( var i = 0; i < observers.length; i++ ) { - r = observers[i](topic, details); +const fireNotification = function(topic, details) { + let result; + for ( const observer of observers ) { + const r = observer(topic, details); if ( r !== undefined ) { result = r; } } return result; @@ -414,7 +414,7 @@ const updateAssetSourceRegistry = function(json, silent) { saveAssetSourceRegistry(); }; -const getAssetSourceRegistry = function(callback) { +const getAssetSourceRegistry = function() { if ( assetSourceRegistryPromise === undefined ) { assetSourceRegistryPromise = µBlock.cacheStorage.get( 'assetSourceRegistry' @@ -424,34 +424,33 @@ const getAssetSourceRegistry = function(callback) { bin.assetSourceRegistry instanceof Object ) { assetSourceRegistry = bin.assetSourceRegistry; - return; + return assetSourceRegistry; } - return new Promise(resolve => { - api.fetchText( - µBlock.assetsBootstrapLocation || 'assets/assets.json', - details => { - updateAssetSourceRegistry(details.content, true); - resolve(); - } - ); + return api.fetchText( + µBlock.assetsBootstrapLocation || 'assets/assets.json' + ).then(details => { + return details.content !== '' + ? details + : api.fetchText('assets/assets.json'); + }).then(details => { + updateAssetSourceRegistry(details.content, true); + return assetSourceRegistry; }); }); } - assetSourceRegistryPromise.then(( ) => { - callback(assetSourceRegistry); - }); + return assetSourceRegistryPromise; }; api.registerAssetSource = function(assetKey, details) { - getAssetSourceRegistry(function() { + getAssetSourceRegistry().then(( ) => { registerAssetSource(assetKey, details); saveAssetSourceRegistry(true); }); }; api.unregisterAssetSource = function(assetKey) { - getAssetSourceRegistry(function() { + getAssetSourceRegistry().then(( ) => { unregisterAssetSource(assetKey); saveAssetSourceRegistry(true); }); @@ -771,7 +770,7 @@ api.get = function(assetKey, options, callback) { if ( details.content !== '' ) { return reportBack(details.content); } - getAssetSourceRegistry(function(registry) { + getAssetSourceRegistry().then(registry => { assetDetails = registry[assetKey] || {}; if ( typeof assetDetails.contentURL === 'string' ) { contentURLs = [ assetDetails.contentURL ]; @@ -792,12 +791,12 @@ api.get = function(assetKey, options, callback) { /******************************************************************************/ const getRemote = function(assetKey, callback) { - var assetDetails = {}, - contentURLs, - contentURL; + let assetDetails = {}; + let contentURLs; + let contentURL; - var reportBack = function(content, err) { - var details = { assetKey: assetKey, content: content }; + const reportBack = function(content, err) { + const details = { assetKey: assetKey, content: content }; if ( err ) { details.error = assetDetails.lastError = err; } else { @@ -806,7 +805,7 @@ const getRemote = function(assetKey, callback) { callback(details); }; - var onRemoteContentLoaded = function(details) { + const onRemoteContentLoaded = function(details) { if ( stringIsNotEmpty(details.content) === false ) { registerAssetSource(assetKey, { error: { time: Date.now(), error: 'No content' } }); tryLoading(); @@ -820,8 +819,8 @@ const getRemote = function(assetKey, callback) { reportBack(details.content); }; - var onRemoteContentError = function(details) { - var text = details.statusText; + const onRemoteContentError = function(details) { + let text = details.statusText; if ( details.statusCode === 0 ) { text = 'network error'; } @@ -829,7 +828,7 @@ const getRemote = function(assetKey, callback) { tryLoading(); }; - var tryLoading = function() { + const tryLoading = function() { while ( (contentURL = contentURLs.shift()) ) { if ( reIsExternalPath.test(contentURL) ) { break; } } @@ -843,7 +842,7 @@ const getRemote = function(assetKey, callback) { } }; - getAssetSourceRegistry(function(registry) { + getAssetSourceRegistry().then(registry => { assetDetails = registry[assetKey] || {}; if ( typeof assetDetails.contentURL === 'string' ) { contentURLs = [ assetDetails.contentURL ]; @@ -877,9 +876,6 @@ api.put = function(assetKey, content, callback) { /******************************************************************************/ api.metadata = function(callback) { - let assetRegistryReady = false, - cacheRegistryReady = false; - const onReady = function() { const assetDict = JSON.parse(JSON.stringify(assetSourceRegistry)); const cacheDict = assetCacheRegistry; @@ -905,15 +901,12 @@ api.metadata = function(callback) { callback(assetDict); }; - getAssetSourceRegistry(( ) => { - assetRegistryReady = true; - if ( cacheRegistryReady ) { onReady(); } - }); - - getAssetCacheRegistry().then(( ) => { - cacheRegistryReady = true; - if ( assetRegistryReady ) { onReady(); } - }); + Promise.all([ + getAssetSourceRegistry(), + getAssetCacheRegistry(), + ]).then( + ( ) => onReady() + ); }; /******************************************************************************/ @@ -1025,15 +1018,12 @@ const updateNext = function() { getRemote(assetKey, updatedOne); }; - getAssetSourceRegistry(function(dict) { - assetDict = dict; - if ( !cacheDict ) { return; } - updateOne(); - }); - - getAssetCacheRegistry().then(dict => { - cacheDict = dict; - if ( !assetDict ) { return; } + Promise.all([ + getAssetSourceRegistry(), + getAssetCacheRegistry(), + ]).then(results => { + assetDict = results[0]; + cacheDict = results[1]; updateOne(); }); }; diff --git a/src/js/background.js b/src/js/background.js index d42b6cdba..d38724349 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -155,7 +155,7 @@ const µBlock = (function() { // jshint ignore:line // Allows to fully customize uBO's assets, typically set through admin // settings. The content of 'assets.json' will also tell which filter // lists to enable by default when uBO is first installed. - assetsBootstrapLocation: 'assets/assets.json', + assetsBootstrapLocation: undefined, userFiltersPath: 'user-filters', pslAssetKey: 'public_suffix_list.dat', diff --git a/src/js/storage.js b/src/js/storage.js index 24ea2d8ee..c63961741 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -1188,11 +1188,14 @@ const bin = {}; let binNotEmpty = false; - // Allows an admin to set their own 'assets.json' file, with their own - // set of stock assets. - if ( typeof data.assetsBootstrapLocation === 'string' ) { - bin.assetsBootstrapLocation = data.assetsBootstrapLocation; - binNotEmpty = true; + // https://github.com/uBlockOrigin/uBlock-issues/issues/666 + // Allows an admin to set their own 'assets.json' file, with their + // own set of stock assets. + if ( + typeof data.assetsBootstrapLocation === 'string' && + data.assetsBootstrapLocation !== '' + ) { + µBlock.assetsBootstrapLocation = data.assetsBootstrapLocation; } if ( typeof data.userSettings === 'object' ) {