diff --git a/content/install-hook-userstyles.js b/content/install-hook-userstyles.js index d21e4d2f..32835ece 100644 --- a/content/install-hook-userstyles.js +++ b/content/install-hook-userstyles.js @@ -130,8 +130,18 @@ return; } onClick.processing = true; - (event.type.includes('Update') ? onUpdate() : onInstall()) - .then(done, done); + doInstall() + .then(() => { + if (!event.type.includes('Update')) { + // FIXME: sometimes the button is broken i.e. the button sends + // 'install' instead of 'update' event while the style is already + // install. + // This triggers an incorrect install count but we don't really care. + return getResource(getMeta('stylish-install-ping-url-chrome')); + } + }) + .catch(console.error) + .then(done); function done() { setTimeout(() => { onClick.processing = false; @@ -139,26 +149,26 @@ } } - - function onInstall() { - return getResource(getMeta('stylish-description')) - .then(name => saveStyleCode('styleInstall', name)) - .then(() => getResource(getMeta('stylish-install-ping-url-chrome'))); - } - - - function onUpdate() { - return new Promise((resolve, reject) => { - API.findStyle({ - md5Url: getMeta('stylish-md5-url') || location.href - }, true).then(style => { - saveStyleCode('styleUpdate', style.name, {id: style.id}) - .then(resolve, reject); + function doInstall() { + let oldStyle; + return API.findStyle({ + md5Url: getMeta('stylish-md5-url') || location.href + }, true) + .then(_oldStyle => { + oldStyle = _oldStyle; + return oldStyle ? + oldStyle.name : + getResource(getMeta('stylish-description')); + }) + .then(name => { + const props = {}; + if (oldStyle) { + props.id = oldStyle.id; + } + return saveStyleCode(oldStyle ? 'styleUpdate' : 'styleInstall', name, props); }); - }); } - function saveStyleCode(message, name, addProps = {}) { const isNew = message === 'styleInstall'; const needsConfirmation = isNew || !saveStyleCode.confirmed;