mirror of https://github.com/gorhill/uBlock.git
more code review after testing
This commit is contained in:
parent
e5573eb985
commit
3f55e5ecc2
153
js/assets.js
153
js/assets.js
|
@ -430,8 +430,6 @@ var readLocalFile = function(path, callback) {
|
||||||
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getLocal = readLocalFile;
|
|
||||||
|
|
||||||
// https://www.youtube.com/watch?v=r9KVpuFPtHc
|
// https://www.youtube.com/watch?v=r9KVpuFPtHc
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -450,18 +448,18 @@ var readRepoFile = function(path, callback) {
|
||||||
|
|
||||||
var onRepoFileLoaded = function() {
|
var onRepoFileLoaded = function() {
|
||||||
this.onload = this.onerror = null;
|
this.onload = this.onerror = null;
|
||||||
// console.log('µBlock> readRepoFile() / onRepoFileLoaded()');
|
console.log('µBlock> readRepoFile("%s") / onRepoFileLoaded()', path);
|
||||||
// https://github.com/gorhill/httpswitchboard/issues/263
|
// https://github.com/gorhill/httpswitchboard/issues/263
|
||||||
if ( this.status === 200 ) {
|
if ( this.status === 200 ) {
|
||||||
reportBack(this.responseText);
|
reportBack(this.responseText);
|
||||||
} else {
|
} else {
|
||||||
reportBack('', 'Error ' + this.statusText);
|
reportBack('', 'Error: ' + this.statusText);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var onRepoFileError = function() {
|
var onRepoFileError = function() {
|
||||||
this.onload = this.onerror = null;
|
this.onload = this.onerror = null;
|
||||||
console.error('µBlock> readRepoFile() / onRepoFileError("%s")', path);
|
console.error('µBlock> readRepoFile("%s") / onRepoFileError()', path);
|
||||||
reportBack('', 'Error');
|
reportBack('', 'Error');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -475,51 +473,6 @@ var readRepoFile = function(path, callback) {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var readExternalFile = function(path, callback) {
|
|
||||||
var reportBack = function(content, err) {
|
|
||||||
var details = {
|
|
||||||
'path': path,
|
|
||||||
'content': content
|
|
||||||
};
|
|
||||||
if ( err ) {
|
|
||||||
details.error = err;
|
|
||||||
}
|
|
||||||
callback(details);
|
|
||||||
};
|
|
||||||
|
|
||||||
var onExternalFileCached = function(details) {
|
|
||||||
this.onload = this.onerror = null;
|
|
||||||
// console.log('µBlock> onExternalFileCached()');
|
|
||||||
reportBack(details.content);
|
|
||||||
};
|
|
||||||
|
|
||||||
var onExternalFileLoaded = function() {
|
|
||||||
this.onload = this.onerror = null;
|
|
||||||
// console.log('µBlock> onExternalFileLoaded()');
|
|
||||||
cachedAssetsManager.save(path, this.responseText, onExternalFileCached);
|
|
||||||
};
|
|
||||||
|
|
||||||
var onExternalFileError = function() {
|
|
||||||
console.error('µBlock> onExternalFileError() / onLocalFileError("%s")', path);
|
|
||||||
reportBack('', 'Error');
|
|
||||||
this.onload = this.onerror = null;
|
|
||||||
};
|
|
||||||
|
|
||||||
var onCachedContentLoaded = function(details) {
|
|
||||||
// console.log('µBlock> readExternalFile() / onCacheFileLoaded()');
|
|
||||||
reportBack(details.content);
|
|
||||||
};
|
|
||||||
|
|
||||||
var onCachedContentError = function(details) {
|
|
||||||
// console.error('µBlock> readExternalFile() / onCacheFileError("%s")', path);
|
|
||||||
getTextFileFromURL(details.path, onExternalFileLoaded, onExternalFileError);
|
|
||||||
};
|
|
||||||
|
|
||||||
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
// An asset from an external source with a copy shipped with the extension:
|
// An asset from an external source with a copy shipped with the extension:
|
||||||
// Path --> starts with 'assets/(thirdparties|ublock)/', with a home URL
|
// Path --> starts with 'assets/(thirdparties|ublock)/', with a home URL
|
||||||
// External -->
|
// External -->
|
||||||
|
@ -542,11 +495,9 @@ var readRepoCopyAsset = function(path, callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
var updateChecksum = function() {
|
var updateChecksum = function() {
|
||||||
if ( assetEntry !== undefined ) {
|
if ( assetEntry !== undefined && assetEntry.repoChecksum !== assetEntry.localChecksum ) {
|
||||||
if ( assetEntry.repoChecksum !== assetEntry.localChecksum ) {
|
assetEntry.localChecksum = assetEntry.repoChecksum;
|
||||||
assetEntry.localChecksum = assetEntry.repoChecksum;
|
updateLocalChecksums();
|
||||||
updateLocalChecksums();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -577,7 +528,7 @@ var readRepoCopyAsset = function(path, callback) {
|
||||||
var onRepoFileLoaded = function() {
|
var onRepoFileLoaded = function() {
|
||||||
this.onload = this.onerror = null;
|
this.onload = this.onerror = null;
|
||||||
if ( typeof this.responseText !== 'string' || this.responseText === '' ) {
|
if ( typeof this.responseText !== 'string' || this.responseText === '' ) {
|
||||||
console.error('µBlock> readRepoCopyAsset("%s") / onRepoFileLoaded("%s"): no response', path, repositoryURL);
|
console.error('µBlock> readRepoCopyAsset("%s") / onRepoFileLoaded("%s"): error', path, repositoryURL);
|
||||||
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -621,22 +572,22 @@ var readRepoCopyAsset = function(path, callback) {
|
||||||
};
|
};
|
||||||
|
|
||||||
var onCacheMetaReady = function(entries) {
|
var onCacheMetaReady = function(entries) {
|
||||||
|
// Fetch from remote if:
|
||||||
|
// - Auto-update enabled AND (not in cache OR in cache but obsolete)
|
||||||
var timestamp = entries[path];
|
var timestamp = entries[path];
|
||||||
|
var obsolete = Date.now() - exports.autoUpdateDelay;
|
||||||
|
if ( exports.autoUpdate && (typeof timestamp !== 'number' || timestamp <= obsolete) ) {
|
||||||
|
console.log('µBlock> readRepoCopyAsset("%s") / onCacheMetaReady(): not cached or obsolete', path);
|
||||||
|
getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// In cache
|
// In cache
|
||||||
if ( typeof timestamp === 'number' ) {
|
if ( typeof timestamp === 'number' ) {
|
||||||
// Verify obsolescence
|
|
||||||
if ( exports.autoUpdate ) {
|
|
||||||
var obsolete = Date.now() - exports.autoUpdateDelay;
|
|
||||||
if ( timestamp <= obsolete ) {
|
|
||||||
console.log('µBlock> readRepoCopyAsset("%s") / onCacheMetaReady(): cache is obsolete', path);
|
|
||||||
getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Not obsolete
|
|
||||||
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not in cache
|
// Not in cache
|
||||||
getTextFileFromURL(chrome.runtime.getURL(path), onInstallFileLoaded, onInstallFileError);
|
getTextFileFromURL(chrome.runtime.getURL(path), onInstallFileLoaded, onInstallFileError);
|
||||||
};
|
};
|
||||||
|
@ -646,20 +597,18 @@ var readRepoCopyAsset = function(path, callback) {
|
||||||
|
|
||||||
// Asset doesn't exist
|
// Asset doesn't exist
|
||||||
if ( assetEntry === undefined ) {
|
if ( assetEntry === undefined ) {
|
||||||
reportBack('', 'Error: Asset not found');
|
reportBack('', 'Error: asset not found');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repo copy changed: fetch from home URL
|
// Repo copy changed: fetch from home URL
|
||||||
if ( exports.autoUpdate ) {
|
if ( exports.autoUpdate && assetEntry.localChecksum !== assetEntry.repoChecksum ) {
|
||||||
if ( assetEntry.localChecksum !== assetEntry.repoChecksum ) {
|
console.log('µBlock> readRepoCopyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
|
||||||
console.log('µBlock> readRepoCopyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
|
getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
|
||||||
getTextFileFromURL(assetEntry.homeURL, onHomeFileLoaded, onHomeFileError);
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// No change, we will inspect the cached version for obsolescence
|
// Load from cache
|
||||||
cachedAssetsManager.entries(onCacheMetaReady);
|
cachedAssetsManager.entries(onCacheMetaReady);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -745,20 +694,18 @@ var readRepoOnlyAsset = function(path, callback) {
|
||||||
|
|
||||||
// Asset doesn't exist
|
// Asset doesn't exist
|
||||||
if ( assetEntry === undefined ) {
|
if ( assetEntry === undefined ) {
|
||||||
reportBack('', 'Error: Asset not found');
|
reportBack('', 'Error: asset not found');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asset added or changed: load from repo URL and then cache result
|
// Asset added or changed: load from repo URL and then cache result
|
||||||
if ( exports.autoUpdate ) {
|
if ( exports.autoUpdate && assetEntry.localChecksum !== assetEntry.repoChecksum ) {
|
||||||
if ( assetEntry.localChecksum !== assetEntry.repoChecksum ) {
|
console.log('µBlock> readRepoOnlyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
|
||||||
console.log('µBlock> readRepoOnlyAsset("%s") / onRepoMetaReady(): repo has newer version', path);
|
getTextFileFromURL(repositoryURL, onRepoFileLoaded, onRepoFileError);
|
||||||
getTextFileFromURL(repositoryURL, onRepoFileLoaded, onRepoFileError);
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asset unchanged: load from cache
|
// Load from cache
|
||||||
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -813,39 +760,29 @@ var readExternalAsset = function(path, callback) {
|
||||||
reportBack(this.responseText);
|
reportBack(this.responseText);
|
||||||
};
|
};
|
||||||
|
|
||||||
var onExternalFileError2 = function() {
|
var onExternalFileError = function() {
|
||||||
this.onload = this.onerror = null;
|
this.onload = this.onerror = null;
|
||||||
console.error('µBlock> readExternalAsset("%s") / onExternalFileError2()', path);
|
console.error('µBlock> readExternalAsset("%s") / onExternalFileError()', path);
|
||||||
reportBack('', 'Error');
|
|
||||||
};
|
|
||||||
|
|
||||||
var onExternalFileError1 = function() {
|
|
||||||
this.onload = this.onerror = null;
|
|
||||||
console.error('µBlock> readExternalAsset("%s") / onExternalFileError1()', path);
|
|
||||||
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
var entriesReady = function(entries) {
|
var onCacheMetaReady = function(entries) {
|
||||||
|
// Fetch from remote if:
|
||||||
|
// - Not in cache OR
|
||||||
|
//
|
||||||
|
// - Auto-update enabled AND in cache but obsolete
|
||||||
var timestamp = entries[path];
|
var timestamp = entries[path];
|
||||||
// In cache
|
var obsolete = Date.now() - exports.autoUpdateDelay;
|
||||||
if ( typeof timestamp === 'number' ) {
|
if ( typeof timestamp !== 'number' || (exports.autoUpdate && timestamp <= obsolete) ) {
|
||||||
// Obsolete?
|
getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError);
|
||||||
if ( exports.autoUpdate ) {
|
|
||||||
var obsolete = Date.now() - exports.autoUpdateDelay;
|
|
||||||
if ( timestamp <= obsolete ) {
|
|
||||||
getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Not obsolete
|
|
||||||
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Not in cache
|
|
||||||
getTextFileFromURL(path, onExternalFileLoaded, onExternalFileError2);
|
// In cache
|
||||||
|
cachedAssetsManager.load(path, onCachedContentLoaded, onCachedContentError);
|
||||||
};
|
};
|
||||||
|
|
||||||
cachedAssetsManager.entries(entriesReady);
|
cachedAssetsManager.entries(onCacheMetaReady);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -943,6 +880,10 @@ exports.get = function(path, callback) {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
exports.getLocal = readLocalFile;
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
exports.put = function(path, content, callback) {
|
exports.put = function(path, content, callback) {
|
||||||
cachedAssetsManager.save(path, content, callback);
|
cachedAssetsManager.save(path, content, callback);
|
||||||
};
|
};
|
||||||
|
|
|
@ -46,7 +46,7 @@ return {
|
||||||
allowedRequestCount: 0
|
allowedRequestCount: 0
|
||||||
},
|
},
|
||||||
|
|
||||||
updateAssetsEvery: 2 * 24 * 60 * 60 * 1000,
|
updateAssetsEvery: 47 * 60 * 60 * 1000,
|
||||||
projectServerRoot: 'https://raw.githubusercontent.com/gorhill/uBlock/master/',
|
projectServerRoot: 'https://raw.githubusercontent.com/gorhill/uBlock/master/',
|
||||||
userFiltersPath: 'assets/user/filters.txt',
|
userFiltersPath: 'assets/user/filters.txt',
|
||||||
|
|
||||||
|
|
44
js/start.js
44
js/start.js
|
@ -32,20 +32,42 @@
|
||||||
// Automatic update of non-user assets
|
// Automatic update of non-user assets
|
||||||
// https://github.com/gorhill/httpswitchboard/issues/334
|
// https://github.com/gorhill/httpswitchboard/issues/334
|
||||||
|
|
||||||
(function() {
|
µBlock.updater = (function() {
|
||||||
var µb = µBlock;
|
|
||||||
|
|
||||||
var jobCallback = function() {
|
/******************************************************************************/
|
||||||
if ( µb.userSettings.autoUpdate !== true ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// TODO: need smarter update, currently blindly reloading all.
|
|
||||||
µb.loadUpdatableAssets(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
µb.asyncJobs.add('autoUpdateAssets', null, jobCallback, µb.updateAssetsEvery, true);
|
var µb = µBlock;
|
||||||
})();
|
var bufferTime = 0 * 60 * 1000;
|
||||||
|
var exports = {};
|
||||||
|
|
||||||
|
var jobCallback = function() {
|
||||||
|
if ( µb.userSettings.autoUpdate !== true ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// TODO: need smarter update, currently blindly reloading all.
|
||||||
|
µb.loadUpdatableAssets(true);
|
||||||
|
};
|
||||||
|
|
||||||
// https://www.youtube.com/watch?v=cIrGQD84F1g
|
// https://www.youtube.com/watch?v=cIrGQD84F1g
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
exports.restart = function() {
|
||||||
|
µb.asyncJobs.add(
|
||||||
|
'autoUpdateAssets',
|
||||||
|
null,
|
||||||
|
jobCallback,
|
||||||
|
µb.updateAssetsEvery - bufferTime,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.restart();
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
return exports;
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
|
@ -446,6 +446,11 @@
|
||||||
this.assets.autoUpdateDelay = this.updateAssetsEvery;
|
this.assets.autoUpdateDelay = this.updateAssetsEvery;
|
||||||
this.loadPublicSuffixList();
|
this.loadPublicSuffixList();
|
||||||
this.loadUbiquitousBlacklists();
|
this.loadUbiquitousBlacklists();
|
||||||
|
|
||||||
|
// It could be a manual update, so we reset the auto-updater
|
||||||
|
if ( update ) {
|
||||||
|
this.updater.restart();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
Loading…
Reference in New Issue