2021-01-01 14:27:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-18 11:17:15 +00:00
|
|
|
/* exported
|
2021-01-01 14:27:58 +00:00
|
|
|
CHROME_POPUP_BORDER_BUG
|
2021-01-26 13:33:17 +00:00
|
|
|
RX_META
|
2022-01-27 12:21:02 +00:00
|
|
|
UA
|
2020-11-18 11:17:15 +00:00
|
|
|
capitalize
|
2022-01-23 09:44:25 +00:00
|
|
|
clamp
|
2020-11-18 11:17:15 +00:00
|
|
|
closeCurrentTab
|
|
|
|
deepEqual
|
|
|
|
download
|
|
|
|
getActiveTab
|
2021-01-01 14:27:58 +00:00
|
|
|
getOwnTab
|
2020-11-18 11:17:15 +00:00
|
|
|
getTab
|
|
|
|
ignoreChromeError
|
2021-01-01 14:27:58 +00:00
|
|
|
isEmptyObj
|
2022-01-28 23:54:56 +00:00
|
|
|
mapObj
|
2020-11-18 11:17:15 +00:00
|
|
|
onTabReady
|
|
|
|
openURL
|
|
|
|
sessionStore
|
|
|
|
stringAsRegExp
|
|
|
|
tryCatch
|
|
|
|
tryRegExp
|
2021-05-27 11:18:28 +00:00
|
|
|
tryURL
|
2021-02-22 12:12:19 +00:00
|
|
|
waitForTabUrl
|
2020-11-18 11:17:15 +00:00
|
|
|
*/
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2022-01-27 12:21:02 +00:00
|
|
|
let FIREFOX;
|
|
|
|
const [CHROME, UA] = (() => {
|
|
|
|
const uad = navigator.userAgentData;
|
|
|
|
const ua = uad || navigator.userAgent;
|
|
|
|
const brands = uad ? uad.brands.map(_ => `${_.brand}/${_.version}`).join(' ') : ua;
|
|
|
|
const getVer = name => Number(brands.match(new RegExp(name + '\\w*/(\\d+)|$'))[1]) || false;
|
|
|
|
FIREFOX = !chrome.app && getVer('Firefox');
|
|
|
|
return [
|
|
|
|
getVer('Chrom'),
|
|
|
|
{
|
|
|
|
mobile: uad ? uad.mobile : /Android/.test(ua),
|
|
|
|
windows: /Windows/.test(uad ? uad.platform : ua),
|
|
|
|
opera: getVer('(Opera|OPR)'),
|
|
|
|
vivaldi: getVer('Vivaldi'),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
})();
|
2017-11-08 03:46:56 +00:00
|
|
|
|
2019-09-24 13:21:36 +00:00
|
|
|
// see PR #781
|
2021-01-01 14:27:58 +00:00
|
|
|
const CHROME_POPUP_BORDER_BUG = CHROME >= 62 && CHROME <= 74;
|
2019-09-24 13:21:36 +00:00
|
|
|
|
2017-11-14 07:52:54 +00:00
|
|
|
if (!CHROME && !chrome.browserAction.openPopup) {
|
2017-11-08 03:46:56 +00:00
|
|
|
// in FF pre-57 legacy addons can override useragent so we assume the worst
|
|
|
|
// until we know for sure in the async getBrowserInfo()
|
|
|
|
// (browserAction.openPopup was added in 57)
|
2018-02-25 13:51:07 +00:00
|
|
|
FIREFOX = browser.runtime.getBrowserInfo ? 51 : 50;
|
|
|
|
// getBrowserInfo was added in FF 51
|
|
|
|
Promise.resolve(FIREFOX >= 51 ? browser.runtime.getBrowserInfo() : {version: 50}).then(info => {
|
2017-11-08 03:46:56 +00:00
|
|
|
FIREFOX = parseFloat(info.version);
|
2017-12-04 16:14:04 +00:00
|
|
|
document.documentElement.classList.add('moz-appearance-bug', FIREFOX && FIREFOX < 54);
|
2017-11-08 03:46:56 +00:00
|
|
|
});
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
|
|
|
|
const URLS = {
|
|
|
|
ownOrigin: chrome.runtime.getURL(''),
|
|
|
|
|
|
|
|
configureCommands:
|
2022-01-27 12:21:02 +00:00
|
|
|
UA.opera ? 'opera://settings/configureCommands'
|
2017-07-12 18:17:04 +00:00
|
|
|
: 'chrome://extensions/configureCommands',
|
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
installUsercss: chrome.runtime.getURL('install-usercss.html'),
|
|
|
|
|
2017-07-12 18:17:04 +00:00
|
|
|
// CWS cannot be scripted in chromium, see ChromeExtensionsClient::IsScriptableURL
|
|
|
|
// https://cs.chromium.org/chromium/src/chrome/common/extensions/chrome_extensions_client.cc
|
2017-07-22 16:52:07 +00:00
|
|
|
browserWebStore:
|
|
|
|
FIREFOX ? 'https://addons.mozilla.org/' :
|
2022-01-27 12:21:02 +00:00
|
|
|
UA.opera ? 'https://addons.opera.com/' :
|
2017-07-22 16:52:07 +00:00
|
|
|
'https://chrome.google.com/webstore/',
|
|
|
|
|
2018-03-30 00:23:21 +00:00
|
|
|
emptyTab: [
|
|
|
|
// Chrome and simple forks
|
|
|
|
'chrome://newtab/',
|
|
|
|
// Opera
|
|
|
|
'chrome://startpage/',
|
|
|
|
// Vivaldi
|
|
|
|
'chrome-extension://mpognobbkildjkofajifpdfhcoklimli/components/startpage/startpage.html',
|
|
|
|
// Firefox
|
|
|
|
'about:home',
|
|
|
|
'about:newtab',
|
|
|
|
],
|
|
|
|
|
2022-01-27 02:07:28 +00:00
|
|
|
favicon: host => `https://icons.duckduckgo.com/ip3/${host}.ico`,
|
|
|
|
|
2017-07-22 16:52:07 +00:00
|
|
|
// Chrome 61.0.3161+ doesn't run content scripts on NTP https://crrev.com/2978953002/
|
|
|
|
// TODO: remove when "minimum_chrome_version": "61" or higher
|
2020-05-31 05:43:56 +00:00
|
|
|
chromeProtectsNTP: CHROME >= 61,
|
2017-07-22 16:52:07 +00:00
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
uso: 'https://userstyles.org/',
|
|
|
|
usoJson: 'https://userstyles.org/styles/chrome/',
|
|
|
|
|
2021-08-12 17:35:56 +00:00
|
|
|
usoArchive: 'https://uso.kkx.one/',
|
|
|
|
usoArchiveRaw: [
|
|
|
|
'https://cdn.jsdelivr.net/gh/33kk/uso-archive@flomaster/data/',
|
|
|
|
'https://raw.githubusercontent.com/33kk/uso-archive/flomaster/data/',
|
|
|
|
],
|
2021-04-20 17:40:04 +00:00
|
|
|
|
|
|
|
usw: 'https://userstyles.world/',
|
|
|
|
|
2020-10-04 05:30:02 +00:00
|
|
|
extractUsoArchiveId: url =>
|
|
|
|
url &&
|
2021-08-12 17:35:56 +00:00
|
|
|
URLS.usoArchiveRaw.some(u => url.startsWith(u)) &&
|
2021-02-26 10:01:27 +00:00
|
|
|
Number(url.match(/\/(\d+)\.user\.css|$/)[1]),
|
2021-01-01 14:27:58 +00:00
|
|
|
extractUsoArchiveInstallUrl: url => {
|
|
|
|
const id = URLS.extractUsoArchiveId(url);
|
2021-08-22 17:30:09 +00:00
|
|
|
return id ? `${URLS.usoArchive}style/${id}` : '';
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
2021-08-12 17:35:56 +00:00
|
|
|
makeUsoArchiveCodeUrl: id => `${URLS.usoArchiveRaw[0]}usercss/${id}.user.css`,
|
2018-04-12 06:13:31 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
extractGreasyForkInstallUrl: url =>
|
|
|
|
/^(https:\/\/(?:greasy|sleazy)fork\.org\/scripts\/\d+)[^/]*\/code\/[^/]*\.user\.css$|$/.exec(url)[1],
|
2020-10-06 10:49:43 +00:00
|
|
|
|
2021-04-20 17:40:04 +00:00
|
|
|
extractUSwId: url =>
|
|
|
|
url &&
|
|
|
|
url.startsWith(URLS.usw) &&
|
|
|
|
Number(url.match(/\/(\d+)\.user\.css|$/)[1]),
|
|
|
|
extractUSwInstallUrl: url => {
|
|
|
|
const id = URLS.extractUSwId(url);
|
|
|
|
return id ? `${URLS.usw}style/${id}` : '';
|
|
|
|
},
|
|
|
|
makeUswCodeUrl: id => `${URLS.usw}api/style/${id}.user.css`,
|
|
|
|
|
2017-08-16 17:40:24 +00:00
|
|
|
supported: url => (
|
2020-10-23 10:36:38 +00:00
|
|
|
url.startsWith('http') ||
|
2017-08-16 17:40:24 +00:00
|
|
|
url.startsWith('ftp') ||
|
|
|
|
url.startsWith('file') ||
|
|
|
|
url.startsWith(URLS.ownOrigin) ||
|
|
|
|
!URLS.chromeProtectsNTP && url.startsWith('chrome://newtab/')
|
|
|
|
),
|
2021-11-02 12:32:43 +00:00
|
|
|
|
|
|
|
isLocalhost: url => /^file:|^https?:\/\/(localhost|127\.0\.0\.1)\//.test(url),
|
2017-07-12 18:17:04 +00:00
|
|
|
};
|
|
|
|
|
2021-01-26 13:33:17 +00:00
|
|
|
const RX_META = /\/\*!?\s*==userstyle==[\s\S]*?==\/userstyle==\s*\*\//i;
|
|
|
|
|
2022-01-27 12:21:02 +00:00
|
|
|
if (FIREFOX || UA.opera || UA.vivaldi) {
|
2021-01-01 14:27:58 +00:00
|
|
|
document.documentElement.classList.add(
|
|
|
|
FIREFOX && 'firefox' ||
|
2022-01-27 12:21:02 +00:00
|
|
|
UA.opera && 'opera' ||
|
|
|
|
UA.vivaldi && 'vivaldi');
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 22:54:54 +00:00
|
|
|
// FF57+ supports openerTabId, but not in Android
|
|
|
|
// (detecting FF57 by the feature it added, not navigator.ua which may be spoofed in about:config)
|
|
|
|
const openerTabIdSupported = (!FIREFOX || window.AbortController) && chrome.windows != null;
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2022-01-23 09:44:25 +00:00
|
|
|
function clamp(value, min, max) {
|
|
|
|
return Math.min(Math.max(value, min), max);
|
|
|
|
}
|
|
|
|
|
2017-07-12 18:17:04 +00:00
|
|
|
function getOwnTab() {
|
2020-08-14 12:16:01 +00:00
|
|
|
return browser.tabs.getCurrent();
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function getActiveTab() {
|
|
|
|
return (await browser.tabs.query({currentWindow: true, active: true}))[0];
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 04:36:54 +00:00
|
|
|
function urlToMatchPattern(url, ignoreSearch) {
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns
|
|
|
|
if (!/^(http|https|ws|wss|ftp|data|file)$/.test(url.protocol)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (ignoreSearch) {
|
|
|
|
return [
|
|
|
|
`${url.protocol}//${url.hostname}/${url.pathname}`,
|
2020-11-18 11:17:15 +00:00
|
|
|
`${url.protocol}//${url.hostname}/${url.pathname}?*`,
|
2020-02-02 04:36:54 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
// FIXME: is %2f allowed in pathname and search?
|
|
|
|
return `${url.protocol}//${url.hostname}/${url.pathname}${url.search}`;
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function findExistingTab({url, currentWindow, ignoreHash = true, ignoreSearch = false}) {
|
2021-05-27 11:18:28 +00:00
|
|
|
url = tryURL(url);
|
2021-01-01 14:27:58 +00:00
|
|
|
const tabs = await browser.tabs.query({
|
|
|
|
url: urlToMatchPattern(url, ignoreSearch),
|
|
|
|
currentWindow,
|
|
|
|
});
|
|
|
|
return tabs.find(tab => {
|
2021-05-27 11:18:28 +00:00
|
|
|
const tabUrl = tryURL(tab.pendingUrl || tab.url);
|
2020-02-02 04:36:54 +00:00
|
|
|
return tabUrl.protocol === url.protocol &&
|
|
|
|
tabUrl.username === url.username &&
|
|
|
|
tabUrl.password === url.password &&
|
|
|
|
tabUrl.hostname === url.hostname &&
|
|
|
|
tabUrl.port === url.port &&
|
|
|
|
tabUrl.pathname === url.pathname &&
|
|
|
|
(ignoreSearch || tabUrl.search === url.search) &&
|
|
|
|
(ignoreHash || tabUrl.hash === url.hash);
|
2021-01-01 14:27:58 +00:00
|
|
|
});
|
2020-02-02 04:36:54 +00:00
|
|
|
}
|
2018-03-14 02:58:28 +00:00
|
|
|
|
2017-12-31 14:58:35 +00:00
|
|
|
/**
|
|
|
|
* Opens a tab or activates an existing one,
|
|
|
|
* reuses the New Tab page or about:blank if it's focused now
|
2020-02-20 22:54:54 +00:00
|
|
|
* @param {Object} _
|
|
|
|
* @param {string} _.url - if relative, it's auto-expanded to the full extension URL
|
|
|
|
* @param {number} [_.index] move the tab to this index in the tab strip, -1 = last
|
|
|
|
* @param {number} [_.openerTabId] defaults to the active tab
|
|
|
|
* @param {Boolean} [_.active=true] `true` to activate the tab
|
|
|
|
* @param {Boolean|null} [_.currentWindow=true] `null` to check all windows
|
2020-10-18 13:37:42 +00:00
|
|
|
* @param {chrome.windows.CreateData} [_.newWindow] creates a new window with these params if specified
|
2021-05-27 11:34:19 +00:00
|
|
|
* @param {boolean} [_.ignoreExisting] specify to skip findExistingTab
|
2020-02-20 22:54:54 +00:00
|
|
|
* @returns {Promise<chrome.tabs.Tab>} Promise -> opened/activated tab
|
2017-12-31 14:58:35 +00:00
|
|
|
*/
|
2020-10-18 13:37:42 +00:00
|
|
|
async function openURL({
|
2020-02-20 22:54:54 +00:00
|
|
|
url,
|
|
|
|
index,
|
|
|
|
openerTabId,
|
|
|
|
active = true,
|
|
|
|
currentWindow = true,
|
2020-10-18 13:37:42 +00:00
|
|
|
newWindow,
|
2021-05-27 11:34:19 +00:00
|
|
|
ignoreExisting,
|
2020-02-20 22:54:54 +00:00
|
|
|
}) {
|
2020-02-02 04:36:54 +00:00
|
|
|
if (!url.includes('://')) {
|
|
|
|
url = chrome.runtime.getURL(url);
|
|
|
|
}
|
2021-05-27 11:34:19 +00:00
|
|
|
let tab = !ignoreExisting && await findExistingTab({url, currentWindow});
|
2020-10-18 13:37:42 +00:00
|
|
|
if (tab) {
|
|
|
|
return activateTab(tab, {
|
|
|
|
index,
|
|
|
|
openerTabId,
|
|
|
|
// when hash is different we can only set `url` if it has # otherwise the tab would reload
|
|
|
|
url: url !== (tab.pendingUrl || tab.url) && url.includes('#') ? url : undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (newWindow && browser.windows) {
|
2021-01-01 14:27:58 +00:00
|
|
|
return (await browser.windows.create(Object.assign({url}, newWindow))).tabs[0];
|
2020-10-18 13:37:42 +00:00
|
|
|
}
|
|
|
|
tab = await getActiveTab() || {url: ''};
|
|
|
|
if (isTabReplaceable(tab, url)) {
|
|
|
|
return activateTab(tab, {url, openerTabId});
|
2020-02-20 22:54:54 +00:00
|
|
|
}
|
2020-10-18 13:37:42 +00:00
|
|
|
const id = openerTabId == null ? tab.id : openerTabId;
|
|
|
|
const opener = id != null && !tab.incognito && openerTabIdSupported && {openerTabId: id};
|
|
|
|
return browser.tabs.create(Object.assign({url, index, active}, opener));
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/**
|
|
|
|
* Replaces empty tab (NTP or about:blank)
|
|
|
|
* except when new URL is chrome:// or chrome-extension:// and the empty tab is in incognito
|
|
|
|
*/
|
2020-02-02 04:36:54 +00:00
|
|
|
function isTabReplaceable(tab, newUrl) {
|
2021-01-01 14:27:58 +00:00
|
|
|
return tab &&
|
|
|
|
URLS.emptyTab.includes(tab.pendingUrl || tab.url) &&
|
|
|
|
!(tab.incognito && newUrl.startsWith('chrome'));
|
2020-02-02 04:36:54 +00:00
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function activateTab(tab, {url, index, openerTabId} = {}) {
|
2020-02-02 04:36:54 +00:00
|
|
|
const options = {active: true};
|
|
|
|
if (url) {
|
|
|
|
options.url = url;
|
|
|
|
}
|
2020-02-20 22:54:54 +00:00
|
|
|
if (openerTabId != null && openerTabIdSupported) {
|
|
|
|
options.openerTabId = openerTabId;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
await Promise.all([
|
2020-08-14 12:16:01 +00:00
|
|
|
browser.tabs.update(tab.id, options),
|
|
|
|
browser.windows && browser.windows.update(tab.windowId, {focused: true}),
|
2020-11-18 11:17:15 +00:00
|
|
|
index != null && browser.tabs.move(tab.id, {index}),
|
2021-01-01 14:27:58 +00:00
|
|
|
]);
|
|
|
|
return tab;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function stringAsRegExp(s, flags, asString) {
|
|
|
|
s = s.replace(/[{}()[\]\\.+*?^$|]/g, '\\$&');
|
|
|
|
return asString ? s : new RegExp(s, flags);
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ignoreChromeError() {
|
2017-12-18 06:57:00 +00:00
|
|
|
// eslint-disable-next-line no-unused-expressions
|
|
|
|
chrome.runtime.lastError;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function isEmptyObj(obj) {
|
|
|
|
if (obj) {
|
|
|
|
for (const k in obj) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(obj, k)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 23:54:56 +00:00
|
|
|
/**
|
|
|
|
* @param {?Object} obj
|
|
|
|
* @param {function(val:?, key:string, obj:Object):T} [fn]
|
|
|
|
* @param {string[]} [keys]
|
|
|
|
* @returns {?Object<string,T>}
|
|
|
|
* @template T
|
|
|
|
*/
|
|
|
|
function mapObj(obj, fn, keys) {
|
|
|
|
if (!obj) return obj;
|
|
|
|
const res = {};
|
|
|
|
for (const k of keys || Object.keys(obj)) {
|
|
|
|
if (!keys || k in obj) {
|
|
|
|
res[k] = fn ? fn(obj[k], k, obj) : obj[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/**
|
|
|
|
* js engine can't optimize the entire function if it contains try-catch
|
|
|
|
* so we should keep it isolated from normal code in a minimal wrapper
|
|
|
|
* 2020 update: probably fixed at least in V8
|
|
|
|
*/
|
2017-07-12 18:17:04 +00:00
|
|
|
function tryCatch(func, ...args) {
|
|
|
|
try {
|
|
|
|
return func(...args);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
2017-12-06 06:39:45 +00:00
|
|
|
function tryRegExp(regexp, flags) {
|
2017-07-12 18:17:04 +00:00
|
|
|
try {
|
2017-12-06 06:39:45 +00:00
|
|
|
return new RegExp(regexp, flags);
|
2017-07-12 18:17:04 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryJSONparse(jsonString) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(jsonString);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
2022-01-17 00:53:16 +00:00
|
|
|
function tryURL(url) {
|
2021-05-27 11:18:28 +00:00
|
|
|
try {
|
|
|
|
return new URL(url);
|
|
|
|
} catch (e) {
|
2022-01-17 00:53:16 +00:00
|
|
|
return {
|
|
|
|
hash: '',
|
|
|
|
host: '',
|
|
|
|
hostname: '',
|
|
|
|
href: '',
|
|
|
|
origin: '',
|
|
|
|
password: '',
|
|
|
|
pathname: '',
|
|
|
|
port: '',
|
|
|
|
protocol: '',
|
|
|
|
search: '',
|
|
|
|
searchParams: new URLSearchParams(),
|
|
|
|
username: '',
|
|
|
|
};
|
2021-05-27 11:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function debounce(fn, delay, ...args) {
|
2017-07-12 18:17:04 +00:00
|
|
|
clearTimeout(debounce.timers.get(fn));
|
|
|
|
debounce.timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(debounce, {
|
2017-07-12 18:17:04 +00:00
|
|
|
timers: new Map(),
|
|
|
|
run(fn, ...args) {
|
|
|
|
debounce.timers.delete(fn);
|
|
|
|
fn(...args);
|
|
|
|
},
|
|
|
|
unregister(fn) {
|
|
|
|
clearTimeout(debounce.timers.get(fn));
|
|
|
|
debounce.timers.delete(fn);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-01-28 23:54:56 +00:00
|
|
|
function deepMerge(src, dst, mergeArrays) {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (!src || typeof src !== 'object') {
|
|
|
|
return src;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (Array.isArray(src)) {
|
|
|
|
// using `Array` that belongs to this `window`; not using Array.from as it's slower
|
2022-01-28 23:54:56 +00:00
|
|
|
if (!dst || !mergeArrays) dst = Array.prototype.map.call(src, deepCopy);
|
2021-01-01 14:27:58 +00:00
|
|
|
else for (const v of src) dst.push(deepMerge(v));
|
|
|
|
} else {
|
|
|
|
// using an explicit {} that belongs to this `window`
|
|
|
|
if (!dst) dst = {};
|
|
|
|
for (const [k, v] of Object.entries(src)) {
|
|
|
|
dst[k] = deepMerge(v, dst[k]);
|
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
return dst;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** Useful in arr.map(deepCopy) to ignore the extra parameters passed by map() */
|
|
|
|
function deepCopy(src) {
|
|
|
|
return deepMerge(src);
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2019-09-25 08:44:33 +00:00
|
|
|
function deepEqual(a, b, ignoredKeys) {
|
|
|
|
if (!a || !b) return a === b;
|
|
|
|
const type = typeof a;
|
|
|
|
if (type !== typeof b) return false;
|
|
|
|
if (type !== 'object') return a === b;
|
|
|
|
if (Array.isArray(a)) {
|
|
|
|
return Array.isArray(b) &&
|
|
|
|
a.length === b.length &&
|
|
|
|
a.every((v, i) => deepEqual(v, b[i], ignoredKeys));
|
|
|
|
}
|
|
|
|
for (const key in a) {
|
|
|
|
if (!Object.hasOwnProperty.call(a, key) ||
|
|
|
|
ignoredKeys && ignoredKeys.includes(key)) continue;
|
|
|
|
if (!Object.hasOwnProperty.call(b, key)) return false;
|
|
|
|
if (!deepEqual(a[key], b[key], ignoredKeys)) return false;
|
|
|
|
}
|
|
|
|
for (const key in b) {
|
|
|
|
if (!Object.hasOwnProperty.call(b, key) ||
|
|
|
|
ignoredKeys && ignoredKeys.includes(key)) continue;
|
|
|
|
if (!Object.hasOwnProperty.call(a, key)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-18 11:17:15 +00:00
|
|
|
/* A simple polyfill in case DOM storage is disabled in the browser */
|
|
|
|
const sessionStore = new Proxy({}, {
|
|
|
|
get(target, name) {
|
|
|
|
try {
|
|
|
|
return sessionStorage[name];
|
|
|
|
} catch (e) {
|
|
|
|
Object.defineProperty(window, 'sessionStorage', {value: target});
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2020-11-18 11:17:15 +00:00
|
|
|
},
|
|
|
|
set(target, name, value, proxy) {
|
|
|
|
try {
|
|
|
|
sessionStorage[name] = `${value}`;
|
|
|
|
} catch (e) {
|
|
|
|
proxy[name]; // eslint-disable-line no-unused-expressions
|
|
|
|
target[name] = `${value}`;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
deleteProperty(target, name) {
|
|
|
|
return delete target[name];
|
|
|
|
},
|
|
|
|
});
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2018-03-12 18:41:25 +00:00
|
|
|
/**
|
|
|
|
* @param {String} url
|
|
|
|
* @param {Object} params
|
|
|
|
* @param {String} [params.method]
|
|
|
|
* @param {String|Object} [params.body]
|
2021-01-26 13:33:17 +00:00
|
|
|
* @param {'arraybuffer'|'blob'|'document'|'json'|'text'} [params.responseType]
|
2018-03-12 18:41:25 +00:00
|
|
|
* @param {Number} [params.requiredStatusCode] resolved when matches, otherwise rejected
|
|
|
|
* @param {Number} [params.timeout] ms
|
|
|
|
* @param {Object} [params.headers] {name: value}
|
2021-01-26 13:33:17 +00:00
|
|
|
* @param {string[]} [params.responseHeaders]
|
2018-03-12 18:41:25 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2017-11-24 19:30:39 +00:00
|
|
|
function download(url, {
|
2018-03-12 18:41:25 +00:00
|
|
|
method = 'GET',
|
2018-03-12 19:39:19 +00:00
|
|
|
body,
|
2017-12-10 07:05:47 +00:00
|
|
|
responseType = 'text',
|
2017-11-24 19:30:39 +00:00
|
|
|
requiredStatusCode = 200,
|
2020-11-19 15:10:23 +00:00
|
|
|
timeout = 60e3, // connection timeout, USO is that bad
|
2020-05-16 19:50:04 +00:00
|
|
|
loadTimeout = 2 * 60e3, // data transfer timeout (counted from the first remote response)
|
2021-01-01 14:27:58 +00:00
|
|
|
headers,
|
2021-01-26 13:33:17 +00:00
|
|
|
responseHeaders,
|
2017-11-24 19:30:39 +00:00
|
|
|
} = {}) {
|
2021-01-01 14:27:58 +00:00
|
|
|
/* USO can't handle POST requests for style json and XHR/fetch can't handle super long URL
|
|
|
|
* so we need to collapse all long variables and expand them in the response */
|
|
|
|
const queryPos = url.startsWith(URLS.uso) ? url.indexOf('?') : -1;
|
|
|
|
if (queryPos >= 0) {
|
|
|
|
if (body === undefined) {
|
|
|
|
method = 'POST';
|
|
|
|
body = url.slice(queryPos);
|
|
|
|
url = url.slice(0, queryPos);
|
|
|
|
}
|
|
|
|
if (headers === undefined) {
|
|
|
|
headers = {
|
|
|
|
'Content-type': 'application/x-www-form-urlencoded',
|
|
|
|
};
|
|
|
|
}
|
2018-03-12 18:41:25 +00:00
|
|
|
}
|
2018-04-12 06:13:31 +00:00
|
|
|
const usoVars = [];
|
2017-07-12 18:17:04 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-01 14:27:58 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
2022-01-18 13:39:33 +00:00
|
|
|
const u = new URL(collapseUsoVars(url), location);
|
2020-05-16 19:50:04 +00:00
|
|
|
const onTimeout = () => {
|
2021-01-01 14:27:58 +00:00
|
|
|
xhr.abort();
|
2020-05-16 19:50:04 +00:00
|
|
|
reject(new Error('Timeout fetching ' + u.href));
|
|
|
|
};
|
|
|
|
let timer = setTimeout(onTimeout, timeout);
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState >= XMLHttpRequest.HEADERS_RECEIVED) {
|
|
|
|
xhr.onreadystatechange = null;
|
2021-01-01 14:27:58 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
timer = loadTimeout && setTimeout(onTimeout, loadTimeout);
|
2017-11-24 19:30:39 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-26 13:33:17 +00:00
|
|
|
xhr.onload = () => {
|
|
|
|
if (xhr.status === requiredStatusCode || !requiredStatusCode || u.protocol === 'file:') {
|
|
|
|
const response = expandUsoVars(xhr.response);
|
|
|
|
if (responseHeaders) {
|
|
|
|
const headers = {};
|
|
|
|
for (const h of responseHeaders) headers[h] = xhr.getResponseHeader(h);
|
|
|
|
resolve({headers, response});
|
|
|
|
} else {
|
|
|
|
resolve(response);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reject(xhr.status);
|
|
|
|
}
|
|
|
|
};
|
2021-01-01 14:27:58 +00:00
|
|
|
xhr.onerror = () => reject(xhr.status);
|
|
|
|
xhr.onloadend = () => clearTimeout(timer);
|
2017-12-10 07:05:47 +00:00
|
|
|
xhr.responseType = responseType;
|
2021-01-01 14:27:58 +00:00
|
|
|
xhr.open(method, u.href);
|
|
|
|
for (const [name, value] of Object.entries(headers || {})) {
|
|
|
|
xhr.setRequestHeader(name, value);
|
2017-09-18 22:50:20 +00:00
|
|
|
}
|
2017-11-24 19:30:39 +00:00
|
|
|
xhr.send(body);
|
2017-07-12 18:17:04 +00:00
|
|
|
});
|
2018-04-12 06:13:31 +00:00
|
|
|
|
|
|
|
function collapseUsoVars(url) {
|
|
|
|
if (queryPos < 0 ||
|
|
|
|
url.length < 2000 ||
|
2020-10-04 05:30:02 +00:00
|
|
|
!url.startsWith(URLS.usoJson) ||
|
2018-04-12 06:13:31 +00:00
|
|
|
!/^get$/i.test(method)) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
const params = new URLSearchParams(url.slice(queryPos + 1));
|
|
|
|
for (const [k, v] of params.entries()) {
|
|
|
|
if (v.length < 10 || v.startsWith('ik-')) continue;
|
|
|
|
usoVars.push(v);
|
|
|
|
params.set(k, `\x01${usoVars.length}\x02`);
|
|
|
|
}
|
|
|
|
return url.slice(0, queryPos + 1) + params.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function expandUsoVars(response) {
|
|
|
|
if (!usoVars.length || !response) return response;
|
|
|
|
const isText = typeof response === 'string';
|
|
|
|
const json = isText && tryJSONparse(response) || response;
|
|
|
|
json.updateUrl = url;
|
|
|
|
for (const section of json.sections || []) {
|
|
|
|
const {code} = section;
|
|
|
|
if (code.includes('\x01')) {
|
|
|
|
section.code = code.replace(/\x01(\d+)\x02/g, (_, num) => usoVars[num - 1] || '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isText ? JSON.stringify(json) : json;
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
async function closeCurrentTab() {
|
|
|
|
// https://bugzil.la/1409375
|
|
|
|
const tab = await getOwnTab();
|
|
|
|
if (tab) chrome.tabs.remove(tab.id);
|
2017-09-25 10:43:55 +00:00
|
|
|
}
|
2019-11-05 19:30:45 +00:00
|
|
|
|
2021-02-22 12:12:19 +00:00
|
|
|
function waitForTabUrl(tab) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
browser.tabs.onUpdated.addListener(...[
|
|
|
|
function onUpdated(tabId, info, updatedTab) {
|
|
|
|
if (info.url && tabId === tab.id) {
|
|
|
|
browser.tabs.onUpdated.removeListener(onUpdated);
|
|
|
|
resolve(updatedTab);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
...'UpdateFilter' in browser.tabs ? [{tabId: tab.id}] : [], // FF only
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-05 19:30:45 +00:00
|
|
|
function capitalize(s) {
|
|
|
|
return s[0].toUpperCase() + s.slice(1);
|
|
|
|
}
|