Fix USO update button (#555)

* Fix USO update button

* Fix background & search-results

* Prevent JSON parse error

* Get & set currentMd5 outside of condition

* fetchMd5 with API without options

* Only update originalMd5 for valid styles

* Always set currentMd5 & add a delay before firing USO event
This commit is contained in:
Rob Garrison 2018-11-18 07:30:47 -06:00 committed by GitHub
parent b41a39d36e
commit 7261a074cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 22 deletions

View File

@ -156,7 +156,14 @@
} }
// USO can't handle POST requests for style json // USO can't handle POST requests for style json
return download(style.updateUrl, {body: null}) return download(style.updateUrl, {body: null})
.then(text => tryJSONparse(text)); .then(text => {
const style = tryJSONparse(text);
if (style) {
// USO may not provide a correctly updated originalMd5 (#555)
style.originalMd5 = md5;
}
return style;
});
}); });
} }

View File

@ -18,6 +18,7 @@
}); });
let gotBody = false; let gotBody = false;
let currentMd5;
new MutationObserver(observeDOM).observe(document.documentElement, { new MutationObserver(observeDOM).observe(document.documentElement, {
childList: true, childList: true,
subtree: true, subtree: true,
@ -30,9 +31,12 @@
gotBody = true; gotBody = true;
// TODO: remove the following statement when USO pagination title is fixed // TODO: remove the following statement when USO pagination title is fixed
document.title = document.title.replace(/^(\d+)&\w+=/, '#$1: '); document.title = document.title.replace(/^(\d+)&\w+=/, '#$1: ');
API.findStyle({ const md5Url = getMeta('stylish-md5-url') || location.href;
md5Url: getMeta('stylish-md5-url') || location.href Promise.all([
}).then(checkUpdatability); API.findStyle({md5Url}),
getResource(md5Url)
])
.then(checkUpdatability);
} }
if (document.getElementById('install_button')) { if (document.getElementById('install_button')) {
onDOMready().then(() => { onDOMready().then(() => {
@ -66,11 +70,12 @@
return jsonUrl + (paramsMissing ? textUrl.replace(/^[^?]+/, '') : ''); return jsonUrl + (paramsMissing ? textUrl.replace(/^[^?]+/, '') : '');
} }
function checkUpdatability(installedStyle) { function checkUpdatability([installedStyle, md5]) {
// TODO: remove the following statement when USO is fixed // TODO: remove the following statement when USO is fixed
document.dispatchEvent(new CustomEvent('stylusFixBuggyUSOsettings', { document.dispatchEvent(new CustomEvent('stylusFixBuggyUSOsettings', {
detail: installedStyle && installedStyle.updateUrl, detail: installedStyle && installedStyle.updateUrl,
})); }));
currentMd5 = md5;
if (!installedStyle) { if (!installedStyle) {
sendEvent({type: 'styleCanBeInstalledChrome'}); sendEvent({type: 'styleCanBeInstalledChrome'});
return; return;
@ -78,11 +83,7 @@
const isCustomizable = /\?/.test(installedStyle.updateUrl); const isCustomizable = /\?/.test(installedStyle.updateUrl);
const md5Url = getMeta('stylish-md5-url'); const md5Url = getMeta('stylish-md5-url');
if (md5Url && installedStyle.md5Url && installedStyle.originalMd5) { if (md5Url && installedStyle.md5Url && installedStyle.originalMd5) {
getResource(md5Url).then(md5 => { reportUpdatable(isCustomizable || md5 !== installedStyle.originalMd5);
reportUpdatable(
isCustomizable ||
md5 !== installedStyle.originalMd5);
});
} else { } else {
getStyleJson().then(json => { getStyleJson().then(json => {
reportUpdatable( reportUpdatable(
@ -93,14 +94,17 @@
} }
function reportUpdatable(isUpdatable) { function reportUpdatable(isUpdatable) {
sendEvent({ // USO doesn't bind these listeners immediately
type: isUpdatable setTimeout(() => {
? 'styleCanBeUpdatedChrome' sendEvent({
: 'styleAlreadyInstalledChrome', type: isUpdatable
detail: { ? 'styleCanBeUpdatedChrome'
updateUrl: installedStyle.updateUrl : 'styleAlreadyInstalledChrome',
}, detail: {
}); updateUrl: installedStyle.updateUrl
},
});
}, 300);
} }
} }
@ -155,7 +159,7 @@
} }
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;
if (needsConfirmation && !confirm(chrome.i18n.getMessage(message, [name]))) { if (needsConfirmation && !confirm(chrome.i18n.getMessage(message, [name]))) {
@ -169,7 +173,8 @@
'https://github.com/openstyles/stylus/issues/195'); 'https://github.com/openstyles/stylus/issues/195');
return; return;
} }
return API.installStyle(Object.assign(json, addProps)) // Update originalMd5 since USO changed it (2018-11-11) to NOT match the current md5
return API.installStyle(Object.assign(json, addProps, {originalMd5: currentMd5}))
.then(style => { .then(style => {
if (!isNew && style.updateUrl.includes('?')) { if (!isNew && style.updateUrl.includes('?')) {
enableUpdateButton(true); enableUpdateButton(true);

View File

@ -510,7 +510,6 @@ window.addEventListener('showStyles:done', function _() {
const installButton = $('.search-result-install', entry); const installButton = $('.search-result-install', entry);
installButton.onclick = onInstallClicked; installButton.onclick = onInstallClicked;
if ((result.style_settings || []).length > 0) { if ((result.style_settings || []).length > 0) {
// Style has customizations // Style has customizations
installButton.classList.add('customize'); installButton.classList.add('customize');
@ -552,11 +551,13 @@ window.addEventListener('showStyles:done', function _() {
Promise.all([ Promise.all([
fetchStyleJson(result), fetchStyleJson(result),
fetchStyleSettings(result), fetchStyleSettings(result),
API.download({url: UPDATE_URL.replace('%', result.id)})
]) ])
.then(([style, settings]) => { .then(([style, settings, md5]) => {
pingback(result); pingback(result);
// show a 'config-on-homepage' icon in the popup // show a 'config-on-homepage' icon in the popup
style.updateUrl += settings.length ? '?' : ''; style.updateUrl += settings.length ? '?' : '';
style.originalMd5 = md5;
return API.installStyle(style); return API.installStyle(style);
}) })
.catch(reason => { .catch(reason => {