cosmetics
This commit is contained in:
parent
a4cd6d16e2
commit
b2faf438b0
95
js/msg.js
95
js/msg.js
|
@ -2,8 +2,8 @@
|
||||||
/* global deepCopy getOwnTab URLS */ // not used in content scripts
|
/* global deepCopy getOwnTab URLS */ // not used in content scripts
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-expressions, no-var
|
// eslint-disable-next-line no-unused-expressions
|
||||||
var msg = window.INJECTED === 1 && window.msg || (() => {
|
window.INJECTED !== 1 && (() => {
|
||||||
promisifyChrome({
|
promisifyChrome({
|
||||||
runtime: ['sendMessage', 'getBackgroundPage'],
|
runtime: ['sendMessage', 'getBackgroundPage'],
|
||||||
tabs: ['sendMessage', 'query'],
|
tabs: ['sendMessage', 'query'],
|
||||||
|
@ -32,33 +32,31 @@ var msg = window.INJECTED === 1 && window.msg || (() => {
|
||||||
bg = null;
|
bg = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(handleMessage);
|
// TODO: maybe move into polyfill.js and hook addListener + sendMessage so they wrap/unwrap automatically
|
||||||
|
const wrapData = data => ({
|
||||||
window.API = new Proxy({}, {
|
data,
|
||||||
get(target, name) {
|
|
||||||
// using a named function for convenience when debugging
|
|
||||||
return async function invokeAPI(...args) {
|
|
||||||
if (!bg && chrome.tabs) {
|
|
||||||
bg = await browser.runtime.getBackgroundPage().catch(() => {});
|
|
||||||
}
|
|
||||||
const message = {method: 'invokeAPI', name, args};
|
|
||||||
// content scripts, frames and probably private tabs
|
|
||||||
if (!bg || window !== parent) {
|
|
||||||
return msg.send(message);
|
|
||||||
}
|
|
||||||
// in FF, the object would become a dead object when the window
|
|
||||||
// is closed, so we have to clone the object into background.
|
|
||||||
const res = bg.msg._execute(TARGETS.extension, bg.deepCopy(message), {
|
|
||||||
frameId: 0,
|
|
||||||
tab: NEEDS_TAB_IN_SENDER.includes(name) && await getOwnTab(),
|
|
||||||
url: location.href,
|
|
||||||
});
|
});
|
||||||
return deepCopy(await res);
|
const wrapError = error => ({
|
||||||
};
|
error: Object.assign({
|
||||||
},
|
message: error.message || `${error}`,
|
||||||
|
stack: error.stack,
|
||||||
|
}, error), // passing custom properties e.g. `error.index`
|
||||||
|
});
|
||||||
|
const unwrapResponse = ({data, error} = {error: {message: ERR_NO_RECEIVER}}) =>
|
||||||
|
error
|
||||||
|
? Promise.reject(Object.assign(new Error(error.message), error))
|
||||||
|
: data;
|
||||||
|
chrome.runtime.onMessage.addListener(({data, target}, sender, sendResponse) => {
|
||||||
|
const res = window.msg._execute(TARGETS[target] || TARGETS.all, data, sender);
|
||||||
|
if (res instanceof Promise) {
|
||||||
|
res.then(wrapData, wrapError).then(sendResponse);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (res !== undefined) sendResponse(wrapData(res));
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
// This direct assignment allows IDEs to provide autocomplete for msg methods automatically
|
||||||
|
const msg = window.msg = {
|
||||||
isBg,
|
isBg,
|
||||||
|
|
||||||
async broadcast(data) {
|
async broadcast(data) {
|
||||||
|
@ -137,32 +135,27 @@ var msg = window.INJECTED === 1 && window.msg || (() => {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: maybe move into polyfill.js and hook addListener + sendMessage so they wrap/unwrap automatically
|
window.API = new Proxy({}, {
|
||||||
function handleMessage({data, target}, sender, sendResponse) {
|
get(target, name) {
|
||||||
const res = msg._execute(TARGETS[target] || TARGETS.all, data, sender);
|
// using a named function for convenience when debugging
|
||||||
if (res instanceof Promise) {
|
return async function invokeAPI(...args) {
|
||||||
res.then(wrapData, wrapError).then(sendResponse);
|
if (!bg && chrome.tabs) {
|
||||||
return true;
|
bg = await browser.runtime.getBackgroundPage().catch(() => {});
|
||||||
}
|
}
|
||||||
if (res !== undefined) sendResponse(wrapData(res));
|
const message = {method: 'invokeAPI', name, args};
|
||||||
|
// content scripts, frames and probably private tabs
|
||||||
|
if (!bg || window !== parent) {
|
||||||
|
return msg.send(message);
|
||||||
}
|
}
|
||||||
|
// in FF, the object would become a dead object when the window
|
||||||
function wrapData(data) {
|
// is closed, so we have to clone the object into background.
|
||||||
return {data};
|
const res = bg.msg._execute(TARGETS.extension, bg.deepCopy(message), {
|
||||||
}
|
frameId: 0,
|
||||||
|
tab: NEEDS_TAB_IN_SENDER.includes(name) && await getOwnTab(),
|
||||||
function wrapError(error) {
|
url: location.href,
|
||||||
return {
|
});
|
||||||
error: Object.assign({
|
return deepCopy(await res);
|
||||||
message: error.message || `${error}`,
|
|
||||||
stack: error.stack,
|
|
||||||
}, error), // passing custom properties e.g. `error.index`
|
|
||||||
};
|
};
|
||||||
}
|
},
|
||||||
|
});
|
||||||
function unwrapResponse({data, error} = {error: {message: ERR_NO_RECEIVER}}) {
|
|
||||||
return error
|
|
||||||
? Promise.reject(Object.assign(new Error(error.message), error))
|
|
||||||
: data;
|
|
||||||
}
|
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user