1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

Allow setting assetsBootstrapLocation from admin settings

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/666
This commit is contained in:
Raymond Hill 2019-07-18 10:53:08 -04:00
parent d19de3e283
commit 10fe9fe656
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 53 additions and 60 deletions

View File

@ -23,7 +23,7 @@
/******************************************************************************/ /******************************************************************************/
µBlock.assets = (function() { µBlock.assets = (( ) => {
/******************************************************************************/ /******************************************************************************/
@ -36,7 +36,7 @@ const api = {};
/******************************************************************************/ /******************************************************************************/
var observers = []; const observers = [];
api.addObserver = function(observer) { api.addObserver = function(observer) {
if ( observers.indexOf(observer) === -1 ) { if ( observers.indexOf(observer) === -1 ) {
@ -45,16 +45,16 @@ api.addObserver = function(observer) {
}; };
api.removeObserver = function(observer) { api.removeObserver = function(observer) {
var pos; let pos;
while ( (pos = observers.indexOf(observer)) !== -1 ) { while ( (pos = observers.indexOf(observer)) !== -1 ) {
observers.splice(pos, 1); observers.splice(pos, 1);
} }
}; };
var fireNotification = function(topic, details) { const fireNotification = function(topic, details) {
var result, r; let result;
for ( var i = 0; i < observers.length; i++ ) { for ( const observer of observers ) {
r = observers[i](topic, details); const r = observer(topic, details);
if ( r !== undefined ) { result = r; } if ( r !== undefined ) { result = r; }
} }
return result; return result;
@ -414,7 +414,7 @@ const updateAssetSourceRegistry = function(json, silent) {
saveAssetSourceRegistry(); saveAssetSourceRegistry();
}; };
const getAssetSourceRegistry = function(callback) { const getAssetSourceRegistry = function() {
if ( assetSourceRegistryPromise === undefined ) { if ( assetSourceRegistryPromise === undefined ) {
assetSourceRegistryPromise = µBlock.cacheStorage.get( assetSourceRegistryPromise = µBlock.cacheStorage.get(
'assetSourceRegistry' 'assetSourceRegistry'
@ -424,34 +424,33 @@ const getAssetSourceRegistry = function(callback) {
bin.assetSourceRegistry instanceof Object bin.assetSourceRegistry instanceof Object
) { ) {
assetSourceRegistry = bin.assetSourceRegistry; assetSourceRegistry = bin.assetSourceRegistry;
return; return assetSourceRegistry;
} }
return new Promise(resolve => { return api.fetchText(
api.fetchText( µBlock.assetsBootstrapLocation || 'assets/assets.json'
µBlock.assetsBootstrapLocation || 'assets/assets.json', ).then(details => {
details => { return details.content !== ''
? details
: api.fetchText('assets/assets.json');
}).then(details => {
updateAssetSourceRegistry(details.content, true); updateAssetSourceRegistry(details.content, true);
resolve(); return assetSourceRegistry;
}
);
}); });
}); });
} }
assetSourceRegistryPromise.then(( ) => { return assetSourceRegistryPromise;
callback(assetSourceRegistry);
});
}; };
api.registerAssetSource = function(assetKey, details) { api.registerAssetSource = function(assetKey, details) {
getAssetSourceRegistry(function() { getAssetSourceRegistry().then(( ) => {
registerAssetSource(assetKey, details); registerAssetSource(assetKey, details);
saveAssetSourceRegistry(true); saveAssetSourceRegistry(true);
}); });
}; };
api.unregisterAssetSource = function(assetKey) { api.unregisterAssetSource = function(assetKey) {
getAssetSourceRegistry(function() { getAssetSourceRegistry().then(( ) => {
unregisterAssetSource(assetKey); unregisterAssetSource(assetKey);
saveAssetSourceRegistry(true); saveAssetSourceRegistry(true);
}); });
@ -771,7 +770,7 @@ api.get = function(assetKey, options, callback) {
if ( details.content !== '' ) { if ( details.content !== '' ) {
return reportBack(details.content); return reportBack(details.content);
} }
getAssetSourceRegistry(function(registry) { getAssetSourceRegistry().then(registry => {
assetDetails = registry[assetKey] || {}; assetDetails = registry[assetKey] || {};
if ( typeof assetDetails.contentURL === 'string' ) { if ( typeof assetDetails.contentURL === 'string' ) {
contentURLs = [ assetDetails.contentURL ]; contentURLs = [ assetDetails.contentURL ];
@ -792,12 +791,12 @@ api.get = function(assetKey, options, callback) {
/******************************************************************************/ /******************************************************************************/
const getRemote = function(assetKey, callback) { const getRemote = function(assetKey, callback) {
var assetDetails = {}, let assetDetails = {};
contentURLs, let contentURLs;
contentURL; let contentURL;
var reportBack = function(content, err) { const reportBack = function(content, err) {
var details = { assetKey: assetKey, content: content }; const details = { assetKey: assetKey, content: content };
if ( err ) { if ( err ) {
details.error = assetDetails.lastError = err; details.error = assetDetails.lastError = err;
} else { } else {
@ -806,7 +805,7 @@ const getRemote = function(assetKey, callback) {
callback(details); callback(details);
}; };
var onRemoteContentLoaded = function(details) { const onRemoteContentLoaded = function(details) {
if ( stringIsNotEmpty(details.content) === false ) { if ( stringIsNotEmpty(details.content) === false ) {
registerAssetSource(assetKey, { error: { time: Date.now(), error: 'No content' } }); registerAssetSource(assetKey, { error: { time: Date.now(), error: 'No content' } });
tryLoading(); tryLoading();
@ -820,8 +819,8 @@ const getRemote = function(assetKey, callback) {
reportBack(details.content); reportBack(details.content);
}; };
var onRemoteContentError = function(details) { const onRemoteContentError = function(details) {
var text = details.statusText; let text = details.statusText;
if ( details.statusCode === 0 ) { if ( details.statusCode === 0 ) {
text = 'network error'; text = 'network error';
} }
@ -829,7 +828,7 @@ const getRemote = function(assetKey, callback) {
tryLoading(); tryLoading();
}; };
var tryLoading = function() { const tryLoading = function() {
while ( (contentURL = contentURLs.shift()) ) { while ( (contentURL = contentURLs.shift()) ) {
if ( reIsExternalPath.test(contentURL) ) { break; } if ( reIsExternalPath.test(contentURL) ) { break; }
} }
@ -843,7 +842,7 @@ const getRemote = function(assetKey, callback) {
} }
}; };
getAssetSourceRegistry(function(registry) { getAssetSourceRegistry().then(registry => {
assetDetails = registry[assetKey] || {}; assetDetails = registry[assetKey] || {};
if ( typeof assetDetails.contentURL === 'string' ) { if ( typeof assetDetails.contentURL === 'string' ) {
contentURLs = [ assetDetails.contentURL ]; contentURLs = [ assetDetails.contentURL ];
@ -877,9 +876,6 @@ api.put = function(assetKey, content, callback) {
/******************************************************************************/ /******************************************************************************/
api.metadata = function(callback) { api.metadata = function(callback) {
let assetRegistryReady = false,
cacheRegistryReady = false;
const onReady = function() { const onReady = function() {
const assetDict = JSON.parse(JSON.stringify(assetSourceRegistry)); const assetDict = JSON.parse(JSON.stringify(assetSourceRegistry));
const cacheDict = assetCacheRegistry; const cacheDict = assetCacheRegistry;
@ -905,15 +901,12 @@ api.metadata = function(callback) {
callback(assetDict); callback(assetDict);
}; };
getAssetSourceRegistry(( ) => { Promise.all([
assetRegistryReady = true; getAssetSourceRegistry(),
if ( cacheRegistryReady ) { onReady(); } getAssetCacheRegistry(),
}); ]).then(
( ) => onReady()
getAssetCacheRegistry().then(( ) => { );
cacheRegistryReady = true;
if ( assetRegistryReady ) { onReady(); }
});
}; };
/******************************************************************************/ /******************************************************************************/
@ -1025,15 +1018,12 @@ const updateNext = function() {
getRemote(assetKey, updatedOne); getRemote(assetKey, updatedOne);
}; };
getAssetSourceRegistry(function(dict) { Promise.all([
assetDict = dict; getAssetSourceRegistry(),
if ( !cacheDict ) { return; } getAssetCacheRegistry(),
updateOne(); ]).then(results => {
}); assetDict = results[0];
cacheDict = results[1];
getAssetCacheRegistry().then(dict => {
cacheDict = dict;
if ( !assetDict ) { return; }
updateOne(); updateOne();
}); });
}; };

View File

@ -155,7 +155,7 @@ const µBlock = (function() { // jshint ignore:line
// Allows to fully customize uBO's assets, typically set through admin // Allows to fully customize uBO's assets, typically set through admin
// settings. The content of 'assets.json' will also tell which filter // settings. The content of 'assets.json' will also tell which filter
// lists to enable by default when uBO is first installed. // lists to enable by default when uBO is first installed.
assetsBootstrapLocation: 'assets/assets.json', assetsBootstrapLocation: undefined,
userFiltersPath: 'user-filters', userFiltersPath: 'user-filters',
pslAssetKey: 'public_suffix_list.dat', pslAssetKey: 'public_suffix_list.dat',

View File

@ -1188,11 +1188,14 @@
const bin = {}; const bin = {};
let binNotEmpty = false; let binNotEmpty = false;
// Allows an admin to set their own 'assets.json' file, with their own // https://github.com/uBlockOrigin/uBlock-issues/issues/666
// set of stock assets. // Allows an admin to set their own 'assets.json' file, with their
if ( typeof data.assetsBootstrapLocation === 'string' ) { // own set of stock assets.
bin.assetsBootstrapLocation = data.assetsBootstrapLocation; if (
binNotEmpty = true; typeof data.assetsBootstrapLocation === 'string' &&
data.assetsBootstrapLocation !== ''
) {
µBlock.assetsBootstrapLocation = data.assetsBootstrapLocation;
} }
if ( typeof data.userSettings === 'object' ) { if ( typeof data.userSettings === 'object' ) {