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
*/
function getStyleURL() {
const url = getMeta('stylish-code-chrome');
// TODO: remove when USO is fixed
const directUrl = getMeta('stylish-update-url');
if (directUrl.includes('?') && !url.includes('?')) {
/* get custom settings from the update url */
return Object.assign(new URL(url), {
search: (new URL(directUrl)).search
}).href;
}
return url;
const textUrl = getMeta('stylish-update-url') || '';
const jsonUrl = getMeta('stylish-code-chrome') ||
textUrl.replace(/styles\/(\d+)\/[^?]*/, 'styles/chrome/$1.json');
const paramsMissing = !jsonUrl.includes('?') && textUrl.includes('?');
return jsonUrl + (paramsMissing ? textUrl.replace(/^[^?]+/, '') : '');
}
function checkUpdatability([installedStyle]) {
@ -154,9 +149,9 @@ function checkUpdatability([installedStyle]) {
reportUpdatable(md5 !== installedStyle.originalMd5);
});
} else {
getResource(getStyleURL()).then(code => {
reportUpdatable(code === null ||
!styleSectionsEqual(JSON.parse(code), installedStyle));
getStyleJson().then(json => {
reportUpdatable(!json ||
!styleSectionsEqual(json, installedStyle));
});
}
@ -233,9 +228,14 @@ function saveStyleCode(message, name, addProps) {
return;
}
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(
Object.assign(JSON.parse(code), addProps, {
Object.assign(json, addProps, {
method: 'saveStyle',
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}) {
if (!a || !b) {
return undefined;