simplify getStylesViaXhr
This commit is contained in:
parent
0816933932
commit
151805f1bb
|
@ -89,7 +89,7 @@ CHROME && (async () => {
|
||||||
res = true;
|
res = true;
|
||||||
responseHeaders.push({
|
responseHeaders.push({
|
||||||
name: 'Set-Cookie',
|
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)
|
// Allow cookies in CSP sandbox (known case: raw github urls)
|
||||||
if (csp) {
|
if (csp) {
|
||||||
|
|
|
@ -59,8 +59,7 @@ self.INJECTED !== 1 && (() => {
|
||||||
if (STYLE_VIA_API) {
|
if (STYLE_VIA_API) {
|
||||||
await API.styleViaAPI({method: 'styleApply'});
|
await API.styleViaAPI({method: 'styleApply'});
|
||||||
} else {
|
} else {
|
||||||
const blobId = chrome.app && getXhrBlobId();
|
const styles = chrome.app && !chrome.tabs && getStylesViaXhr() ||
|
||||||
const styles = blobId && getStylesViaXhr(blobId) ||
|
|
||||||
await API.getSectionsByUrl(getMatchUrl(), null, true);
|
await API.getSectionsByUrl(getMatchUrl(), null, true);
|
||||||
if (styles.disableAll) {
|
if (styles.disableAll) {
|
||||||
delete styles.disableAll;
|
delete styles.disableAll;
|
||||||
|
@ -70,27 +69,16 @@ self.INJECTED !== 1 && (() => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getXhrBlobId() {
|
function getStylesViaXhr() {
|
||||||
try {
|
try {
|
||||||
const {cookie} = document; // may throw in sandboxed frames
|
const blobId = document.cookie.split(chrome.runtime.id + '=')[1].split(';')[0];
|
||||||
return new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).exec(cookie)[2];
|
const url = 'blob:' + chrome.runtime.getURL(blobId);
|
||||||
} catch (e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStylesViaXhr(data) {
|
|
||||||
try {
|
|
||||||
const disableAll = data[0] === '1';
|
|
||||||
const url = 'blob:' + chrome.runtime.getURL(data.slice(1));
|
|
||||||
document.cookie = `${chrome.runtime.id}=1; max-age=0`; // remove our cookie
|
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();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', url, false); // synchronous
|
xhr.open('GET', url, false); // synchronous
|
||||||
xhr.send();
|
xhr.send();
|
||||||
res = JSON.parse(xhr.response);
|
|
||||||
}
|
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
return res;
|
return JSON.parse(xhr.response);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user