try to get style json on USO install page twice

also report errors
fixes #195
This commit is contained in:
tophf 2017-11-21 13:23:32 +03:00
parent 4e9e4d24b1
commit f1836f399f

View File

@ -127,16 +127,11 @@ new MutationObserver((mutations, observer) => {
we need to fix this URL using "stylish-update-url" meta key we need to fix this URL using "stylish-update-url" meta key
*/ */
function getStyleURL() { function getStyleURL() {
const url = getMeta('stylish-code-chrome'); const textUrl = getMeta('stylish-update-url') || '';
// TODO: remove when USO is fixed const jsonUrl = getMeta('stylish-code-chrome') ||
const directUrl = getMeta('stylish-update-url'); textUrl.replace(/styles\/(\d+)\/[^?]*/, 'styles/chrome/$1.json');
if (directUrl.includes('?') && !url.includes('?')) { const paramsMissing = !jsonUrl.includes('?') && textUrl.includes('?');
/* get custom settings from the update url */ return jsonUrl + (paramsMissing ? textUrl.replace(/^[^?]+/, '') : '');
return Object.assign(new URL(url), {
search: (new URL(directUrl)).search
}).href;
}
return url;
} }
function checkUpdatability([installedStyle]) { function checkUpdatability([installedStyle]) {
@ -154,9 +149,9 @@ function checkUpdatability([installedStyle]) {
reportUpdatable(md5 !== installedStyle.originalMd5); reportUpdatable(md5 !== installedStyle.originalMd5);
}); });
} else { } else {
getResource(getStyleURL()).then(code => { getStyleJson().then(json => {
reportUpdatable(code === null || reportUpdatable(!json ||
!styleSectionsEqual(JSON.parse(code), installedStyle)); !styleSectionsEqual(json, installedStyle));
}); });
} }
@ -233,9 +228,14 @@ function saveStyleCode(message, name, addProps) {
return; return;
} }
enableUpdateButton(false); enableUpdateButton(false);
getResource(getStyleURL()).then(code => { getStyleJson().then(json => {
if (!json) {
prompt(chrome.i18n.getMessage('styleInstallFailed', ''),
'https://github.com/openstyles/stylus/issues/195');
return;
}
chrome.runtime.sendMessage( chrome.runtime.sendMessage(
Object.assign(JSON.parse(code), addProps, { Object.assign(json, addProps, {
method: 'saveStyle', method: 'saveStyle',
reason: 'update', reason: 'update',
}), }),
@ -278,6 +278,18 @@ function getResource(url) {
} }
function getStyleJson() {
const url = getStyleURL();
return getResource(url).then(code => {
try {
return JSON.parse(code);
} catch (e) {
return fetch(url).then(r => r.json()).catch(() => null);
}
});
}
function styleSectionsEqual({sections: a}, {sections: b}) { function styleSectionsEqual({sections: a}, {sections: b}) {
if (!a || !b) { if (!a || !b) {
return undefined; return undefined;