2018-01-01 17:02:49 +00:00
|
|
|
/*
|
|
|
|
global BG: true
|
|
|
|
global FIREFOX: true
|
|
|
|
global onRuntimeMessage applyOnMessage
|
|
|
|
*/
|
2017-07-12 18:17:04 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// keep message channel open for sendResponse in chrome.runtime.onMessage listener
|
|
|
|
const KEEP_CHANNEL_OPEN = true;
|
|
|
|
|
2017-11-14 07:52:54 +00:00
|
|
|
const CHROME = Boolean(chrome.app) && parseInt(navigator.userAgent.match(/Chrom\w+\/(?:\d+\.){2}(\d+)|$/)[1]);
|
2018-01-01 17:02:49 +00:00
|
|
|
const OPERA = Boolean(chrome.app) && parseFloat(navigator.userAgent.match(/\bOPR\/(\d+\.\d+)|$/)[1]);
|
|
|
|
const VIVALDI = Boolean(chrome.app) && navigator.userAgent.includes('Vivaldi');
|
2017-11-25 13:24:07 +00:00
|
|
|
const ANDROID = !chrome.windows;
|
2018-01-01 17:02:49 +00:00
|
|
|
let FIREFOX = !chrome.app && parseFloat(navigator.userAgent.match(/\bFirefox\/(\d+\.\d+)|$/)[1]);
|
2017-11-08 03:46:56 +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)
|
|
|
|
FIREFOX = 50;
|
|
|
|
browser.runtime.getBrowserInfo().then(info => {
|
|
|
|
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(''),
|
|
|
|
|
|
|
|
optionsUI: [
|
2017-07-14 08:25:33 +00:00
|
|
|
chrome.runtime.getURL('options.html'),
|
2017-07-12 18:17:04 +00:00
|
|
|
'chrome://extensions/?options=' + chrome.runtime.id,
|
|
|
|
],
|
|
|
|
|
|
|
|
configureCommands:
|
|
|
|
OPERA ? 'opera://settings/configureCommands'
|
|
|
|
: 'chrome://extensions/configureCommands',
|
|
|
|
|
|
|
|
// 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/' :
|
|
|
|
OPERA ? 'https://addons.opera.com/' :
|
|
|
|
'https://chrome.google.com/webstore/',
|
|
|
|
|
|
|
|
// Chrome 61.0.3161+ doesn't run content scripts on NTP https://crrev.com/2978953002/
|
|
|
|
// TODO: remove when "minimum_chrome_version": "61" or higher
|
2017-11-14 07:52:54 +00:00
|
|
|
chromeProtectsNTP: CHROME >= 3161,
|
2017-07-22 16:52:07 +00:00
|
|
|
|
2017-08-16 17:40:24 +00:00
|
|
|
supported: url => (
|
2017-12-22 05:36:10 +00:00
|
|
|
url.startsWith('http') && (FIREFOX || !url.startsWith(URLS.browserWebStore)) ||
|
2017-08-16 17:40:24 +00:00
|
|
|
url.startsWith('ftp') ||
|
|
|
|
url.startsWith('file') ||
|
|
|
|
url.startsWith(URLS.ownOrigin) ||
|
|
|
|
!URLS.chromeProtectsNTP && url.startsWith('chrome://newtab/')
|
|
|
|
),
|
2017-07-12 18:17:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let BG = chrome.extension.getBackgroundPage();
|
2017-08-04 09:42:34 +00:00
|
|
|
if (BG && !BG.getStyles && BG !== window) {
|
2017-07-29 05:15:10 +00:00
|
|
|
// own page like editor/manage is being loaded on browser startup
|
|
|
|
// before the background page has been fully initialized;
|
|
|
|
// it'll be resolved in onBackgroundReady() instead
|
|
|
|
BG = null;
|
|
|
|
}
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!BG || BG !== window) {
|
2017-12-07 03:52:43 +00:00
|
|
|
if (FIREFOX) {
|
|
|
|
document.documentElement.classList.add('firefox');
|
2017-12-07 03:54:51 +00:00
|
|
|
} else if (OPERA) {
|
2017-12-07 03:52:43 +00:00
|
|
|
document.documentElement.classList.add('opera');
|
2018-01-01 17:02:49 +00:00
|
|
|
} else {
|
|
|
|
if (VIVALDI) document.documentElement.classList.add('vivaldi');
|
2017-12-07 03:52:43 +00:00
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
// TODO: remove once our manifest's minimum_chrome_version is 50+
|
|
|
|
// Chrome 49 doesn't report own extension pages in webNavigation apparently
|
2017-11-14 07:52:54 +00:00
|
|
|
if (CHROME && CHROME < 2661) {
|
2018-01-01 17:02:49 +00:00
|
|
|
getActiveTab().then(tab =>
|
|
|
|
window.API.updateIcon({tab}));
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 10:29:56 +00:00
|
|
|
const FIREFOX_NO_DOM_STORAGE = FIREFOX && !tryCatch(() => localStorage);
|
|
|
|
if (FIREFOX_NO_DOM_STORAGE) {
|
|
|
|
// may be disabled via dom.storage.enabled
|
|
|
|
Object.defineProperty(window, 'localStorage', {value: {}});
|
|
|
|
Object.defineProperty(window, 'sessionStorage', {value: {}});
|
|
|
|
}
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var API = (() => {
|
|
|
|
return new Proxy(() => {}, {
|
|
|
|
get: (target, name) =>
|
|
|
|
name === 'remoteCall' ?
|
|
|
|
remoteCall :
|
|
|
|
arg => invokeBG(name, arg),
|
|
|
|
});
|
|
|
|
|
|
|
|
function remoteCall(name, arg, remoteWindow) {
|
|
|
|
let thing = window[name] || window.API_METHODS[name];
|
|
|
|
if (typeof thing === 'function') {
|
|
|
|
thing = thing(arg);
|
|
|
|
}
|
|
|
|
if (!thing || typeof thing !== 'object') {
|
|
|
|
return thing;
|
|
|
|
} else if (thing instanceof Promise) {
|
|
|
|
return thing.then(product => remoteWindow.deepCopy(product));
|
|
|
|
} else {
|
|
|
|
return remoteWindow.deepCopy(thing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function invokeBG(name, arg = {}) {
|
|
|
|
if (BG && (name in BG || name in BG.API_METHODS)) {
|
|
|
|
const call = BG !== window ?
|
|
|
|
BG.API.remoteCall(name, BG.deepCopy(arg), window) :
|
|
|
|
remoteCall(name, arg, BG);
|
|
|
|
return Promise.resolve(call);
|
|
|
|
}
|
|
|
|
if (BG && BG.getStyles) {
|
|
|
|
throw new Error('Bad API method', name, arg);
|
|
|
|
}
|
|
|
|
if (FIREFOX) {
|
|
|
|
arg.method = name;
|
|
|
|
return sendMessage(arg);
|
|
|
|
}
|
|
|
|
return onBackgroundReady().then(() => invokeBG(name, arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
function onBackgroundReady() {
|
|
|
|
return BG && BG.getStyles ? Promise.resolve() : new Promise(function ping(resolve) {
|
|
|
|
sendMessage({method: 'healthCheck'}, health => {
|
|
|
|
if (health !== undefined) {
|
|
|
|
BG = chrome.extension.getBackgroundPage();
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
setTimeout(ping, 0, resolve);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2017-07-29 05:15:10 +00:00
|
|
|
|
2017-07-12 18:17:04 +00:00
|
|
|
function notifyAllTabs(msg) {
|
|
|
|
const originalMessage = msg;
|
2018-01-10 18:56:14 +00:00
|
|
|
const styleUpdated = msg.method === 'styleUpdated';
|
|
|
|
if (styleUpdated || msg.method === 'styleAdded') {
|
2017-07-12 18:17:04 +00:00
|
|
|
// apply/popup/manage use only meta for these two methods,
|
|
|
|
// editor may need the full code but can fetch it directly,
|
|
|
|
// so we send just the meta to avoid spamming lots of tabs with huge styles
|
|
|
|
msg = Object.assign({}, msg, {
|
|
|
|
style: getStyleWithNoCode(msg.style)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const affectsAll = !msg.affects || msg.affects.all;
|
|
|
|
const affectsOwnOriginOnly = !affectsAll && (msg.affects.editor || msg.affects.manager);
|
|
|
|
const affectsTabs = affectsAll || affectsOwnOriginOnly;
|
|
|
|
const affectsIcon = affectsAll || msg.affects.icon;
|
|
|
|
const affectsPopup = affectsAll || msg.affects.popup;
|
|
|
|
const affectsSelf = affectsPopup || msg.prefs;
|
2018-01-14 15:18:08 +00:00
|
|
|
// notify all open extension pages and popups
|
2018-01-01 17:02:49 +00:00
|
|
|
if (affectsSelf) {
|
|
|
|
msg.tabId = undefined;
|
|
|
|
sendMessage(msg, ignoreChromeError);
|
|
|
|
}
|
|
|
|
// notify tabs
|
2017-07-12 18:17:04 +00:00
|
|
|
if (affectsTabs || affectsIcon) {
|
|
|
|
const notifyTab = tab => {
|
2018-01-10 18:56:14 +00:00
|
|
|
if (!styleUpdated
|
|
|
|
&& (affectsTabs || URLS.optionsUI.includes(tab.url))
|
2018-01-14 15:18:08 +00:00
|
|
|
// own pages are already notified via sendMessage
|
2017-07-12 18:17:04 +00:00
|
|
|
&& !(affectsSelf && tab.url.startsWith(URLS.ownOrigin))
|
|
|
|
// skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF
|
|
|
|
&& (!FIREFOX || tab.width)) {
|
2017-11-24 15:26:20 +00:00
|
|
|
msg.tabId = tab.id;
|
|
|
|
sendMessage(msg, ignoreChromeError);
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
if (affectsIcon) {
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
debounce(API.updateIcon, 0, {tab});
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// list all tabs including chrome-extension:// which can be ours
|
|
|
|
Promise.all([
|
|
|
|
queryTabs(affectsOwnOriginOnly ? {url: URLS.ownOrigin + '*'} : {}),
|
|
|
|
getActiveTab(),
|
|
|
|
]).then(([tabs, activeTab]) => {
|
|
|
|
const activeTabId = activeTab && activeTab.id;
|
|
|
|
for (const tab of tabs) {
|
|
|
|
invokeOrPostpone(tab.id === activeTabId, notifyTab, tab);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// notify self: the message no longer is sent to the origin in new Chrome
|
2017-07-16 18:02:00 +00:00
|
|
|
if (typeof onRuntimeMessage !== 'undefined') {
|
2017-07-12 18:17:04 +00:00
|
|
|
onRuntimeMessage(originalMessage);
|
|
|
|
}
|
|
|
|
// notify apply.js on own pages
|
2017-07-16 18:02:00 +00:00
|
|
|
if (typeof applyOnMessage !== 'undefined') {
|
2017-07-12 18:17:04 +00:00
|
|
|
applyOnMessage(originalMessage);
|
|
|
|
}
|
2018-01-10 18:56:14 +00:00
|
|
|
// propagate saved style state/code efficiently
|
|
|
|
if (styleUpdated) {
|
|
|
|
API.refreshAllTabs(msg);
|
|
|
|
}
|
2017-11-24 15:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function sendMessage(msg, callback) {
|
|
|
|
/*
|
|
|
|
Promise mode [default]:
|
|
|
|
- rejects on receiving {__ERROR__: message} created by background.js::onRuntimeMessage
|
|
|
|
- automatically suppresses chrome.runtime.lastError because it's autogenerated
|
|
|
|
by browserAction.setText which lacks a callback param in chrome API
|
|
|
|
Standard callback mode:
|
|
|
|
- enabled by passing a second param
|
|
|
|
*/
|
|
|
|
const {tabId, frameId} = msg;
|
2017-11-25 17:24:15 +00:00
|
|
|
const fn = tabId >= 0 ? chrome.tabs.sendMessage : chrome.runtime.sendMessage;
|
|
|
|
const args = tabId >= 0 ? [tabId, msg, {frameId}] : [msg];
|
2017-11-24 15:26:20 +00:00
|
|
|
if (callback) {
|
|
|
|
fn(...args, callback);
|
|
|
|
} else {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fn(...args, r => {
|
|
|
|
const err = r && r.__ERROR__;
|
|
|
|
(err ? reject : resolve)(err || r);
|
2017-12-18 06:57:00 +00:00
|
|
|
ignoreChromeError();
|
2017-11-24 15:26:20 +00:00
|
|
|
});
|
|
|
|
});
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function queryTabs(options = {}) {
|
|
|
|
return new Promise(resolve =>
|
|
|
|
chrome.tabs.query(options, tabs =>
|
|
|
|
resolve(tabs)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getTab(id) {
|
|
|
|
return new Promise(resolve =>
|
|
|
|
chrome.tabs.get(id, tab =>
|
|
|
|
!chrome.runtime.lastError && resolve(tab)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getOwnTab() {
|
|
|
|
return new Promise(resolve =>
|
|
|
|
chrome.tabs.getCurrent(tab => resolve(tab)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getActiveTab() {
|
|
|
|
return queryTabs({currentWindow: true, active: true})
|
|
|
|
.then(tabs => tabs[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getActiveTabRealURL() {
|
|
|
|
return getActiveTab()
|
|
|
|
.then(getTabRealURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getTabRealURL(tab) {
|
|
|
|
return new Promise(resolve => {
|
2017-07-22 16:52:07 +00:00
|
|
|
if (tab.url !== 'chrome://newtab/' || URLS.chromeProtectsNTP) {
|
2017-07-12 18:17:04 +00:00
|
|
|
resolve(tab.url);
|
|
|
|
} else {
|
|
|
|
chrome.webNavigation.getFrame({tabId: tab.id, frameId: 0, processId: -1}, frame => {
|
|
|
|
resolve(frame && frame.url || '');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param {Object} params - or just a string e.g. openURL('foo')
|
|
|
|
* @param {string} params.url - if relative, it's auto-expanded to the full extension URL
|
|
|
|
* @param {number} [params.index] - move the tab to this index in the tab strip, -1 = last
|
|
|
|
* @param {Boolean} [params.active=tue] - true to activate the tab, false to open in background
|
|
|
|
* @param {?Boolean} [params.currentWindow=true] - pass null to check all windows
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
function openURL({
|
|
|
|
url = arguments[0],
|
|
|
|
index,
|
|
|
|
active,
|
|
|
|
currentWindow = true,
|
|
|
|
}) {
|
|
|
|
url = url.includes('://') ? url : chrome.runtime.getURL(url);
|
|
|
|
// [some] chromium forks don't handle their fake branded protocols
|
|
|
|
url = url.replace(/^(opera|vivaldi)/, 'chrome');
|
|
|
|
// FF doesn't handle moz-extension:// URLs (bug)
|
|
|
|
// FF decodes %2F in encoded parameters (bug)
|
|
|
|
// API doesn't handle the hash-fragment part
|
|
|
|
const urlQuery = url.startsWith('moz-extension') ? undefined :
|
|
|
|
FIREFOX && url.includes('%2F') ?
|
|
|
|
url.replace(/%2F.*/, '*').replace(/#.*/, '') :
|
|
|
|
url.replace(/#.*/, '');
|
|
|
|
return queryTabs({url: urlQuery, currentWindow}).then(maybeSwitch);
|
|
|
|
|
|
|
|
function maybeSwitch(tabs = []) {
|
|
|
|
const urlFF = FIREFOX && url.replace(/%2F/g, '/');
|
|
|
|
const tab = tabs.find(tab => tab.url === url || tab.url === urlFF);
|
|
|
|
if (!tab) {
|
|
|
|
return getActiveTab().then(maybeReplace);
|
|
|
|
}
|
|
|
|
if (index !== undefined && tab.index !== index) {
|
|
|
|
chrome.tabs.move(tab.id, {index});
|
|
|
|
}
|
|
|
|
return activateTab(tab);
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
|
2017-12-31 14:58:35 +00:00
|
|
|
// update current NTP or about:blank
|
|
|
|
// except when 'url' is chrome:// or chrome-extension:// in incognito
|
|
|
|
function maybeReplace(tab) {
|
|
|
|
const chromeInIncognito = tab && tab.incognito && url.startsWith('chrome');
|
|
|
|
const emptyTab = tab && (tab.url === 'chrome://newtab/' || tab.url === 'about:newtab');
|
|
|
|
if (emptyTab && !chromeInIncognito) {
|
|
|
|
return new Promise(resolve =>
|
|
|
|
chrome.tabs.update({url}, resolve));
|
|
|
|
}
|
|
|
|
const options = {url, index, active};
|
|
|
|
// FF57+ supports openerTabId, but not in Android (indicated by the absence of chrome.windows)
|
|
|
|
if (tab && (!FIREFOX || FIREFOX >= 57 && chrome.windows) && !chromeInIncognito) {
|
|
|
|
options.openerTabId = tab.id;
|
|
|
|
}
|
|
|
|
return new Promise(resolve =>
|
|
|
|
chrome.tabs.create(options, resolve));
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function activateTab(tab) {
|
|
|
|
return Promise.all([
|
|
|
|
new Promise(resolve => {
|
|
|
|
chrome.tabs.update(tab.id, {active: true}, resolve);
|
|
|
|
}),
|
2017-11-25 13:24:07 +00:00
|
|
|
chrome.windows && new Promise(resolve => {
|
2017-07-12 18:17:04 +00:00
|
|
|
chrome.windows.update(tab.windowId, {focused: true}, resolve);
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function stringAsRegExp(s, flags) {
|
2017-07-22 16:52:07 +00:00
|
|
|
return new RegExp(s.replace(/[{}()[\]\\.+*?^$|]/g, '\\$&'), 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getStyleWithNoCode(style) {
|
2018-01-01 17:02:49 +00:00
|
|
|
const stripped = deepCopy(style);
|
|
|
|
for (const section of stripped.sections) section.code = null;
|
|
|
|
stripped.sourceCode = null;
|
2017-07-12 18:17:04 +00:00
|
|
|
return stripped;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// Update: might get fixed in V8 TurboFan in the future
|
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const debounce = Object.assign((fn, delay, ...args) => {
|
|
|
|
clearTimeout(debounce.timers.get(fn));
|
|
|
|
debounce.timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
|
|
|
|
}, {
|
|
|
|
timers: new Map(),
|
|
|
|
run(fn, ...args) {
|
|
|
|
debounce.timers.delete(fn);
|
|
|
|
fn(...args);
|
|
|
|
},
|
|
|
|
unregister(fn) {
|
|
|
|
clearTimeout(debounce.timers.get(fn));
|
|
|
|
debounce.timers.delete(fn);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function deepCopy(obj) {
|
2018-01-01 17:02:49 +00:00
|
|
|
if (!obj || typeof obj !== 'object') return obj;
|
2018-01-10 18:56:14 +00:00
|
|
|
// N.B. the copy should be an explicit literal
|
2018-01-01 17:02:49 +00:00
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
const copy = [];
|
|
|
|
for (const v of obj) {
|
|
|
|
copy.push(!v || typeof v !== 'object' ? v : deepCopy(v));
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
return copy;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
2018-01-01 17:02:49 +00:00
|
|
|
const copy = {};
|
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
|
|
for (const k in obj) {
|
|
|
|
if (!hasOwnProperty.call(obj, k)) continue;
|
|
|
|
const v = obj[k];
|
|
|
|
copy[k] = !v || typeof v !== 'object' ? v : deepCopy(v);
|
|
|
|
}
|
|
|
|
return copy;
|
2017-07-12 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function sessionStorageHash(name) {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
value: tryCatch(JSON.parse, sessionStorage[name]) || {},
|
|
|
|
set(k, v) {
|
|
|
|
this.value[k] = v;
|
|
|
|
this.updateStorage();
|
|
|
|
},
|
|
|
|
unset(k) {
|
|
|
|
delete this.value[k];
|
|
|
|
this.updateStorage();
|
|
|
|
},
|
|
|
|
updateStorage() {
|
|
|
|
sessionStorage[this.name] = JSON.stringify(this.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-24 19:30:39 +00:00
|
|
|
function download(url, {
|
|
|
|
method = url.includes('?') ? 'POST' : 'GET',
|
|
|
|
body = url.includes('?') ? url.slice(url.indexOf('?')) : null,
|
2017-12-10 07:05:47 +00:00
|
|
|
responseType = 'text',
|
2017-11-24 19:30:39 +00:00
|
|
|
requiredStatusCode = 200,
|
|
|
|
timeout = 10e3,
|
|
|
|
headers = {
|
|
|
|
'Content-type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
} = {}) {
|
2017-07-12 18:17:04 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-18 22:50:20 +00:00
|
|
|
url = new URL(url);
|
2017-11-09 00:16:39 +00:00
|
|
|
if (url.protocol === 'file:' && FIREFOX) {
|
2017-09-18 22:50:20 +00:00
|
|
|
// https://stackoverflow.com/questions/42108782/firefox-webextensions-get-local-files-content-by-path
|
|
|
|
// FIXME: add FetchController when it is available.
|
2017-11-24 19:30:39 +00:00
|
|
|
const timer = setTimeout(reject, timeout, new Error('Timeout fetching ' + url.href));
|
2017-09-18 22:50:20 +00:00
|
|
|
fetch(url.href, {mode: 'same-origin'})
|
|
|
|
.then(r => {
|
|
|
|
clearTimeout(timer);
|
2017-11-24 19:30:39 +00:00
|
|
|
return r.status === 200 ? r.text() : Promise.reject(r.status);
|
2017-09-18 22:50:20 +00:00
|
|
|
})
|
2017-11-24 19:30:39 +00:00
|
|
|
.catch(reject)
|
|
|
|
.then(resolve);
|
2017-09-18 22:50:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-12 18:17:04 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-11-24 19:30:39 +00:00
|
|
|
xhr.timeout = timeout;
|
|
|
|
xhr.onloadend = event => {
|
|
|
|
if (event.type !== 'error' && (
|
|
|
|
xhr.status === requiredStatusCode || !requiredStatusCode ||
|
|
|
|
url.protocol === 'file:')) {
|
2017-12-10 07:05:47 +00:00
|
|
|
resolve(xhr.response);
|
2017-11-24 19:30:39 +00:00
|
|
|
} else {
|
|
|
|
reject(xhr.status);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = xhr.onloadend;
|
2017-12-10 07:05:47 +00:00
|
|
|
xhr.responseType = responseType;
|
2017-11-24 19:30:39 +00:00
|
|
|
xhr.open(method, url.href, true);
|
|
|
|
for (const key in headers) {
|
|
|
|
xhr.setRequestHeader(key, headers[key]);
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function invokeOrPostpone(isInvoke, fn, ...args) {
|
|
|
|
return isInvoke
|
|
|
|
? fn(...args)
|
|
|
|
: setTimeout(invokeOrPostpone, 0, true, fn, ...args);
|
|
|
|
}
|
2017-08-05 16:49:25 +00:00
|
|
|
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function openEditor({id}) {
|
2017-08-05 16:49:25 +00:00
|
|
|
let url = '/edit.html';
|
|
|
|
if (id) {
|
|
|
|
url += `?id=${id}`;
|
|
|
|
}
|
2017-11-25 13:24:07 +00:00
|
|
|
if (chrome.windows && prefs.get('openEditInWindow')) {
|
2017-08-05 16:49:25 +00:00
|
|
|
chrome.windows.create(Object.assign({url}, prefs.get('windowPosition')));
|
|
|
|
} else {
|
|
|
|
openURL({url});
|
|
|
|
}
|
|
|
|
}
|
2017-09-25 10:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
function closeCurrentTab() {
|
2017-11-09 04:49:37 +00:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1409375
|
|
|
|
getOwnTab().then(tab => {
|
2017-09-25 10:43:55 +00:00
|
|
|
if (tab) {
|
|
|
|
chrome.tabs.remove(tab.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|