2020-02-23 15:43:26 +00:00
|
|
|
/* global download prefs openURL FIREFOX CHROME
|
2020-10-06 10:49:43 +00:00
|
|
|
URLS ignoreChromeError chromeLocal semverCompare
|
2020-02-23 15:43:26 +00:00
|
|
|
styleManager msg navigatorUtil workerUtil contentScripts sync
|
2020-08-14 12:16:01 +00:00
|
|
|
findExistingTab activateTab isTabReplaceable getActiveTab
|
2020-10-06 10:49:43 +00:00
|
|
|
*/
|
2020-02-02 04:36:54 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-15 12:41:39 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var backgroundWorker = workerUtil.createWorker({
|
|
|
|
url: '/background/background-worker.js'
|
|
|
|
});
|
2018-01-01 17:02:49 +00:00
|
|
|
|
2020-04-16 10:17:12 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var browserCommands, contextMenus;
|
|
|
|
|
|
|
|
// *************************************************************************
|
|
|
|
// browser commands
|
|
|
|
browserCommands = {
|
|
|
|
openManage,
|
|
|
|
openOptions: () => openManage({options: true}),
|
|
|
|
styleDisableAll(info) {
|
|
|
|
prefs.set('disableAll', info ? info.checked : !prefs.get('disableAll'));
|
|
|
|
},
|
|
|
|
reload: () => chrome.runtime.reload(),
|
|
|
|
};
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
window.API_METHODS = Object.assign(window.API_METHODS || {}, {
|
|
|
|
deleteStyle: styleManager.deleteStyle,
|
|
|
|
editSave: styleManager.editSave,
|
|
|
|
findStyle: styleManager.findStyle,
|
|
|
|
getAllStyles: styleManager.getAllStyles, // used by importer
|
|
|
|
getSectionsByUrl: styleManager.getSectionsByUrl,
|
|
|
|
getStyle: styleManager.get,
|
|
|
|
getStylesByUrl: styleManager.getStylesByUrl,
|
|
|
|
importStyle: styleManager.importStyle,
|
2018-11-11 06:04:22 +00:00
|
|
|
importManyStyles: styleManager.importMany,
|
2018-11-07 06:09:29 +00:00
|
|
|
installStyle: styleManager.installStyle,
|
|
|
|
styleExists: styleManager.styleExists,
|
|
|
|
toggleStyle: styleManager.toggleStyle,
|
|
|
|
|
2019-03-03 22:54:37 +00:00
|
|
|
addInclusion: styleManager.addInclusion,
|
|
|
|
removeInclusion: styleManager.removeInclusion,
|
|
|
|
addExclusion: styleManager.addExclusion,
|
|
|
|
removeExclusion: styleManager.removeExclusion,
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
getTabUrlPrefix() {
|
2020-02-02 04:36:54 +00:00
|
|
|
const {url} = this.sender.tab;
|
|
|
|
if (url.startsWith(URLS.ownOrigin)) {
|
|
|
|
return 'stylus';
|
|
|
|
}
|
|
|
|
return url.match(/^([\w-]+:\/+[^/#]+)/)[1];
|
2018-11-07 06:09:29 +00:00
|
|
|
},
|
2018-10-02 12:22:18 +00:00
|
|
|
|
2018-03-13 14:23:47 +00:00
|
|
|
download(msg) {
|
|
|
|
delete msg.method;
|
|
|
|
return download(msg.url, msg);
|
|
|
|
},
|
2018-07-05 12:42:42 +00:00
|
|
|
parseCss({code}) {
|
2018-11-07 06:09:29 +00:00
|
|
|
return backgroundWorker.parseMozFormat({code});
|
2018-07-05 12:42:42 +00:00
|
|
|
},
|
2020-11-08 10:29:15 +00:00
|
|
|
getPrefs: () => prefs.values,
|
2020-10-28 06:53:02 +00:00
|
|
|
setPref: (key, value) => prefs.set(key, value),
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
openEditor,
|
2018-11-07 06:09:29 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
/* Same as openURL, the only extra prop in `opts` is `message` - it'll be sent when the tab is ready,
|
|
|
|
which is needed in the popup, otherwise another extension could force the tab to open in foreground
|
|
|
|
thus auto-closing the popup (in Chrome at least) and preventing the sendMessage code from running */
|
2020-10-13 18:14:54 +00:00
|
|
|
async openURL(opts) {
|
|
|
|
const tab = await openURL(opts);
|
|
|
|
if (opts.message) {
|
|
|
|
await onTabReady(tab);
|
|
|
|
await msg.sendTab(tab.id, opts.message);
|
|
|
|
}
|
|
|
|
return tab;
|
2020-02-24 23:16:45 +00:00
|
|
|
function onTabReady(tab) {
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
setTimeout(function ping(numTries = 10, delay = 100) {
|
|
|
|
msg.sendTab(tab.id, {method: 'ping'})
|
|
|
|
.catch(() => false)
|
|
|
|
.then(pong => pong
|
|
|
|
? resolve(tab)
|
|
|
|
: numTries && setTimeout(ping, delay, numTries - 1, delay * 1.5) ||
|
|
|
|
reject('timeout'));
|
|
|
|
}));
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
},
|
2018-01-01 17:02:49 +00:00
|
|
|
|
2018-04-12 18:02:34 +00:00
|
|
|
optionsCustomizeHotkeys() {
|
2020-04-16 10:17:12 +00:00
|
|
|
return browserCommands.openOptions()
|
|
|
|
.then(() => new Promise(resolve => setTimeout(resolve, 500)))
|
2018-11-07 06:09:29 +00:00
|
|
|
.then(() => msg.broadcastExtension({method: 'optionsCustomizeHotkeys'}));
|
2019-11-05 19:30:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
syncStart: sync.start,
|
|
|
|
syncStop: sync.stop,
|
|
|
|
syncNow: sync.syncNow,
|
|
|
|
getSyncStatus: sync.getStatus,
|
2020-02-02 04:36:54 +00:00
|
|
|
syncLogin: sync.login,
|
|
|
|
|
|
|
|
openManage
|
2018-01-04 14:04:23 +00:00
|
|
|
});
|
2018-01-01 17:02:49 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// register all listeners
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.on(onRuntimeMessage);
|
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
// tell apply.js to refresh styles for non-committed navigation
|
2018-11-07 06:09:29 +00:00
|
|
|
navigatorUtil.onUrlChange(({tabId, frameId}, type) => {
|
2020-02-23 15:43:26 +00:00
|
|
|
if (type !== 'committed') {
|
|
|
|
msg.sendTab(tabId, {method: 'urlChanged'}, {frameId})
|
|
|
|
.catch(msg.ignoreError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-09 17:33:04 +00:00
|
|
|
if (FIREFOX) {
|
2018-11-07 06:09:29 +00:00
|
|
|
// FF misses some about:blank iframes so we inject our content script explicitly
|
|
|
|
navigatorUtil.onDOMContentLoaded(webNavIframeHelperFF, {
|
|
|
|
url: [
|
|
|
|
{urlEquals: 'about:blank'},
|
|
|
|
]
|
2018-08-09 17:33:04 +00:00
|
|
|
});
|
2017-07-27 14:37:05 +00:00
|
|
|
}
|
2017-04-20 14:00:43 +00:00
|
|
|
|
2017-11-25 13:24:07 +00:00
|
|
|
if (chrome.contextMenus) {
|
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) =>
|
|
|
|
contextMenus[info.menuItemId].click(info, tab));
|
|
|
|
}
|
2017-04-20 14:00:43 +00:00
|
|
|
|
2017-11-25 13:24:07 +00:00
|
|
|
if (chrome.commands) {
|
2017-04-20 14:00:43 +00:00
|
|
|
// Not available in Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1240350
|
|
|
|
chrome.commands.onCommand.addListener(command => browserCommands[command]());
|
2015-02-23 22:48:27 +00:00
|
|
|
}
|
2015-01-30 16:36:46 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
2020-10-07 07:47:51 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(({reason, previousVersion}) => {
|
2018-11-30 04:04:43 +00:00
|
|
|
// save install type: "admin", "development", "normal", "sideload" or "other"
|
|
|
|
// "normal" = addon installed from webstore
|
2018-12-31 00:01:55 +00:00
|
|
|
chrome.management.getSelf(info => {
|
|
|
|
localStorage.installType = info.installType;
|
2020-02-12 12:47:24 +00:00
|
|
|
if (reason === 'install' && info.installType === 'development' && chrome.contextMenus) {
|
|
|
|
createContextMenus(['reload']);
|
|
|
|
}
|
2018-12-31 00:01:55 +00:00
|
|
|
});
|
2018-11-30 04:04:43 +00:00
|
|
|
|
2018-08-06 15:20:57 +00:00
|
|
|
if (reason !== 'update') return;
|
|
|
|
// translations may change
|
|
|
|
localStorage.L10N = JSON.stringify({
|
|
|
|
browserUIlanguage: chrome.i18n.getUILanguage(),
|
|
|
|
});
|
|
|
|
// themes may change
|
|
|
|
delete localStorage.codeMirrorThemes;
|
2020-10-07 07:47:51 +00:00
|
|
|
// inline search cache for USO is not needed anymore, TODO: remove this by the middle of 2021
|
|
|
|
if (semverCompare(previousVersion, '1.5.13') <= 0) {
|
|
|
|
setTimeout(async () => {
|
|
|
|
const del = Object.keys(await chromeLocal.get())
|
|
|
|
.filter(key => key.startsWith('usoSearchCache'));
|
|
|
|
if (del.length) chromeLocal.remove(del);
|
|
|
|
}, 15e3);
|
|
|
|
}
|
2018-08-06 15:20:57 +00:00
|
|
|
});
|
2015-01-29 18:41:45 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
2017-03-18 22:35:27 +00:00
|
|
|
// context menus
|
2017-11-26 11:20:44 +00:00
|
|
|
contextMenus = {
|
2017-03-26 02:30:59 +00:00
|
|
|
'show-badge': {
|
|
|
|
title: 'menuShowBadge',
|
|
|
|
click: info => prefs.set(info.menuItemId, info.checked),
|
|
|
|
},
|
|
|
|
'disableAll': {
|
|
|
|
title: 'disableAllStyles',
|
|
|
|
click: browserCommands.styleDisableAll,
|
|
|
|
},
|
|
|
|
'open-manager': {
|
|
|
|
title: 'openStylesManager',
|
|
|
|
click: browserCommands.openManage,
|
|
|
|
},
|
2020-02-02 04:36:54 +00:00
|
|
|
'open-options': {
|
|
|
|
title: 'openOptions',
|
|
|
|
click: browserCommands.openOptions,
|
|
|
|
},
|
2020-02-12 12:47:24 +00:00
|
|
|
'reload': {
|
|
|
|
presentIf: () => localStorage.installType === 'development',
|
|
|
|
title: 'reload',
|
|
|
|
click: browserCommands.reload,
|
|
|
|
},
|
2017-04-29 16:54:16 +00:00
|
|
|
'editor.contextDelete': {
|
2017-11-26 11:20:44 +00:00
|
|
|
presentIf: () => !FIREFOX && prefs.get('editor.contextDelete'),
|
2017-03-26 02:30:59 +00:00
|
|
|
title: 'editDeleteText',
|
2017-04-29 16:54:16 +00:00
|
|
|
type: 'normal',
|
2017-03-26 02:30:59 +00:00
|
|
|
contexts: ['editable'],
|
2017-04-09 06:43:51 +00:00
|
|
|
documentUrlPatterns: [URLS.ownOrigin + 'edit*'],
|
2017-03-26 02:30:59 +00:00
|
|
|
click: (info, tab) => {
|
2020-10-28 21:13:15 +00:00
|
|
|
msg.sendTab(tab.id, {method: 'editDeleteText'}, undefined, 'extension')
|
|
|
|
.catch(msg.ignoreError);
|
2017-03-26 02:30:59 +00:00
|
|
|
},
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
2017-11-26 11:20:44 +00:00
|
|
|
};
|
2017-03-18 22:35:27 +00:00
|
|
|
|
2020-02-12 12:47:24 +00:00
|
|
|
function createContextMenus(ids) {
|
|
|
|
for (const id of ids) {
|
|
|
|
let item = contextMenus[id];
|
|
|
|
if (item.presentIf && !item.presentIf()) {
|
|
|
|
continue;
|
2017-04-29 16:54:16 +00:00
|
|
|
}
|
2020-02-12 12:47:24 +00:00
|
|
|
item = Object.assign({id}, item);
|
|
|
|
delete item.presentIf;
|
|
|
|
item.title = chrome.i18n.getMessage(item.title);
|
|
|
|
if (!item.type && typeof prefs.defaults[id] === 'boolean') {
|
|
|
|
item.type = 'checkbox';
|
|
|
|
item.checked = prefs.get(id);
|
|
|
|
}
|
|
|
|
if (!item.contexts) {
|
|
|
|
item.contexts = ['browser_action'];
|
|
|
|
}
|
|
|
|
delete item.click;
|
|
|
|
chrome.contextMenus.create(item, ignoreChromeError);
|
|
|
|
}
|
|
|
|
}
|
2017-12-02 13:06:57 +00:00
|
|
|
|
2020-02-12 12:47:24 +00:00
|
|
|
if (chrome.contextMenus) {
|
2020-10-28 06:53:02 +00:00
|
|
|
// "Delete" item in context menu for browsers that don't have it
|
|
|
|
if (CHROME &&
|
|
|
|
// looking at the end of UA string
|
|
|
|
/(Vivaldi|Safari)\/[\d.]+$/.test(navigator.userAgent) &&
|
|
|
|
// skip forks with Flash as those are likely to have the menu e.g. CentBrowser
|
|
|
|
!Array.from(navigator.plugins).some(p => p.name === 'Shockwave Flash')) {
|
|
|
|
prefs.defaults['editor.contextDelete'] = true;
|
|
|
|
}
|
2017-12-12 03:20:07 +00:00
|
|
|
// circumvent the bug with disabling check marks in Chrome 62-64
|
2020-05-31 05:43:56 +00:00
|
|
|
const toggleCheckmark = CHROME >= 62 && CHROME <= 64 ?
|
2017-12-02 13:06:57 +00:00
|
|
|
(id => chrome.contextMenus.remove(id, () => createContextMenus([id]) + ignoreChromeError())) :
|
|
|
|
((id, checked) => chrome.contextMenus.update(id, {checked}, ignoreChromeError));
|
|
|
|
|
2017-11-26 11:20:44 +00:00
|
|
|
const togglePresence = (id, checked) => {
|
|
|
|
if (checked) {
|
|
|
|
createContextMenus([id]);
|
2017-04-29 16:54:16 +00:00
|
|
|
} else {
|
2017-11-26 11:20:44 +00:00
|
|
|
chrome.contextMenus.remove(id, ignoreChromeError);
|
2017-04-29 16:54:16 +00:00
|
|
|
}
|
2017-11-26 11:20:44 +00:00
|
|
|
};
|
2017-12-02 13:06:57 +00:00
|
|
|
|
2017-11-26 11:20:44 +00:00
|
|
|
const keys = Object.keys(contextMenus);
|
2018-11-07 06:09:29 +00:00
|
|
|
prefs.subscribe(keys.filter(id => typeof prefs.defaults[id] === 'boolean'), toggleCheckmark);
|
2020-10-28 06:53:02 +00:00
|
|
|
prefs.subscribe(keys.filter(id => contextMenus[id].presentIf && id in prefs.defaults), togglePresence);
|
2017-11-26 11:20:44 +00:00
|
|
|
createContextMenus(keys);
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
2017-02-23 07:37:25 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
// reinject content scripts when the extension is reloaded/updated. Firefox
|
|
|
|
// would handle this automatically.
|
|
|
|
if (!FIREFOX) {
|
2018-12-31 00:01:55 +00:00
|
|
|
setTimeout(contentScripts.injectToAllTabs, 0);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2017-09-03 18:25:19 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
// register hotkeys
|
|
|
|
if (FIREFOX && browser.commands && browser.commands.update) {
|
|
|
|
const hotkeyPrefs = Object.keys(prefs.defaults).filter(k => k.startsWith('hotkey.'));
|
|
|
|
prefs.subscribe(hotkeyPrefs, (name, value) => {
|
|
|
|
try {
|
|
|
|
name = name.split('.')[1];
|
|
|
|
if (value.trim()) {
|
|
|
|
browser.commands.update({name, shortcut: value});
|
|
|
|
} else {
|
|
|
|
browser.commands.reset(name);
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
2018-01-01 17:02:49 +00:00
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2017-09-03 18:25:19 +00:00
|
|
|
|
2020-10-26 14:39:07 +00:00
|
|
|
msg.broadcast({method: 'backgroundReady'});
|
2018-01-03 07:37:54 +00:00
|
|
|
|
2018-03-08 16:19:23 +00:00
|
|
|
function webNavIframeHelperFF({tabId, frameId}) {
|
|
|
|
if (!frameId) return;
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.sendTab(tabId, {method: 'ping'}, {frameId})
|
|
|
|
.catch(() => false)
|
|
|
|
.then(pong => {
|
|
|
|
if (pong) return;
|
|
|
|
// insert apply.js to iframe
|
|
|
|
const files = chrome.runtime.getManifest().content_scripts[0].js;
|
|
|
|
for (const file of files) {
|
|
|
|
chrome.tabs.executeScript(tabId, {
|
|
|
|
frameId,
|
|
|
|
file,
|
|
|
|
matchAboutBlank: true,
|
|
|
|
}, ignoreChromeError);
|
|
|
|
}
|
|
|
|
});
|
2018-03-08 16:19:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function onRuntimeMessage(msg, sender) {
|
|
|
|
if (msg.method !== 'invokeAPI') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const fn = window.API_METHODS[msg.name];
|
|
|
|
if (!fn) {
|
|
|
|
throw new Error(`unknown API: ${msg.name}`);
|
|
|
|
}
|
2020-11-01 19:46:47 +00:00
|
|
|
const res = fn.apply({msg, sender}, msg.args);
|
|
|
|
return res === undefined ? null : res;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2020-02-02 04:36:54 +00:00
|
|
|
function openEditor(params) {
|
|
|
|
/* Open the editor. Activate if it is already opened
|
|
|
|
|
|
|
|
params: {
|
|
|
|
id?: Number,
|
|
|
|
domain?: String,
|
|
|
|
'url-prefix'?: String
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2020-02-02 04:36:54 +00:00
|
|
|
*/
|
2020-10-13 18:14:54 +00:00
|
|
|
const u = new URL(chrome.runtime.getURL('edit.html'));
|
|
|
|
u.search = new URLSearchParams(params);
|
2020-02-02 04:36:54 +00:00
|
|
|
return openURL({
|
2020-10-13 18:14:54 +00:00
|
|
|
url: `${u}`,
|
2020-10-18 13:37:42 +00:00
|
|
|
currentWindow: null,
|
|
|
|
newWindow: prefs.get('openEditInWindow') && Object.assign({},
|
|
|
|
prefs.get('openEditInWindow.popup') && {type: 'popup'},
|
|
|
|
prefs.get('windowPosition')),
|
2020-02-02 04:36:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function openManage({options = false, search} = {}) {
|
|
|
|
let url = chrome.runtime.getURL('manage.html');
|
|
|
|
if (search) {
|
|
|
|
url += `?search=${encodeURIComponent(search)}`;
|
|
|
|
}
|
|
|
|
if (options) {
|
|
|
|
url += '#stylus-options';
|
|
|
|
}
|
2020-02-20 22:54:54 +00:00
|
|
|
return findExistingTab({
|
2020-02-02 04:36:54 +00:00
|
|
|
url,
|
|
|
|
currentWindow: null,
|
|
|
|
ignoreHash: true,
|
|
|
|
ignoreSearch: true
|
|
|
|
})
|
|
|
|
.then(tab => {
|
|
|
|
if (tab) {
|
|
|
|
return Promise.all([
|
|
|
|
activateTab(tab),
|
2020-09-22 10:54:48 +00:00
|
|
|
(tab.pendingUrl || tab.url) !== url && msg.sendTab(tab.id, {method: 'pushState', url})
|
2020-02-02 04:36:54 +00:00
|
|
|
.catch(console.error)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return getActiveTab().then(tab => {
|
|
|
|
if (isTabReplaceable(tab, url)) {
|
|
|
|
return activateTab(tab, {url});
|
|
|
|
}
|
2020-08-14 12:16:01 +00:00
|
|
|
return browser.tabs.create({url});
|
2020-02-02 04:36:54 +00:00
|
|
|
});
|
|
|
|
});
|
2017-09-01 10:21:45 +00:00
|
|
|
}
|