1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

Fix spurious warning when force-reloading the dashboard

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/905

Additionally, minor changes to avoid spurious errors in
browser console.
This commit is contained in:
Raymond Hill 2020-02-24 09:59:35 -05:00
parent 7634604aa8
commit c17a9c8a93
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 40 additions and 33 deletions

View File

@ -230,10 +230,22 @@ vAPI.closePopup = function() {
vAPI.localStorage = {
start: function() {
if ( this.cache instanceof Promise ) { return this.cache; }
if ( this.cache instanceof Object ) { return Promise.resolve(); }
if ( this.promise !== undefined ) { return this.promise; }
this.promise = new Promise(resolve => {
const onChanged = (changes, area) => {
if (
area !== 'local' ||
changes instanceof Object === false ||
changes.localStorage instanceof Object === false
) {
return;
}
const newValue = changes.localStorage.newValue;
this.cache = newValue instanceof Object ? newValue : {};
};
this.cache = new Promise(resolve => {
browser.storage.local.get('localStorage', bin => {
this.cache = undefined;
if (
bin instanceof Object === false ||
bin.localStorage instanceof Object === false
@ -255,22 +267,15 @@ vAPI.localStorage = {
if ( this.cache instanceof Object === false ) {
this.cache = {};
}
this.promise = undefined;
browser.storage.onChanged.addListener((changes, area) => {
if (
area !== 'local' ||
changes instanceof Object === false ||
changes.localStorage instanceof Object === false
) {
return;
}
const newValue = changes.localStorage.newValue;
this.cache = newValue instanceof Object ? newValue : {};
});
resolve();
browser.storage.onChanged.addListener(onChanged);
self.addEventListener('beforeunload', ( ) => {
this.cache = undefined;
browser.storage.onChanged.removeListener(onChanged);
});
});
});
return this.promise;
return this.cache;
},
clear: function() {
this.cache = {};
@ -295,11 +300,11 @@ vAPI.localStorage = {
},
setItem: function(key, value = undefined) {
return this.start().then(( ) => {
if ( value === this.cache[key] ) { return; }
this.cache[key] = value;
return browser.storage.local.set({ localStorage: this.cache });
});
},
promise: undefined,
cache: undefined,
};

View File

@ -25,7 +25,8 @@
/******************************************************************************/
(( ) => {
{
// >>>>> start of local scope
/******************************************************************************/
@ -82,13 +83,7 @@ const discardUnsavedData = function(synchronous = false) {
});
};
const loadDashboardPanel = function(pane = '') {
if ( pane === '' ) {
vAPI.localStorage.getItemAsync('dashboardLastVisitedPane').then(value => {
loadDashboardPanel(value !== null ? value : 'settings.html');
});
return;
}
const loadDashboardPanel = function(pane, first) {
const tabButton = uDom(`[href="#${pane}"]`);
if ( !tabButton || tabButton.hasClass('selected') ) { return; }
const loadPane = ( ) => {
@ -98,6 +93,9 @@ const loadDashboardPanel = function(pane = '') {
uDom.nodeFromId('iframe').setAttribute('src', pane);
vAPI.localStorage.setItem('dashboardLastVisitedPane', pane);
};
if ( first ) {
return loadPane();
}
const r = discardUnsavedData();
if ( r === false ) { return; }
if ( r === true ) {
@ -122,18 +120,22 @@ vAPI.messaging.send('dashboard', {
});
resizeFrame();
loadDashboardPanel();
window.addEventListener('resize', resizeFrame);
uDom('.tabButton').on('click', onTabClickHandler);
vAPI.localStorage.getItemAsync('dashboardLastVisitedPane').then(value => {
loadDashboardPanel(value !== null ? value : 'settings.html', true);
// https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
window.addEventListener('beforeunload', ( ) => {
if ( discardUnsavedData(true) ) { return; }
event.preventDefault();
event.returnValue = '';
window.addEventListener('resize', resizeFrame);
uDom('.tabButton').on('click', onTabClickHandler);
// https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
window.addEventListener('beforeunload', ( ) => {
if ( discardUnsavedData(true) ) { return; }
event.preventDefault();
event.returnValue = '';
});
});
/******************************************************************************/
})();
// <<<<< end of local scope
}