2018-11-30 01:35:21 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(() => {
|
|
|
|
/* Chrome reinjects content script when documentElement is replaced so we ignore it
|
|
|
|
by checking against a literal `1`, not just `if (truthy)`, because <html id="INJECTED">
|
|
|
|
is exposed per HTML spec as a global `window.INJECTED` */
|
|
|
|
if (window.INJECTED === 1) return;
|
2020-02-12 14:39:00 +00:00
|
|
|
|
2020-10-07 20:27:38 +00:00
|
|
|
//#region for content scripts and our extension pages
|
2020-08-14 12:16:01 +00:00
|
|
|
|
2020-11-11 11:53:40 +00:00
|
|
|
if (!((window.browser || {}).runtime || {}).sendMessage) {
|
|
|
|
/* Auto-promisifier with a fallback to direct call on signature error.
|
|
|
|
The fallback isn't used now since we call all synchronous methods via `chrome` */
|
|
|
|
const directEvents = ['addListener', 'removeListener', 'hasListener', 'hasListeners'];
|
|
|
|
// generated by tools/chrome-api-no-cb.js
|
|
|
|
const directMethods = {
|
|
|
|
alarms: ['create'],
|
|
|
|
extension: ['getBackgroundPage', 'getExtensionTabs', 'getURL', 'getViews', 'setUpdateUrlData'],
|
|
|
|
i18n: ['getMessage', 'getUILanguage'],
|
|
|
|
identity: ['getRedirectURL'],
|
|
|
|
runtime: ['connect', 'connectNative', 'getManifest', 'getURL', 'reload', 'restart'],
|
|
|
|
tabs: ['connect'],
|
|
|
|
};
|
|
|
|
const promisify = function (fn, ...args) {
|
|
|
|
let res;
|
|
|
|
try {
|
|
|
|
let resolve, reject;
|
|
|
|
/* Some callbacks have 2 parameters so we're resolving as an array in that case.
|
|
|
|
For example, chrome.runtime.requestUpdateCheck and chrome.webRequest.onAuthRequired */
|
|
|
|
args.push((...results) =>
|
|
|
|
chrome.runtime.lastError ?
|
|
|
|
reject(new Error(chrome.runtime.lastError.message)) :
|
|
|
|
resolve(results.length <= 1 ? results[0] : results));
|
|
|
|
fn.apply(this, args);
|
|
|
|
res = new Promise((...rr) => ([resolve, reject] = rr));
|
|
|
|
} catch (err) {
|
|
|
|
if (!err.message.includes('No matching signature')) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
args.pop();
|
|
|
|
res = fn.apply(this, args);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
const proxify = (src, srcName, target, key) => {
|
|
|
|
let res = src[key];
|
|
|
|
if (res && typeof res === 'object') {
|
|
|
|
res = createProxy(res, key); // eslint-disable-line no-use-before-define
|
|
|
|
} else if (typeof res === 'function') {
|
|
|
|
res = (directMethods[srcName] || directEvents).includes(key)
|
|
|
|
? res.bind(src)
|
|
|
|
: promisify.bind(src, res);
|
|
|
|
}
|
|
|
|
target[key] = res;
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
const createProxy = (src, srcName) =>
|
|
|
|
new Proxy({}, {
|
|
|
|
get(target, key) {
|
|
|
|
return target[key] || proxify(src, srcName, target, key);
|
2020-10-07 20:27:38 +00:00
|
|
|
},
|
|
|
|
});
|
2020-11-11 11:53:40 +00:00
|
|
|
window.browser = createProxy(chrome);
|
2018-11-30 01:35:21 +00:00
|
|
|
}
|
2020-02-12 14:39:00 +00:00
|
|
|
|
2020-11-11 11:53:40 +00:00
|
|
|
//#endregion
|
2020-08-14 12:16:01 +00:00
|
|
|
|
|
|
|
if (!chrome.tabs) return;
|
2020-02-12 14:39:00 +00:00
|
|
|
|
2020-10-07 20:27:38 +00:00
|
|
|
//#region for our extension pages
|
2018-11-30 01:35:21 +00:00
|
|
|
|
2021-04-24 13:37:30 +00:00
|
|
|
const reqPromise = {};
|
2021-01-01 14:27:58 +00:00
|
|
|
window.require = async function require(urls, cb) {
|
|
|
|
const promises = [];
|
|
|
|
const all = [];
|
|
|
|
const toLoad = [];
|
|
|
|
for (let url of Array.isArray(urls) ? urls : [urls]) {
|
|
|
|
const isCss = url.endsWith('.css');
|
|
|
|
const tag = isCss ? 'link' : 'script';
|
|
|
|
const attr = isCss ? 'href' : 'src';
|
|
|
|
if (!isCss && !url.endsWith('.js')) url += '.js';
|
|
|
|
if (url.startsWith('/')) url = url.slice(1);
|
|
|
|
let el = document.head.querySelector(`${tag}[${attr}$="${url}"]`);
|
|
|
|
if (!el) {
|
|
|
|
el = document.createElement(tag);
|
|
|
|
toLoad.push(el);
|
2021-04-24 13:37:30 +00:00
|
|
|
reqPromise[url] = new Promise((resolve, reject) => {
|
2021-01-01 14:27:58 +00:00
|
|
|
el.onload = resolve;
|
|
|
|
el.onerror = reject;
|
|
|
|
el[attr] = url;
|
|
|
|
if (isCss) el.rel = 'stylesheet';
|
2021-04-24 13:37:30 +00:00
|
|
|
}).catch(console.warn);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
2021-04-24 13:37:30 +00:00
|
|
|
promises.push(reqPromise[url]);
|
2021-01-01 14:27:58 +00:00
|
|
|
all.push(el);
|
|
|
|
}
|
|
|
|
if (toLoad.length) document.head.append(...toLoad);
|
|
|
|
if (promises.length) await Promise.all(promises);
|
|
|
|
if (cb) cb(...all);
|
|
|
|
return all[0];
|
|
|
|
};
|
|
|
|
|
2021-12-12 15:14:31 +00:00
|
|
|
window.requireOrdered = async ids => {
|
|
|
|
for (const id of ids) {
|
|
|
|
await require([id]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-13 18:14:54 +00:00
|
|
|
if (!(new URLSearchParams({foo: 1})).get('foo')) {
|
|
|
|
// TODO: remove when minimum_chrome_version >= 61
|
|
|
|
window.URLSearchParams = class extends URLSearchParams {
|
|
|
|
constructor(init) {
|
|
|
|
if (init && typeof init === 'object') {
|
|
|
|
super();
|
|
|
|
for (const [key, val] of Object.entries(init)) {
|
|
|
|
this.set(key, val);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
super(...arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-11-11 11:53:40 +00:00
|
|
|
|
2020-10-07 20:27:38 +00:00
|
|
|
//#endregion
|
2018-11-30 01:35:21 +00:00
|
|
|
})();
|