Skip to content

Commit

Permalink
Allow setting assetsBootstrapLocation from admin settings
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Jul 18, 2019
1 parent d19de3e commit 10fe9fe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 60 deletions.
98 changes: 44 additions & 54 deletions src/js/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/******************************************************************************/

µBlock.assets = (function() {
µBlock.assets = (( ) => {

/******************************************************************************/

Expand All @@ -36,7 +36,7 @@ const api = {};

/******************************************************************************/

var observers = [];
const observers = [];

api.addObserver = function(observer) {
if ( observers.indexOf(observer) === -1 ) {
Expand All @@ -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;
Expand Down Expand Up @@ -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'
Expand All @@ -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);
});
Expand Down Expand Up @@ -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 ];
Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -820,16 +819,16 @@ 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';
}
registerAssetSource(assetKey, { error: { time: Date.now(), error: text } });
tryLoading();
};

var tryLoading = function() {
const tryLoading = function() {
while ( (contentURL = contentURLs.shift()) ) {
if ( reIsExternalPath.test(contentURL) ) { break; }
}
Expand All @@ -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 ];
Expand Down Expand Up @@ -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;
Expand All @@ -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()
);
};

/******************************************************************************/
Expand Down Expand Up @@ -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();
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 8 additions & 5 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) {
Expand Down

0 comments on commit 10fe9fe

Please sign in to comment.