workaround for USO double-firing of install/update events

This commit is contained in:
tophf 2017-11-08 06:52:51 +03:00
parent d053d78c8f
commit 684f445957

View File

@ -5,13 +5,13 @@ const FIREFOX = !chrome.app;
const VIVALDI = chrome.app && /Vivaldi/.test(navigator.userAgent); const VIVALDI = chrome.app && /Vivaldi/.test(navigator.userAgent);
const OPERA = chrome.app && /OPR/.test(navigator.userAgent); const OPERA = chrome.app && /OPR/.test(navigator.userAgent);
document.addEventListener('stylishUpdate', onUpdateClicked); document.addEventListener('stylishUpdate', onClick);
document.addEventListener('stylishUpdateChrome', onUpdateClicked); document.addEventListener('stylishUpdateChrome', onClick);
document.addEventListener('stylishUpdateOpera', onUpdateClicked); document.addEventListener('stylishUpdateOpera', onClick);
document.addEventListener('stylishInstall', onInstallClicked); document.addEventListener('stylishInstall', onClick);
document.addEventListener('stylishInstallChrome', onInstallClicked); document.addEventListener('stylishInstallChrome', onClick);
document.addEventListener('stylishInstallOpera', onInstallClicked); document.addEventListener('stylishInstallOpera', onClick);
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// orphaned content script check // orphaned content script check
@ -191,32 +191,45 @@ function sendEvent(type, detail = null) {
} }
function onInstallClicked() { function onClick(event) {
if (!orphanCheck || !orphanCheck()) { if (onClick.processing || !orphanCheck || !orphanCheck()) {
return; return;
} }
getResource(getMeta('stylish-description')) onClick.processing = true;
(event.type.includes('Update') ? onUpdate() : onInstall())
.then(done, done);
function done() {
setTimeout(() => {
onClick.processing = false;
});
}
}
function onInstall() {
return getResource(getMeta('stylish-description'))
.then(name => saveStyleCode('styleInstall', name)) .then(name => saveStyleCode('styleInstall', name))
.then(() => getResource(getMeta('stylish-install-ping-url-chrome'))); .then(() => getResource(getMeta('stylish-install-ping-url-chrome')));
} }
function onUpdateClicked() { function onUpdate() {
if (!orphanCheck || !orphanCheck()) { return new Promise((resolve, reject) => {
return; chrome.runtime.sendMessage({
} method: 'getStyles',
chrome.runtime.sendMessage({ url: getMeta('stylish-id-url') || location.href,
method: 'getStyles', }, ([style]) => {
url: getMeta('stylish-id-url') || location.href, saveStyleCode('styleUpdate', style.name, {id: style.id})
}, ([style]) => { .then(resolve, reject);
saveStyleCode('styleUpdate', style.name, {id: style.id}); });
}); });
} }
function saveStyleCode(message, name, addProps) { function saveStyleCode(message, name, addProps) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
if (!confirm(chrome.i18n.getMessage(message, [name]))) { if (!confirm(chrome.i18n.getMessage(message, [name]))) {
reject();
return; return;
} }
enableUpdateButton(false); enableUpdateButton(false);
@ -335,13 +348,13 @@ function orphanCheck() {
} }
// we're orphaned due to an extension update // we're orphaned due to an extension update
// we can detach event listeners // we can detach event listeners
document.removeEventListener('stylishUpdate', onUpdateClicked); document.removeEventListener('stylishUpdate', onClick);
document.removeEventListener('stylishUpdateChrome', onUpdateClicked); document.removeEventListener('stylishUpdateChrome', onClick);
document.removeEventListener('stylishUpdateOpera', onUpdateClicked); document.removeEventListener('stylishUpdateOpera', onClick);
document.removeEventListener('stylishInstall', onInstallClicked); document.removeEventListener('stylishInstall', onClick);
document.removeEventListener('stylishInstallChrome', onInstallClicked); document.removeEventListener('stylishInstallChrome', onClick);
document.removeEventListener('stylishInstallOpera', onInstallClicked); document.removeEventListener('stylishInstallOpera', onClick);
// we can't detach chrome.runtime.onMessage because it's no longer connected internally // we can't detach chrome.runtime.onMessage because it's no longer connected internally
// we can destroy global functions in this context to free up memory // we can destroy global functions in this context to free up memory
@ -350,8 +363,9 @@ function orphanCheck() {
'getMeta', 'getMeta',
'getResource', 'getResource',
'onDOMready', 'onDOMready',
'onInstallClicked', 'onClick',
'onUpdateClicked', 'onInstall',
'onUpdate',
'orphanCheck', 'orphanCheck',
'saveStyleCode', 'saveStyleCode',
'sendEvent', 'sendEvent',