1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-24 11:22:44 +01:00
This commit is contained in:
gorhill 2017-03-05 12:54:47 -05:00
parent d7c1f2f919
commit 5015826546
3 changed files with 54 additions and 33 deletions

View File

@ -64,10 +64,8 @@ var fireNotification = function(topic, details) {
/******************************************************************************/ /******************************************************************************/
var getTextFileFromURL = function(url, onLoad, onError) { api.fetchText = function(url, onLoad, onError) {
if ( reIsExternalPath.test(url) === false ) { var actualUrl = reIsExternalPath.test(url) ? url : vAPI.getURL(url);
url = vAPI.getURL(url);
}
if ( typeof onError !== 'function' ) { if ( typeof onError !== 'function' ) {
onError = onLoad; onError = onLoad;
@ -77,28 +75,34 @@ var getTextFileFromURL = function(url, onLoad, onError) {
var onResponseReceived = function() { var onResponseReceived = function() {
this.onload = this.onerror = this.ontimeout = null; this.onload = this.onerror = this.ontimeout = null;
// xhr for local files gives status 0, but actually succeeds // xhr for local files gives status 0, but actually succeeds
var status = this.status || 200; var details = {
if ( status < 200 || status >= 300 ) { url: url,
return onError.call(this); content: '',
statusCode: this.status || 200,
statusText: this.statusText || ''
};
if ( details.statusCode < 200 || details.statusCode >= 300 ) {
return onError.call(null, details);
} }
// consider an empty result to be an error // consider an empty result to be an error
if ( stringIsNotEmpty(this.responseText) === false ) { if ( stringIsNotEmpty(this.responseText) === false ) {
return onError.call(this); return onError.call(null, details);
} }
// we never download anything else than plain text: discard if response // we never download anything else than plain text: discard if response
// appears to be a HTML document: could happen when server serves // appears to be a HTML document: could happen when server serves
// some kind of error page I suppose // some kind of error page I suppose
var text = this.responseText.trim(); var text = this.responseText.trim();
if ( text.startsWith('<') && text.endsWith('>') ) { if ( text.startsWith('<') && text.endsWith('>') ) {
return onError.call(this); return onError.call(null, details);
} }
return onLoad.call(this); details.content = this.responseText;
return onLoad.call(null, details);
}; };
var onErrorReceived = function() { var onErrorReceived = function() {
this.onload = this.onerror = this.ontimeout = null; this.onload = this.onerror = this.ontimeout = null;
µBlock.logger.writeOne('', 'error', errorCantConnectTo.replace('{{msg}}', url)); µBlock.logger.writeOne('', 'error', errorCantConnectTo.replace('{{msg}}', actualUrl));
onError.call(this); onError.call(null, { url: url, content: '' });
}; };
// Be ready for thrown exceptions: // Be ready for thrown exceptions:
@ -106,7 +110,7 @@ var getTextFileFromURL = function(url, onLoad, onError) {
// `file:///` on Chromium 40 results in an exception being thrown. // `file:///` on Chromium 40 results in an exception being thrown.
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
try { try {
xhr.open('get', url, true); xhr.open('get', actualUrl, true);
xhr.timeout = µBlock.hiddenSettings.assetFetchTimeout * 1000 || 30000; xhr.timeout = µBlock.hiddenSettings.assetFetchTimeout * 1000 || 30000;
xhr.onload = onResponseReceived; xhr.onload = onResponseReceived;
xhr.onerror = onErrorReceived; xhr.onerror = onErrorReceived;
@ -405,10 +409,10 @@ var getAssetSourceRegistry = function(callback) {
// First-install case. // First-install case.
var createRegistry = function() { var createRegistry = function() {
getTextFileFromURL( api.fetchText(
µBlock.assetsBootstrapLocation || 'assets/assets.json', µBlock.assetsBootstrapLocation || 'assets/assets.json',
function() { function(details) {
updateAssetSourceRegistry(this.responseText, true); updateAssetSourceRegistry(details.content, true);
registryReady(); registryReady();
} }
); );
@ -757,21 +761,21 @@ api.get = function(assetKey, options, callback) {
if ( !contentURL ) { if ( !contentURL ) {
return reportBack('', 'E_NOTFOUND'); return reportBack('', 'E_NOTFOUND');
} }
getTextFileFromURL(contentURL, onContentLoaded, onContentNotLoaded); api.fetchText(contentURL, onContentLoaded, onContentNotLoaded);
}; };
var onContentLoaded = function() { var onContentLoaded = function(details) {
if ( stringIsNotEmpty(this.responseText) === false ) { if ( stringIsNotEmpty(details.content) === false ) {
onContentNotLoaded(); onContentNotLoaded();
return; return;
} }
if ( reIsExternalPath.test(contentURL) && options.dontCache !== true ) { if ( reIsExternalPath.test(contentURL) && options.dontCache !== true ) {
assetCacheWrite(assetKey, { assetCacheWrite(assetKey, {
content: this.responseText, content: details.content,
url: contentURL url: contentURL
}); });
} }
reportBack(this.responseText); reportBack(details.content);
}; };
var onCachedContentLoaded = function(details) { var onCachedContentLoaded = function(details) {
@ -811,23 +815,23 @@ var getRemote = function(assetKey, callback) {
callback(details); callback(details);
}; };
var onRemoteContentLoaded = function() { var onRemoteContentLoaded = function(details) {
if ( stringIsNotEmpty(this.responseText) === 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();
return; return;
} }
assetCacheWrite(assetKey, { assetCacheWrite(assetKey, {
content: this.responseText, content: details.content,
url: contentURL url: contentURL
}); });
registerAssetSource(assetKey, { error: undefined }); registerAssetSource(assetKey, { error: undefined });
reportBack(this.responseText); reportBack(details.content);
}; };
var onRemoteContentError = function() { var onRemoteContentError = function(details) {
var text = this.statusText; var text = details.statusText;
if ( this.status === 0 ) { if ( details.statusCode === 0 ) {
text = 'network error'; text = 'network error';
} }
registerAssetSource(assetKey, { error: { time: Date.now(), error: text } }); registerAssetSource(assetKey, { error: { time: Date.now(), error: text } });
@ -841,7 +845,7 @@ var getRemote = function(assetKey, callback) {
if ( !contentURL ) { if ( !contentURL ) {
return reportBack('', 'E_NOTFOUND'); return reportBack('', 'E_NOTFOUND');
} }
getTextFileFromURL(contentURL, onRemoteContentLoaded, onRemoteContentError); api.fetchText(contentURL, onRemoteContentLoaded, onRemoteContentError);
}; };
getAssetSourceRegistry(function(registry) { getAssetSourceRegistry(function(registry) {

View File

@ -39,7 +39,8 @@ var µBlock = (function() { // jshint ignore:line
ignoreScriptInjectFilters: false, ignoreScriptInjectFilters: false,
manualUpdateAssetFetchPeriod: 2000, manualUpdateAssetFetchPeriod: 2000,
popupFontSize: 'unset', popupFontSize: 'unset',
suspendTabsUntilReady: false suspendTabsUntilReady: false,
userResourcesLocation: 'unset'
}; };
return { return {

View File

@ -818,17 +818,33 @@
/******************************************************************************/ /******************************************************************************/
µBlock.loadRedirectResources = function(callback) { µBlock.loadRedirectResources = function(callback) {
var µb = this; var µb = this,
content = '';
if ( typeof callback !== 'function' ) { if ( typeof callback !== 'function' ) {
callback = this.noopFunc; callback = this.noopFunc;
} }
var onDone = function() {
µb.redirectEngine.resourcesFromString(content);
callback();
};
var onUserResourcesLoaded = function(details) {
if ( details.content !== '' ) {
content += '\n\n' + details.content;
}
onDone();
};
var onResourcesLoaded = function(details) { var onResourcesLoaded = function(details) {
if ( details.content !== '' ) { if ( details.content !== '' ) {
µb.redirectEngine.resourcesFromString(details.content); content = details.content;
} }
callback(); if ( µb.hiddenSettings.userResourcesLocation === 'unset' ) {
return onDone();
}
µb.assets.fetchText(µb.hiddenSettings.userResourcesLocation, onUserResourcesLoaded);
}; };
this.assets.get('ublock-resources', onResourcesLoaded); this.assets.get('ublock-resources', onResourcesLoaded);