From 151805f1bbae1929c7fc69bb0a13902c0c5cd899 Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 16 Nov 2020 00:38:20 +0300 Subject: [PATCH] simplify getStylesViaXhr --- background/style-via-webrequest.js | 2 +- content/apply.js | 28 ++++++++-------------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/background/style-via-webrequest.js b/background/style-via-webrequest.js index a70e9a2a..d8016ddd 100644 --- a/background/style-via-webrequest.js +++ b/background/style-via-webrequest.js @@ -89,7 +89,7 @@ CHROME && (async () => { res = true; responseHeaders.push({ name: 'Set-Cookie', - value: `${chrome.runtime.id}=${prefs.get(idOff) ? 1 : 0}${id}`, + value: `${chrome.runtime.id}=${id}`, }); // Allow cookies in CSP sandbox (known case: raw github urls) if (csp) { diff --git a/content/apply.js b/content/apply.js index 7ab275f6..e2bad4e2 100644 --- a/content/apply.js +++ b/content/apply.js @@ -59,8 +59,7 @@ self.INJECTED !== 1 && (() => { if (STYLE_VIA_API) { await API.styleViaAPI({method: 'styleApply'}); } else { - const blobId = chrome.app && getXhrBlobId(); - const styles = blobId && getStylesViaXhr(blobId) || + const styles = chrome.app && !chrome.tabs && getStylesViaXhr() || await API.getSectionsByUrl(getMatchUrl(), null, true); if (styles.disableAll) { delete styles.disableAll; @@ -70,27 +69,16 @@ self.INJECTED !== 1 && (() => { } } - function getXhrBlobId() { + function getStylesViaXhr() { try { - const {cookie} = document; // may throw in sandboxed frames - return new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).exec(cookie)[2]; - } catch (e) {} - } - - function getStylesViaXhr(data) { - try { - const disableAll = data[0] === '1'; - const url = 'blob:' + chrome.runtime.getURL(data.slice(1)); + const blobId = document.cookie.split(chrome.runtime.id + '=')[1].split(';')[0]; + const url = 'blob:' + chrome.runtime.getURL(blobId); document.cookie = `${chrome.runtime.id}=1; max-age=0`; // remove our cookie - let res; - if (!disableAll) { // when disabled, will get the styles asynchronously, no rush - const xhr = new XMLHttpRequest(); - xhr.open('GET', url, false); // synchronous - xhr.send(); - res = JSON.parse(xhr.response); - } + const xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); // synchronous + xhr.send(); URL.revokeObjectURL(url); - return res; + return JSON.parse(xhr.response); } catch (e) {} }