From b0ed85c5ead9a4949848b49ee287ec26b3c7d0fc Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 23 Aug 2021 16:21:40 +0300 Subject: [PATCH] use second metablock's `@updateURL` in USO-archive styles fixes #1323 --- background/update-manager.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/background/update-manager.js b/background/update-manager.js index 023006ef..9797c115 100644 --- a/background/update-manager.js +++ b/background/update-manager.js @@ -164,14 +164,10 @@ const updateMan = (() => { } async function updateToUSOArchive(url, req) { - // UserCSS metadata may be embedded in the original USO style so let's use its updateURL - const [meta2] = req.response.replace(RX_META, '').match(RX_META) || []; - if (meta2 && meta2.includes('@updateURL')) { - const {updateUrl} = await API.usercss.buildMeta({sourceCode: meta2}).catch(() => ({})); - if (updateUrl) { - url = updateUrl; - req = await tryDownload(url, RH_ETAG); - } + const m2 = getUsoEmbeddedMeta(req.response); + if (m2) { + url = (await m2).updateUrl; + req = await tryDownload(url, RH_ETAG); } const json = await API.usercss.buildMeta({ id, @@ -209,13 +205,21 @@ const updateMan = (() => { } async function updateUsercss() { - if (style.etag && style.etag === await downloadEtag()) { + let oldVer = ucd.version; + let {etag: oldEtag, updateUrl} = style; + let m2 = URLS.extractUsoArchiveId(updateUrl) && getUsoEmbeddedMeta(); + if (m2 && (m2 = await m2).updateUrl) { + updateUrl = m2.updateUrl; + oldVer = m2.usercssData.version || '0'; + oldEtag = ''; + } + if (oldEtag && oldEtag === await downloadEtag()) { return Promise.reject(STATES.SAME_CODE); } // TODO: when sourceCode is > 100kB use http range request(s) for version check - const {headers: {etag}, response} = await tryDownload(style.updateUrl, RH_ETAG); - const json = await API.usercss.buildMeta({sourceCode: response, etag}); - const delta = compareVersion(json.usercssData.version, ucd.version); + const {headers: {etag}, response} = await tryDownload(updateUrl, RH_ETAG); + const json = await API.usercss.buildMeta({sourceCode: response, etag, updateUrl}); + const delta = compareVersion(json.usercssData.version, oldVer); let err; if (!delta && !ignoreDigest) { // re-install is invalid in a soft upgrade @@ -285,6 +289,12 @@ const updateMan = (() => { } } + /** UserCSS metadata may be embedded in the original USO style so let's use its updateURL */ + function getUsoEmbeddedMeta(code = style.sourceCode) { + const m = code.includes('@updateURL') && code.replace(RX_META, '').match(RX_META); + return m && API.usercss.buildMeta({sourceCode: m[0]}).catch(() => null); + } + function getVarOptByName(varDef, name) { return varDef.options.find(o => o.name === name); }