improve orphan check + cosmetics
This commit is contained in:
parent
685bf1fa3e
commit
b88a978843
|
@ -259,7 +259,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function orphanCheck() {
|
function orphanCheck() {
|
||||||
if (tryCatch(() => chrome.i18n.getUILanguage())) return;
|
if (chrome.runtime.id) return;
|
||||||
// In Chrome content script is orphaned on an extension update/reload
|
// In Chrome content script is orphaned on an extension update/reload
|
||||||
// so we need to detach event listeners
|
// so we need to detach event listeners
|
||||||
window.removeEventListener(orphanEventId, orphanCheck, true);
|
window.removeEventListener(orphanEventId, orphanCheck, true);
|
||||||
|
|
|
@ -174,7 +174,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function orphanCheck() {
|
function orphanCheck() {
|
||||||
if (chrome.i18n) return true;
|
if (chrome.runtime.id) return true;
|
||||||
removeEventListener(orphanEventId, orphanCheck, true);
|
removeEventListener(orphanEventId, orphanCheck, true);
|
||||||
removeEventListener('click', onClick, true);
|
removeEventListener('click', onClick, true);
|
||||||
removeEventListener('change', onChange);
|
removeEventListener('change', onChange);
|
||||||
|
@ -185,42 +185,46 @@
|
||||||
})();
|
})();
|
||||||
|
|
||||||
function inPageContext(eventId, eventIdHost, styleId, apiUrl) {
|
function inPageContext(eventId, eventIdHost, styleId, apiUrl) {
|
||||||
window.isInstalled = true;
|
|
||||||
const {dispatchEvent, CustomEvent, removeEventListener} = window;
|
const {dispatchEvent, CustomEvent, removeEventListener} = window;
|
||||||
const apply = Map.call.bind(Map.apply);
|
const apply = Map.call.bind(Map.apply);
|
||||||
const CR = chrome.runtime;
|
const CR = chrome.runtime;
|
||||||
const {sendMessage} = CR;
|
const SEND = 'sendMessage';
|
||||||
const RP = Response.prototype;
|
const RP = Response.prototype;
|
||||||
const origJson = RP.json;
|
const ORIG = {json: RP.json, [SEND]: CR[SEND]};
|
||||||
let done, vars;
|
let done, orphaned, vars;
|
||||||
CR.sendMessage = function (id, msg, opts, cb = opts) {
|
CR[SEND] = ovrSend;
|
||||||
if (id === 'fjnbnpbmkenffdnngjfgmeleoegfcffe' &&
|
RP.json = ovrJson;
|
||||||
|
window.isInstalled = true;
|
||||||
|
addEventListener(eventId, onCommand, true);
|
||||||
|
function ovrSend(id, msg, opts, cb = opts) {
|
||||||
|
if (!orphaned &&
|
||||||
|
id === 'fjnbnpbmkenffdnngjfgmeleoegfcffe' &&
|
||||||
msg && msg.type === 'deleteStyle' &&
|
msg && msg.type === 'deleteStyle' &&
|
||||||
typeof cb === 'function') {
|
typeof cb === 'function') {
|
||||||
cb(true);
|
cb(true);
|
||||||
} else {
|
} else {
|
||||||
return sendMessage(...arguments);
|
return ORIG[SEND](...arguments);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
RP.json = async function () {
|
async function ovrJson() {
|
||||||
const res = await apply(origJson, this, arguments);
|
const res = await apply(ORIG.json, this, arguments);
|
||||||
try {
|
try {
|
||||||
if (!done && this.url === apiUrl) {
|
if (!done && this.url === apiUrl) {
|
||||||
RP.json = origJson;
|
if (RP.json === ovrJson) RP.json = ORIG.json;
|
||||||
done = true; // will be used if called by another script that saved our RP.json hook
|
done = true;
|
||||||
send(res);
|
send(res);
|
||||||
setVars(res);
|
setVars(res);
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
return res;
|
return res;
|
||||||
};
|
}
|
||||||
addEventListener(eventId, onCommand, true);
|
|
||||||
function onCommand(e) {
|
function onCommand(e) {
|
||||||
if (e.detail === 'quit') {
|
if (e.detail === 'quit') {
|
||||||
removeEventListener(eventId, onCommand, true);
|
removeEventListener(eventId, onCommand, true);
|
||||||
CR.sendMessage = sendMessage;
|
// We can restore the hooks only if another script didn't modify them
|
||||||
RP.json = origJson;
|
if (CR[SEND] === ovrSend) CR[SEND] = ovrSend;
|
||||||
done = true;
|
if (RP.json === ovrJson) RP.json = ORIG.json;
|
||||||
|
done = orphaned = true;
|
||||||
} else if (/^vars:/.test(e.detail)) {
|
} else if (/^vars:/.test(e.detail)) {
|
||||||
vars = JSON.parse(e.detail.slice(5));
|
vars = JSON.parse(e.detail.slice(5));
|
||||||
} else if (e.relatedTarget) {
|
} else if (e.relatedTarget) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user