2017-04-11 10:51:40 +00:00
|
|
|
/* global BG: true, onRuntimeMessage, applyOnMessage, handleUpdate, handleDelete */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-14 23:27:52 +00:00
|
|
|
// keep message channel open for sendResponse in chrome.runtime.onMessage listener
|
|
|
|
const KEEP_CHANNEL_OPEN = true;
|
2017-04-09 06:43:51 +00:00
|
|
|
const FIREFOX = /Firefox/.test(navigator.userAgent);
|
|
|
|
const OPERA = /OPR/.test(navigator.userAgent);
|
|
|
|
const URLS = {
|
|
|
|
ownOrigin: chrome.runtime.getURL(''),
|
2017-04-11 10:51:40 +00:00
|
|
|
optionsUI: [
|
2017-04-09 06:43:51 +00:00
|
|
|
chrome.runtime.getURL('options/index.html'),
|
|
|
|
'chrome://extensions/?options=' + chrome.runtime.id,
|
2017-04-11 10:51:40 +00:00
|
|
|
],
|
|
|
|
configureCommands:
|
|
|
|
OPERA ? 'opera://settings/configureCommands'
|
|
|
|
: 'chrome://extensions/configureCommands',
|
2017-04-09 06:43:51 +00:00
|
|
|
};
|
|
|
|
const RX_SUPPORTED_URLS = new RegExp(`^(file|https?|ftps?):|^${URLS.ownOrigin}`);
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
let BG = chrome.extension.getBackgroundPage();
|
2017-03-14 23:27:52 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
if (!BG || BG != window) {
|
|
|
|
document.documentElement.classList.toggle('firefox', FIREFOX);
|
|
|
|
document.documentElement.classList.toggle('opera', OPERA);
|
|
|
|
}
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function notifyAllTabs(msg) {
|
|
|
|
const originalMessage = msg;
|
|
|
|
if (msg.codeIsUpdated === false && msg.style) {
|
|
|
|
msg = Object.assign({}, msg, {
|
|
|
|
style: getStyleWithNoCode(msg.style)
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
const affectsAll = !msg.affects || msg.affects.all;
|
|
|
|
const affectsOwnOrigin = !affectsAll && (msg.affects.editor || msg.affects.manager);
|
2017-04-07 02:42:24 +00:00
|
|
|
const affectsTabs = affectsAll || affectsOwnOrigin;
|
2017-04-11 10:51:40 +00:00
|
|
|
const affectsIcon = affectsAll || msg.affects.icon;
|
|
|
|
const affectsPopup = affectsAll || msg.affects.popup;
|
2017-04-07 02:42:24 +00:00
|
|
|
if (affectsTabs || affectsIcon) {
|
2017-04-11 10:51:40 +00:00
|
|
|
// list all tabs including chrome-extension:// which can be ours
|
2017-04-09 06:43:51 +00:00
|
|
|
chrome.tabs.query(affectsOwnOrigin ? {url: URLS.ownOrigin + '*'} : {}, tabs => {
|
2017-04-07 02:42:24 +00:00
|
|
|
for (const tab of tabs) {
|
2017-04-11 10:51:40 +00:00
|
|
|
if (affectsTabs || URLS.optionsUI.includes(tab.url)) {
|
|
|
|
chrome.tabs.sendMessage(tab.id, msg);
|
2017-04-07 02:42:24 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
if (affectsIcon && BG) {
|
|
|
|
BG.updateIcon(tab);
|
2017-04-07 02:42:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
// notify self: the message no longer is sent to the origin in new Chrome
|
2017-04-11 10:51:40 +00:00
|
|
|
if (typeof onRuntimeMessage != 'undefined') {
|
|
|
|
onRuntimeMessage(originalMessage);
|
2017-04-07 02:42:24 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
// notify apply.js on own pages
|
|
|
|
if (typeof applyOnMessage != 'undefined') {
|
|
|
|
applyOnMessage(originalMessage);
|
2017-04-11 03:51:32 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
// notify background page and all open popups
|
|
|
|
if (affectsPopup || msg.prefs) {
|
|
|
|
chrome.runtime.sendMessage(msg);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2015-05-21 09:34:44 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
function getActiveTab() {
|
2017-03-26 02:30:59 +00:00
|
|
|
return new Promise(resolve =>
|
|
|
|
chrome.tabs.query({currentWindow: true, active: true}, tabs =>
|
|
|
|
resolve(tabs[0])));
|
2015-05-21 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
|
|
|
function getActiveTabRealURL() {
|
2017-03-26 02:30:59 +00:00
|
|
|
return getActiveTab()
|
|
|
|
.then(getTabRealURL);
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getTabRealURL(tab) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
if (tab.url != 'chrome://newtab/') {
|
|
|
|
resolve(tab.url);
|
|
|
|
} else {
|
|
|
|
chrome.webNavigation.getFrame({tabId: tab.id, frameId: 0, processId: -1}, frame => {
|
2017-03-25 21:14:41 +00:00
|
|
|
resolve(frame && frame.url || '');
|
2017-03-26 02:30:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-05-21 09:34:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-03-25 06:17:14 +00:00
|
|
|
// opens a tab or activates the already opened one,
|
|
|
|
// reuses the New Tab page if it's focused now
|
|
|
|
function openURL({url, currentWindow = true}) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (!url.includes('://')) {
|
|
|
|
url = chrome.runtime.getURL(url);
|
|
|
|
}
|
|
|
|
return new Promise(resolve => {
|
2017-03-29 10:00:19 +00:00
|
|
|
// [some] chromium forks don't handle their fake branded protocols
|
|
|
|
url = url.replace(/^(opera|vivaldi)/, 'chrome');
|
2017-03-27 02:35:10 +00:00
|
|
|
// API doesn't handle the hash-fragment part
|
|
|
|
chrome.tabs.query({url: url.replace(/#.*/, ''), currentWindow}, tabs => {
|
|
|
|
for (const tab of tabs) {
|
|
|
|
if (tab.url == url) {
|
|
|
|
activateTab(tab).then(resolve);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-04-09 06:43:51 +00:00
|
|
|
getActiveTab().then(tab => {
|
|
|
|
if (tab && tab.url == 'chrome://newtab/') {
|
|
|
|
chrome.tabs.update({url}, resolve);
|
|
|
|
} else {
|
|
|
|
chrome.tabs.create(tab && !FIREFOX ? {url, openerTabId: tab.id} : {url}, resolve);
|
|
|
|
}
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
});
|
|
|
|
});
|
2017-03-21 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-25 06:17:14 +00:00
|
|
|
function activateTab(tab) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return Promise.all([
|
|
|
|
new Promise(resolve => {
|
|
|
|
chrome.tabs.update(tab.id, {active: true}, resolve);
|
|
|
|
}),
|
|
|
|
new Promise(resolve => {
|
|
|
|
chrome.windows.update(tab.windowId, {focused: true}, resolve);
|
|
|
|
}),
|
|
|
|
]);
|
2017-03-25 06:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-15 12:41:39 +00:00
|
|
|
function stringAsRegExp(s, flags) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return new RegExp(s.replace(/[{}()[\]/\\.+?^$:=*!|]/g, '\\$&'), flags);
|
2017-03-15 12:41:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function ignoreChromeError() {
|
|
|
|
chrome.runtime.lastError; // eslint-disable-line no-unused-expressions
|
2017-03-15 12:41:39 +00:00
|
|
|
}
|
2017-03-16 13:36:33 +00:00
|
|
|
|
2017-03-21 01:32:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function getStyleWithNoCode(style) {
|
|
|
|
const stripped = Object.assign({}, style, {sections: []});
|
|
|
|
for (const section of style.sections) {
|
|
|
|
stripped.sections.push(Object.assign({}, section, {code: null}));
|
|
|
|
}
|
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function tryRegExp(regexp) {
|
|
|
|
try {
|
|
|
|
return new RegExp(regexp);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function tryJSONparse(jsonString) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(jsonString);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function debounce(fn, delay, ...args) {
|
|
|
|
const timers = debounce.timers = debounce.timers || new Map();
|
|
|
|
debounce.run = debounce.run || ((fn, ...args) => {
|
|
|
|
timers.delete(fn);
|
|
|
|
fn(...args);
|
|
|
|
});
|
|
|
|
clearTimeout(timers.get(fn));
|
|
|
|
timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function deepCopy(obj) {
|
|
|
|
if (!obj || typeof obj != 'object') {
|
|
|
|
return obj;
|
|
|
|
} else {
|
|
|
|
const emptyCopy = Object.create(Object.getPrototypeOf(obj));
|
|
|
|
return deepMerge(emptyCopy, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function deepMerge(target, ...args) {
|
|
|
|
for (const obj of args) {
|
|
|
|
for (const k in obj) {
|
|
|
|
const value = obj[k];
|
|
|
|
if (!value || typeof value != 'object') {
|
|
|
|
target[k] = value;
|
|
|
|
} else if (typeof value.slice == 'function') {
|
|
|
|
const arrayCopy = target[k] = target[k] || [];
|
|
|
|
for (const element of value) {
|
|
|
|
arrayCopy.push(deepCopy(element));
|
|
|
|
}
|
|
|
|
} else if (k in target) {
|
|
|
|
deepMerge(target[k], value);
|
|
|
|
} else {
|
|
|
|
target[k] = deepCopy(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onBackgroundReady() {
|
|
|
|
return BG ? Promise.resolve() : new Promise(ping);
|
|
|
|
function ping(resolve) {
|
|
|
|
chrome.runtime.sendMessage({method: 'healthCheck'}, health => {
|
|
|
|
if (health !== undefined) {
|
|
|
|
BG = chrome.extension.getBackgroundPage();
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
ping(resolve);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// in case Chrome haven't yet loaded the bg page and displays our page like edit/manage
|
|
|
|
function getStylesSafe(options) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if (BG) {
|
|
|
|
BG.getStyles(options, resolve);
|
|
|
|
} else {
|
|
|
|
onBackgroundReady().then(() =>
|
|
|
|
BG.getStyles(options, resolve));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function saveStyleSafe(style) {
|
|
|
|
return onBackgroundReady()
|
|
|
|
.then(() => BG.saveStyle(BG.deepCopy(style)))
|
|
|
|
.then(savedStyle => {
|
|
|
|
if (style.notify === false) {
|
|
|
|
handleUpdate(savedStyle, style);
|
|
|
|
}
|
|
|
|
return savedStyle;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function deleteStyleSafe({id, notify = true} = {}) {
|
|
|
|
return onBackgroundReady()
|
|
|
|
.then(() => BG.deleteStyle({id, notify}))
|
|
|
|
.then(() => {
|
|
|
|
if (!notify) {
|
|
|
|
handleDelete(id);
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
});
|
2017-03-29 00:57:21 +00:00
|
|
|
}
|