From f1836f399f711ca9b982fa548cd495d482a3ba94 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 21 Nov 2017 13:23:32 +0300 Subject: [PATCH] try to get style json on USO install page twice also report errors fixes #195 --- content/install.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/content/install.js b/content/install.js index 25bbd62b..a8b2073a 100644 --- a/content/install.js +++ b/content/install.js @@ -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;