Fix: don't install duplicate styles on USO

This commit is contained in:
eight 2019-05-20 19:51:30 +08:00
parent 8906cbbb1a
commit a194f7eb26

View File

@ -130,8 +130,18 @@
return; return;
} }
onClick.processing = true; onClick.processing = true;
(event.type.includes('Update') ? onUpdate() : onInstall()) doInstall()
.then(done, done); .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() { function done() {
setTimeout(() => { setTimeout(() => {
onClick.processing = false; onClick.processing = false;
@ -139,26 +149,26 @@
} }
} }
function doInstall() {
function onInstall() { let oldStyle;
return getResource(getMeta('stylish-description')) return API.findStyle({
.then(name => saveStyleCode('styleInstall', name)) md5Url: getMeta('stylish-md5-url') || location.href
.then(() => getResource(getMeta('stylish-install-ping-url-chrome'))); }, true)
} .then(_oldStyle => {
oldStyle = _oldStyle;
return oldStyle ?
function onUpdate() { oldStyle.name :
return new Promise((resolve, reject) => { getResource(getMeta('stylish-description'));
API.findStyle({ })
md5Url: getMeta('stylish-md5-url') || location.href .then(name => {
}, true).then(style => { const props = {};
saveStyleCode('styleUpdate', style.name, {id: style.id}) if (oldStyle) {
.then(resolve, reject); props.id = oldStyle.id;
}
return saveStyleCode(oldStyle ? 'styleUpdate' : 'styleInstall', name, props);
}); });
});
} }
function saveStyleCode(message, name, addProps = {}) { function saveStyleCode(message, name, addProps = {}) {
const isNew = message === 'styleInstall'; const isNew = message === 'styleInstall';
const needsConfirmation = isNew || !saveStyleCode.confirmed; const needsConfirmation = isNew || !saveStyleCode.confirmed;