2017-04-25 21:48:27 +00:00
|
|
|
/* global dbExec, getStyles, saveStyle */
|
2017-09-03 08:12:18 +00:00
|
|
|
/* global handleCssTransitionBug */
|
2017-10-06 08:29:06 +00:00
|
|
|
/* global usercssHelper openEditor */
|
2017-11-13 12:28:50 +00:00
|
|
|
/* global styleViaAPI */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-15 12:41:39 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var browserCommands, contextMenus;
|
2017-03-27 09:11:29 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// register all listeners
|
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
2017-03-27 09:11:29 +00:00
|
|
|
|
2017-07-27 14:37:05 +00:00
|
|
|
{
|
2017-11-25 00:19:30 +00:00
|
|
|
const [listener] = [
|
|
|
|
[webNavigationListenerChrome, CHROME],
|
|
|
|
[webNavigationListenerFF, FIREFOX],
|
|
|
|
[webNavigationListener, true],
|
|
|
|
].find(([, selected]) => selected);
|
2017-07-27 14:37:05 +00:00
|
|
|
|
|
|
|
chrome.webNavigation.onBeforeNavigate.addListener(data =>
|
|
|
|
listener(null, data));
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-07-27 14:37:05 +00:00
|
|
|
chrome.webNavigation.onCommitted.addListener(data =>
|
|
|
|
listener('styleApply', data));
|
2017-04-20 14:00:43 +00:00
|
|
|
|
2017-07-27 14:37:05 +00:00
|
|
|
chrome.webNavigation.onHistoryStateUpdated.addListener(data =>
|
|
|
|
listener('styleReplaceAll', data));
|
2017-04-20 14:00:43 +00:00
|
|
|
|
2017-07-27 14:37:05 +00:00
|
|
|
chrome.webNavigation.onReferenceFragmentUpdated.addListener(data =>
|
|
|
|
listener('styleReplaceAll', data));
|
2017-11-24 16:33:50 +00:00
|
|
|
|
|
|
|
if (FIREFOX) {
|
|
|
|
// FF applies page CSP even to content scripts, https://bugzil.la/1267027
|
|
|
|
chrome.webNavigation.onCommitted.addListener(webNavUsercssInstallerFF, {
|
|
|
|
url: [
|
|
|
|
{urlPrefix: 'https://raw.githubusercontent.com/', urlSuffix: '.user.css'},
|
|
|
|
{urlPrefix: 'https://raw.githubusercontent.com/', urlSuffix: '.user.styl'},
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
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));
|
|
|
|
}
|
|
|
|
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-11-25 13:24:07 +00:00
|
|
|
if (!chrome.browserAction ||
|
|
|
|
!['setIcon', 'setBadgeBackgroundColor', 'setBadgeText'].every(name => chrome.browserAction[name])) {
|
|
|
|
window.updateIcon = () => {};
|
|
|
|
}
|
|
|
|
|
2017-06-28 12:48:13 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// set the default icon displayed after a tab is created until webNavigation kicks in
|
2017-09-03 17:06:20 +00:00
|
|
|
prefs.subscribe(['iconset'], () => updateIcon({id: undefined}, {}));
|
2017-06-28 12:48:13 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
2017-04-24 13:26:59 +00:00
|
|
|
{
|
|
|
|
const onInstall = ({reason}) => {
|
|
|
|
chrome.runtime.onInstalled.removeListener(onInstall);
|
|
|
|
const manifest = chrome.runtime.getManifest();
|
|
|
|
// Open FAQs page once after installation to guide new users.
|
|
|
|
// Do not display it in development mode.
|
2017-07-16 18:02:00 +00:00
|
|
|
if (reason === 'install' && manifest.update_url) {
|
2017-11-21 06:48:55 +00:00
|
|
|
// don't hardcode homepage URL, extract it from "Get Help" label translation
|
|
|
|
// TODO: add a built-in tour page in the extension
|
|
|
|
const getHelpHtml = chrome.i18n.getMessage('manageText').match(/<a\s+href=[^>]+/g);
|
|
|
|
const url = (getHelpHtml[1] || '').replace(/^.+?=\s*/, '').replace(/^['"]|["']$/g, '');
|
|
|
|
if (url) {
|
|
|
|
setTimeout(openURL, 100, {url});
|
|
|
|
}
|
2017-04-24 13:26:59 +00:00
|
|
|
}
|
2017-07-11 15:03:35 +00:00
|
|
|
// reset L10N cache on update
|
2017-07-16 18:02:00 +00:00
|
|
|
if (reason === 'update') {
|
2017-04-24 13:26:59 +00:00
|
|
|
localStorage.L10N = JSON.stringify({
|
2017-07-11 15:03:35 +00:00
|
|
|
browserUIlanguage: chrome.i18n.getUILanguage(),
|
2017-04-20 14:00:43 +00:00
|
|
|
});
|
|
|
|
}
|
2017-11-25 00:19:30 +00:00
|
|
|
if (!FIREFOX && chrome.declarativeContent) {
|
|
|
|
chrome.declarativeContent.onPageChanged.removeRules(null, () => {
|
|
|
|
chrome.declarativeContent.onPageChanged.addRules([{
|
|
|
|
conditions: [
|
|
|
|
new chrome.declarativeContent.PageStateMatcher({
|
|
|
|
pageUrl: {urlContains: ':'},
|
|
|
|
})
|
|
|
|
],
|
|
|
|
actions: [
|
|
|
|
new chrome.declarativeContent.RequestContentScript({
|
|
|
|
js: ['/content/apply.js'],
|
|
|
|
allFrames: true,
|
|
|
|
matchAboutBlank: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
}
|
2017-04-20 14:00:43 +00:00
|
|
|
};
|
|
|
|
// bind for 60 seconds max and auto-unbind if it's a normal run
|
2017-04-24 13:26:59 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(onInstall);
|
|
|
|
setTimeout(onInstall, 60e3, {reason: 'unbindme'});
|
2017-03-18 22:35:27 +00:00
|
|
|
}
|
2015-01-29 18:41:45 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// browser commands
|
|
|
|
browserCommands = {
|
2017-03-26 02:30:59 +00:00
|
|
|
openManage() {
|
2017-07-12 18:52:44 +00:00
|
|
|
openURL({url: 'manage.html'});
|
2017-03-26 02:30:59 +00:00
|
|
|
},
|
2017-04-20 14:00:43 +00:00
|
|
|
styleDisableAll(info) {
|
|
|
|
prefs.set('disableAll', info ? info.checked : !prefs.get('disableAll'));
|
2017-03-26 02:30:59 +00:00
|
|
|
},
|
2017-03-18 22:35:27 +00:00
|
|
|
};
|
2015-03-24 14:07:59 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
2017-03-18 22:35:27 +00:00
|
|
|
// context menus
|
2017-04-20 14:00:43 +00:00
|
|
|
contextMenus = Object.assign({
|
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,
|
|
|
|
},
|
2017-07-31 16:39:10 +00:00
|
|
|
}, !FIREFOX && prefs.get('editor.contextDelete') && {
|
2017-04-29 16:54:16 +00:00
|
|
|
'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) => {
|
2017-11-24 15:26:20 +00:00
|
|
|
sendMessage(tab.id, {method: 'editDeleteText'});
|
2017-03-26 02:30:59 +00:00
|
|
|
},
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-18 22:35:27 +00:00
|
|
|
|
2017-11-25 13:24:07 +00:00
|
|
|
if (chrome.contextMenus) {
|
2017-04-29 16:54:16 +00:00
|
|
|
const createContextMenus = (ids = Object.keys(contextMenus)) => {
|
|
|
|
for (const id of ids) {
|
|
|
|
const item = Object.assign({id}, contextMenus[id]);
|
|
|
|
const prefValue = prefs.readOnlyValues[id];
|
|
|
|
item.title = chrome.i18n.getMessage(item.title);
|
2017-07-16 18:02:00 +00:00
|
|
|
if (!item.type && typeof prefValue === 'boolean') {
|
2017-04-29 16:54:16 +00:00
|
|
|
item.type = 'checkbox';
|
|
|
|
item.checked = prefValue;
|
|
|
|
}
|
|
|
|
if (!item.contexts) {
|
|
|
|
item.contexts = ['browser_action'];
|
|
|
|
}
|
|
|
|
delete item.click;
|
|
|
|
chrome.contextMenus.create(item, ignoreChromeError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
createContextMenus();
|
2017-09-03 17:06:20 +00:00
|
|
|
const toggleableIds = Object.keys(contextMenus).filter(key =>
|
|
|
|
typeof prefs.readOnlyValues[key] === 'boolean');
|
|
|
|
prefs.subscribe(toggleableIds, (id, checked) => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if (id === 'editor.contextDelete') {
|
2017-04-29 16:54:16 +00:00
|
|
|
if (checked) {
|
|
|
|
createContextMenus([id]);
|
|
|
|
} else {
|
|
|
|
chrome.contextMenus.remove(id, ignoreChromeError);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chrome.contextMenus.update(id, {checked}, ignoreChromeError);
|
|
|
|
}
|
2017-09-03 17:06:20 +00:00
|
|
|
});
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
2017-02-23 07:37:25 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// [re]inject content scripts
|
2017-09-03 18:25:19 +00:00
|
|
|
window.addEventListener('storageReady', function _() {
|
|
|
|
window.removeEventListener('storageReady', _);
|
|
|
|
|
|
|
|
updateIcon({id: undefined}, {});
|
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
const NTP = 'chrome://newtab/';
|
|
|
|
const ALL_URLS = '<all_urls>';
|
2017-04-09 06:43:51 +00:00
|
|
|
const contentScripts = chrome.runtime.getManifest().content_scripts;
|
2017-11-25 00:19:30 +00:00
|
|
|
if (!FIREFOX) {
|
|
|
|
contentScripts.push({
|
|
|
|
js: ['content/apply.js'],
|
|
|
|
matches: ['<all_urls>'],
|
|
|
|
run_at: 'document_start',
|
|
|
|
match_about_blank: true,
|
|
|
|
all_frames: true
|
|
|
|
});
|
|
|
|
}
|
2017-04-20 14:00:43 +00:00
|
|
|
// expand * as .*?
|
|
|
|
const wildcardAsRegExp = (s, flags) => new RegExp(
|
|
|
|
s.replace(/[{}()[\]/\\.+?^$:=!|]/g, '\\$&')
|
|
|
|
.replace(/\*/g, '.*?'), flags);
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const cs of contentScripts) {
|
|
|
|
cs.matches = cs.matches.map(m => (
|
2017-07-16 18:02:00 +00:00
|
|
|
m === ALL_URLS ? m : wildcardAsRegExp(m)
|
2017-03-26 02:30:59 +00:00
|
|
|
));
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
const injectCS = (cs, tabId) => {
|
|
|
|
chrome.tabs.executeScript(tabId, {
|
|
|
|
file: cs.js[0],
|
|
|
|
runAt: cs.run_at,
|
|
|
|
allFrames: cs.all_frames,
|
|
|
|
matchAboutBlank: cs.match_about_blank,
|
|
|
|
}, ignoreChromeError);
|
|
|
|
};
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
const pingCS = (cs, {id, url}) => {
|
2017-11-25 21:04:19 +00:00
|
|
|
const maybeInject = pong => !pong && injectCS(cs, id);
|
2017-04-20 14:00:43 +00:00
|
|
|
cs.matches.some(match => {
|
2017-11-24 15:26:20 +00:00
|
|
|
if ((match === ALL_URLS || url.match(match)) &&
|
|
|
|
(!url.startsWith('chrome') || url === NTP)) {
|
2017-11-25 21:04:19 +00:00
|
|
|
sendMessage({method: 'ping', tabId: id}, maybeInject);
|
2017-04-20 14:00:43 +00:00
|
|
|
return true;
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
});
|
2017-04-20 14:00:43 +00:00
|
|
|
};
|
|
|
|
|
2017-06-17 10:00:10 +00:00
|
|
|
queryTabs().then(tabs =>
|
2017-06-17 05:49:12 +00:00
|
|
|
tabs.forEach(tab => {
|
2017-11-25 00:19:30 +00:00
|
|
|
if (FIREFOX) {
|
|
|
|
const tabId = tab.id;
|
|
|
|
const frameUrls = {'0': tab.url};
|
|
|
|
styleViaAPI.allFrameUrls.set(tabId, frameUrls);
|
|
|
|
chrome.webNavigation.getAllFrames({tabId}, frames => frames &&
|
|
|
|
frames.forEach(({frameId, parentFrameId, url}) => {
|
|
|
|
if (frameId) {
|
|
|
|
frameUrls[frameId] = url === 'about:blank' ? frameUrls[parentFrameId] : url;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} else if (tab.width) {
|
|
|
|
// skip lazy-loaded aka unloaded tabs that seem to start loading on message
|
2017-06-17 05:49:12 +00:00
|
|
|
contentScripts.forEach(cs =>
|
|
|
|
setTimeout(pingCS, 0, cs, tab));
|
|
|
|
}
|
|
|
|
}));
|
2017-09-03 18:25:19 +00:00
|
|
|
});
|
2017-04-20 14:00:43 +00:00
|
|
|
|
|
|
|
// *************************************************************************
|
|
|
|
|
|
|
|
function webNavigationListener(method, {url, tabId, frameId}) {
|
2017-04-25 21:48:27 +00:00
|
|
|
getStyles({matchUrl: url, enabled: true, asHash: true}).then(styles => {
|
2017-09-03 08:12:18 +00:00
|
|
|
if (method && URLS.supported(url) && tabId >= 0) {
|
|
|
|
if (method === 'styleApply') {
|
2017-09-03 20:52:06 +00:00
|
|
|
handleCssTransitionBug({tabId, frameId, url, styles});
|
2017-09-03 08:12:18 +00:00
|
|
|
}
|
2017-11-24 15:26:20 +00:00
|
|
|
sendMessage({
|
|
|
|
tabId,
|
|
|
|
frameId,
|
2017-04-20 14:00:43 +00:00
|
|
|
method,
|
|
|
|
// ping own page so it retrieves the styles directly
|
|
|
|
styles: url.startsWith(URLS.ownOrigin) ? 'DIY' : styles,
|
2017-11-24 15:26:20 +00:00
|
|
|
});
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
|
|
|
// main page frame id is 0
|
2017-07-16 18:02:00 +00:00
|
|
|
if (frameId === 0) {
|
2017-04-20 14:00:43 +00:00
|
|
|
updateIcon({id: tabId, url}, styles);
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-27 14:37:05 +00:00
|
|
|
function webNavigationListenerChrome(method, data) {
|
2017-11-25 00:19:30 +00:00
|
|
|
const {tabId, frameId, url} = data;
|
|
|
|
if (url.startsWith('https://www.google.') && url.includes('/_/chrome/newtab?')) {
|
|
|
|
// Chrome 61.0.3161+ doesn't run content scripts on NTP
|
|
|
|
getTab(tabId).then(tab => {
|
|
|
|
data.url = tab.url === 'chrome://newtab/' ? tab.url : url;
|
|
|
|
webNavigationListener(method, data);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
webNavigationListener(method, data);
|
|
|
|
// chrome.declarativeContent doesn't inject scripts in about:blank iframes
|
|
|
|
if (method && frameId && url === 'about:blank') {
|
|
|
|
chrome.tabs.executeScript(tabId, {
|
|
|
|
file: '/content/apply.js',
|
|
|
|
runAt: 'document_start',
|
|
|
|
matchAboutBlank: true,
|
|
|
|
frameId,
|
|
|
|
}, ignoreChromeError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function webNavigationListenerFF(method, data) {
|
|
|
|
const {tabId, frameId, url} = data;
|
|
|
|
if (url !== 'about:blank' || !frameId) {
|
|
|
|
styleViaAPI.setFrameUrl(tabId, frameId, url);
|
2017-07-27 14:37:05 +00:00
|
|
|
webNavigationListener(method, data);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-25 00:19:30 +00:00
|
|
|
const frames = styleViaAPI.allFrameUrls.get(tabId);
|
|
|
|
if (Object.keys(frames).length === 1) {
|
|
|
|
frames[frameId] = frames['0'];
|
|
|
|
webNavigationListener(method, data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chrome.webNavigation.getFrame({tabId, frameId}, info => {
|
|
|
|
const hasParent = !chrome.runtime.lastError && info.parentFrameId >= 0;
|
|
|
|
frames[frameId] = hasParent ? frames[info.parentFrameId] : url;
|
2017-07-27 14:37:05 +00:00
|
|
|
webNavigationListener(method, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-24 16:33:50 +00:00
|
|
|
function webNavUsercssInstallerFF(data) {
|
|
|
|
const {tabId} = data;
|
2017-11-25 00:19:30 +00:00
|
|
|
// we need tab index to open the installer next to the original one
|
|
|
|
// and also to skip the double-invocation in FF which assigns tab url later
|
|
|
|
getTab(tabId).then(tab => {
|
|
|
|
if (tab.url !== 'about:blank') {
|
2017-11-24 16:33:50 +00:00
|
|
|
usercssHelper.openInstallPage(tab, {direct: true});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function updateIcon(tab, styles) {
|
2017-04-17 15:56:37 +00:00
|
|
|
if (tab.id < 0) {
|
2017-04-11 10:51:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-27 14:37:05 +00:00
|
|
|
if (URLS.chromeProtectsNTP && tab.url === 'chrome://newtab/') {
|
|
|
|
styles = {};
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
if (styles) {
|
2017-04-23 10:54:20 +00:00
|
|
|
stylesReceived(styles);
|
2017-04-11 10:51:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-25 21:48:27 +00:00
|
|
|
getTabRealURL(tab)
|
|
|
|
.then(url => getStyles({matchUrl: url, enabled: true, asHash: true}))
|
|
|
|
.then(stylesReceived);
|
2017-04-11 10:51:40 +00:00
|
|
|
|
|
|
|
function stylesReceived(styles) {
|
|
|
|
let numStyles = styles.length;
|
|
|
|
if (numStyles === undefined) {
|
|
|
|
// for 'styles' asHash:true fake the length by counting numeric ids manually
|
|
|
|
numStyles = 0;
|
|
|
|
for (const id of Object.keys(styles)) {
|
|
|
|
numStyles += id.match(/^\d+$/) ? 1 : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const disableAll = 'disableAll' in styles ? styles.disableAll : prefs.get('disableAll');
|
2017-07-16 18:02:00 +00:00
|
|
|
const postfix = disableAll ? 'x' : numStyles === 0 ? 'w' : '';
|
2017-04-11 10:51:40 +00:00
|
|
|
const color = prefs.get(disableAll ? 'badgeDisabled' : 'badgeNormal');
|
|
|
|
const text = prefs.get('show-badge') && numStyles ? String(numStyles) : '';
|
2017-06-28 10:49:04 +00:00
|
|
|
const iconset = ['', 'light/'][prefs.get('iconset')] || '';
|
|
|
|
const path = 'images/icon/' + iconset;
|
2017-04-11 10:51:40 +00:00
|
|
|
chrome.browserAction.setIcon({
|
|
|
|
tabId: tab.id,
|
|
|
|
path: {
|
|
|
|
// Material Design 2016 new size is 16px
|
2017-06-28 10:49:04 +00:00
|
|
|
16: `${path}16${postfix}.png`,
|
|
|
|
32: `${path}32${postfix}.png`,
|
2017-04-11 10:51:40 +00:00
|
|
|
// Chromium forks or non-chromium browsers may still use the traditional 19px
|
2017-06-28 10:49:04 +00:00
|
|
|
19: `${path}19${postfix}.png`,
|
|
|
|
38: `${path}38${postfix}.png`,
|
2017-04-11 10:51:40 +00:00
|
|
|
// TODO: add Edge preferred sizes: 20, 25, 30, 40
|
|
|
|
},
|
|
|
|
}, () => {
|
2017-06-28 12:48:13 +00:00
|
|
|
if (chrome.runtime.lastError || tab.id === undefined) {
|
2017-04-23 10:54:20 +00:00
|
|
|
return;
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2017-04-23 10:54:20 +00:00
|
|
|
// Vivaldi bug workaround: setBadgeText must follow setBadgeBackgroundColor
|
|
|
|
chrome.browserAction.setBadgeBackgroundColor({color});
|
2017-07-22 13:57:52 +00:00
|
|
|
getTab(tab.id).then(realTab => {
|
2017-07-22 04:39:43 +00:00
|
|
|
// skip pre-rendered tabs
|
2017-07-22 13:57:52 +00:00
|
|
|
if (realTab.index >= 0) {
|
2017-07-22 04:39:43 +00:00
|
|
|
chrome.browserAction.setBadgeText({text, tabId: tab.id});
|
|
|
|
}
|
2017-04-23 10:54:20 +00:00
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-24 15:26:20 +00:00
|
|
|
function onRuntimeMessage(request, sender, sendResponseInternal) {
|
|
|
|
const sendResponse = data => {
|
|
|
|
// wrap Error object instance as {__ERROR__: message} - will be unwrapped in sendMessage
|
|
|
|
if (data instanceof Error) {
|
|
|
|
data = {__ERROR__: data.message};
|
|
|
|
}
|
|
|
|
// prevent browser exception bug on sending a response to a closed tab
|
|
|
|
tryCatch(sendResponseInternal, data);
|
|
|
|
};
|
2017-04-20 14:00:43 +00:00
|
|
|
switch (request.method) {
|
|
|
|
case 'getStyles':
|
2017-04-25 21:48:27 +00:00
|
|
|
getStyles(request).then(sendResponse);
|
2017-04-20 14:00:43 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
case 'saveStyle':
|
|
|
|
saveStyle(request).then(sendResponse);
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
2017-08-05 16:49:25 +00:00
|
|
|
case 'saveUsercss':
|
2017-09-18 03:34:12 +00:00
|
|
|
usercssHelper.save(request, true).then(sendResponse);
|
2017-08-05 16:49:25 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
2017-09-18 03:34:12 +00:00
|
|
|
case 'buildUsercss':
|
|
|
|
usercssHelper.build(request, true).then(sendResponse);
|
2017-08-05 16:49:25 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
case 'healthCheck':
|
2017-04-25 21:48:27 +00:00
|
|
|
dbExec()
|
|
|
|
.then(() => sendResponse(true))
|
|
|
|
.catch(() => sendResponse(false));
|
2017-04-20 14:00:43 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
case 'download':
|
|
|
|
download(request.url)
|
|
|
|
.then(sendResponse)
|
|
|
|
.catch(() => sendResponse(null));
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
2017-09-01 10:21:45 +00:00
|
|
|
|
2017-09-24 03:39:04 +00:00
|
|
|
case 'openUsercssInstallPage':
|
2017-10-01 12:34:29 +00:00
|
|
|
usercssHelper.openInstallPage(sender.tab, request).then(sendResponse);
|
2017-09-24 03:39:04 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
2017-10-06 08:29:06 +00:00
|
|
|
case 'closeTab':
|
2017-11-24 15:39:15 +00:00
|
|
|
chrome.tabs.remove(request.tabId || sender.tab.id, () => {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
sendResponse(new Error(chrome.runtime.lastError.message));
|
|
|
|
}
|
|
|
|
});
|
2017-10-06 08:29:06 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
case 'openEditor':
|
|
|
|
openEditor(request.id);
|
|
|
|
return;
|
2017-09-01 10:21:45 +00:00
|
|
|
}
|
|
|
|
}
|