handle document.cookie exceptions in sandboxed frames

This commit is contained in:
tophf 2020-10-26 07:49:08 +03:00
parent 5ba111dce9
commit 92fcb02a57

View File

@ -59,7 +59,8 @@ self.INJECTED !== 1 && (() => {
if (STYLE_VIA_API) { if (STYLE_VIA_API) {
await API.styleViaAPI({method: 'styleApply'}); await API.styleViaAPI({method: 'styleApply'});
} else { } else {
const styles = chrome.app && getStylesViaXhr() || const blobId = chrome.app && getXhrBlobId();
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;
@ -69,24 +70,28 @@ self.INJECTED !== 1 && (() => {
} }
} }
function getStylesViaXhr() { function getXhrBlobId() {
if (new RegExp(`(^|\\s|;)${chrome.runtime.id}=\\s*([-\\w]+)\\s*(;|$)`).test(document.cookie)) { try {
const data = RegExp.$2; 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 disableAll = data[0] === '1';
const url = 'blob:' + chrome.runtime.getURL(data.slice(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; let res;
try { if (!disableAll) { // when disabled, will get the styles asynchronously, no rush
if (!disableAll) { // will get the styles asynchronously
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); res = JSON.parse(xhr.response);
} }
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
} catch (e) {}
return res; return res;
} } catch (e) {}
} }
function getMatchUrl() { function getMatchUrl() {