2018-01-01 17:02:49 +00:00
|
|
|
/*
|
|
|
|
global dbExec getStyles saveStyle deleteStyle
|
|
|
|
global handleCssTransitionBug detectSloppyRegexps
|
|
|
|
global openEditor
|
|
|
|
global styleViaAPI
|
|
|
|
global loadScript
|
|
|
|
global updater
|
|
|
|
*/
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-15 12:41:39 +00:00
|
|
|
|
2018-01-04 14:04:23 +00:00
|
|
|
window.API_METHODS = Object.assign(window.API_METHODS || {}, {
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
getStyles,
|
|
|
|
saveStyle,
|
|
|
|
deleteStyle,
|
|
|
|
|
|
|
|
download: msg => download(msg.url),
|
|
|
|
getPrefs: () => prefs.getAll(),
|
|
|
|
healthCheck: () => dbExec().then(() => true),
|
|
|
|
|
|
|
|
detectSloppyRegexps,
|
|
|
|
openEditor,
|
|
|
|
updateIcon,
|
2018-01-03 15:26:31 +00:00
|
|
|
refreshAllTabs,
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
closeTab: (msg, sender, respond) => {
|
|
|
|
chrome.tabs.remove(msg.tabId || sender.tab.id, () => {
|
|
|
|
if (chrome.runtime.lastError && msg.tabId !== sender.tab.id) {
|
|
|
|
respond(new Error(chrome.runtime.lastError.message));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
},
|
2018-01-04 14:04:23 +00:00
|
|
|
});
|
2018-01-01 17:02:49 +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
|
|
|
{
|
|
|
|
const listener =
|
|
|
|
URLS.chromeProtectsNTP
|
|
|
|
? webNavigationListenerChrome
|
|
|
|
: webNavigationListener;
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
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-11-25 13:24:07 +00:00
|
|
|
if (!chrome.browserAction ||
|
|
|
|
!['setIcon', 'setBadgeBackgroundColor', 'setBadgeText'].every(name => chrome.browserAction[name])) {
|
|
|
|
window.updateIcon = () => {};
|
|
|
|
}
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
const tabIcons = new Map();
|
|
|
|
chrome.tabs.onRemoved.addListener(tabId => tabIcons.delete(tabId));
|
|
|
|
chrome.tabs.onReplaced.addListener((added, removed) => tabIcons.delete(removed));
|
|
|
|
|
2017-06-28 12:48:13 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// set the default icon displayed after a tab is created until webNavigation kicks in
|
2018-01-01 17:02:49 +00:00
|
|
|
prefs.subscribe(['iconset'], () =>
|
|
|
|
updateIcon({
|
|
|
|
tab: {id: undefined},
|
|
|
|
styles: {},
|
|
|
|
}));
|
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);
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// 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-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,
|
|
|
|
},
|
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) => {
|
2017-11-26 11:20:44 +00:00
|
|
|
sendMessage({tabId: tab.id, method: 'editDeleteText'});
|
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
|
|
|
|
2017-11-25 13:24:07 +00:00
|
|
|
if (chrome.contextMenus) {
|
2017-11-26 11:20:44 +00:00
|
|
|
const createContextMenus = ids => {
|
2017-04-29 16:54:16 +00:00
|
|
|
for (const id of ids) {
|
2017-11-26 11:20:44 +00:00
|
|
|
let item = contextMenus[id];
|
|
|
|
if (item.presentIf && !item.presentIf()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
item = Object.assign({id}, item);
|
|
|
|
delete item.presentIf;
|
2017-04-29 16:54:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2017-12-02 13:06:57 +00:00
|
|
|
|
2017-12-12 03:20:07 +00:00
|
|
|
// circumvent the bug with disabling check marks in Chrome 62-64
|
|
|
|
const toggleCheckmark = CHROME >= 3172 && CHROME <= 3288 ?
|
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);
|
|
|
|
prefs.subscribe(keys.filter(id => typeof prefs.readOnlyValues[id] === 'boolean'), toggleCheckmark);
|
|
|
|
prefs.subscribe(keys.filter(id => contextMenus[id].presentIf), togglePresence);
|
|
|
|
createContextMenus(keys);
|
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', _);
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
updateIcon({
|
|
|
|
tab: {id: undefined},
|
|
|
|
styles: {},
|
|
|
|
});
|
2017-09-03 18:25:19 +00:00
|
|
|
|
2018-01-03 07:37:54 +00:00
|
|
|
// Firefox injects content script automatically
|
|
|
|
if (FIREFOX) return;
|
|
|
|
|
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-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 => {
|
|
|
|
// skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF
|
2018-01-03 07:37:54 +00:00
|
|
|
if (tab.width) {
|
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}) {
|
2018-01-10 16:51:05 +00:00
|
|
|
getStyles({matchUrl: url, 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) {
|
2018-01-01 17:02:49 +00:00
|
|
|
tabIcons.delete(tabId);
|
|
|
|
updateIcon({tab: {id: tabId, url}, styles});
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-27 14:37:05 +00:00
|
|
|
function webNavigationListenerChrome(method, data) {
|
|
|
|
// Chrome 61.0.3161+ doesn't run content scripts on NTP
|
|
|
|
if (
|
|
|
|
!data.url.startsWith('https://www.google.') ||
|
|
|
|
!data.url.includes('/_/chrome/newtab?')
|
|
|
|
) {
|
|
|
|
webNavigationListener(method, data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
getTab(data.tabId).then(tab => {
|
|
|
|
if (tab.url === 'chrome://newtab/') {
|
|
|
|
data.url = tab.url;
|
|
|
|
}
|
|
|
|
webNavigationListener(method, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-24 16:33:50 +00:00
|
|
|
function webNavUsercssInstallerFF(data) {
|
|
|
|
const {tabId} = data;
|
|
|
|
Promise.all([
|
|
|
|
sendMessage({tabId, method: 'ping'}),
|
|
|
|
// 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(([pong, tab]) => {
|
|
|
|
if (pong !== true && tab.url !== 'about:blank') {
|
2018-01-07 08:22:20 +00:00
|
|
|
window.API_METHODS.installUsercss({direct: true}, {tab});
|
2017-11-24 16:33:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-03 15:26:31 +00:00
|
|
|
function refreshAllTabs(msg, sender = {}) {
|
|
|
|
return Promise.all([
|
|
|
|
sender.tab || getActiveTab(),
|
|
|
|
queryTabs(),
|
|
|
|
]).then(([ownTab, tabs]) => new Promise(resolve => {
|
|
|
|
if (FIREFOX) tabs = tabs.filter(tab => tab.width);
|
|
|
|
const last = tabs.length - 1;
|
|
|
|
for (let i = 0; i < last; i++) {
|
|
|
|
refreshTab(tabs[i], ownTab);
|
|
|
|
}
|
|
|
|
if (tabs.length) {
|
|
|
|
refreshTab(tabs[last], ownTab, resolve);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
function refreshTab(tab, ownTab, resolve) {
|
|
|
|
const {id: tabId, url: matchUrl} = tab;
|
|
|
|
chrome.webNavigation.getAllFrames({tabId}, (frames = []) => {
|
|
|
|
ignoreChromeError();
|
|
|
|
for (const {frameId} of frames[0] ? frames : [{frameId: 0}]) {
|
|
|
|
getStyles({matchUrl, enabled: true, asHash: true}).then(styles => {
|
|
|
|
const message = {method: 'styleReplaceAll', tabId, frameId, styles};
|
|
|
|
invokeOrPostpone(tab.active, sendMessage, message, ignoreChromeError);
|
|
|
|
if (!frameId) setTimeout(updateIcon, 0, {tab, styles});
|
|
|
|
if (resolve) resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-01 17:02:49 +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)
|
2018-01-10 16:51:05 +00:00
|
|
|
.then(url => getStyles({matchUrl: url, asHash: true}))
|
2017-04-25 21:48:27 +00:00
|
|
|
.then(stylesReceived);
|
2017-04-11 10:51:40 +00:00
|
|
|
|
|
|
|
function stylesReceived(styles) {
|
|
|
|
const disableAll = 'disableAll' in styles ? styles.disableAll : prefs.get('disableAll');
|
2018-01-10 16:51:05 +00:00
|
|
|
const postfix = disableAll ? 'x' : !styles.length ? 'w' : '';
|
2017-04-11 10:51:40 +00:00
|
|
|
const color = prefs.get(disableAll ? 'badgeDisabled' : 'badgeNormal');
|
2018-01-10 16:51:05 +00:00
|
|
|
const text = prefs.get('show-badge') && styles.length ? String(styles.length) : '';
|
2017-06-28 10:49:04 +00:00
|
|
|
const iconset = ['', 'light/'][prefs.get('iconset')] || '';
|
2018-01-03 07:39:40 +00:00
|
|
|
|
|
|
|
let tabIcon = tabIcons.get(tab.id);
|
|
|
|
if (!tabIcon) tabIcons.set(tab.id, (tabIcon = {}));
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
if (tabIcon.iconType !== iconset + postfix) {
|
|
|
|
tabIcon.iconType = iconset + postfix;
|
2018-01-03 07:39:40 +00:00
|
|
|
const sizes = FIREFOX || CHROME >= 2883 && !VIVALDI ? [16, 32] : [19, 38];
|
|
|
|
Promise.all(sizes.map(size => {
|
|
|
|
const src = `/images/icon/${iconset}${size}${postfix}.png`;
|
|
|
|
return tabIcons.get(src) || loadIcon(src);
|
|
|
|
})).then(data => {
|
|
|
|
const imageData = {};
|
|
|
|
sizes.forEach((size, i) => (imageData[size] = data[i]));
|
|
|
|
chrome.browserAction.setIcon({tabId: tab.id, imageData}, ignoreChromeError);
|
|
|
|
});
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
|
|
|
if (tab.id === undefined) return;
|
2018-01-03 07:39:40 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
let defaultIcon = tabIcons.get(undefined);
|
|
|
|
if (!defaultIcon) tabIcons.set(undefined, (defaultIcon = {}));
|
|
|
|
if (defaultIcon.color !== color) {
|
|
|
|
defaultIcon.color = color;
|
2017-04-23 10:54:20 +00:00
|
|
|
chrome.browserAction.setBadgeBackgroundColor({color});
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
2018-01-03 07:39:40 +00:00
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
if (tabIcon.text !== text) {
|
|
|
|
tabIcon.text = text;
|
2017-11-25 18:47:02 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
getTab(tab.id).then(realTab => {
|
|
|
|
// skip pre-rendered tabs
|
|
|
|
if (realTab.index >= 0) {
|
|
|
|
chrome.browserAction.setBadgeText({text, tabId: tab.id});
|
|
|
|
}
|
|
|
|
});
|
2017-04-23 10:54:20 +00:00
|
|
|
});
|
2018-01-01 17:02:49 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2018-01-03 07:39:40 +00:00
|
|
|
|
|
|
|
function loadIcon(src, resolve) {
|
|
|
|
if (!resolve) return new Promise(resolve => loadIcon(src, resolve));
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
const img = new Image();
|
|
|
|
img.src = src;
|
|
|
|
img.onload = () => {
|
|
|
|
const w = canvas.width = img.width;
|
|
|
|
const h = canvas.height = img.height;
|
|
|
|
ctx.clearRect(0, 0, w, h);
|
|
|
|
ctx.drawImage(img, 0, 0, w, h);
|
|
|
|
const data = ctx.getImageData(0, 0, w, h);
|
|
|
|
tabIcons.set(src, data);
|
|
|
|
resolve(data);
|
|
|
|
};
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-01 17:02:49 +00:00
|
|
|
function onRuntimeMessage(msg, sender, sendResponse) {
|
2018-01-07 08:22:20 +00:00
|
|
|
const fn = window.API_METHODS[msg.method];
|
2018-01-01 17:02:49 +00:00
|
|
|
if (!fn) return;
|
|
|
|
|
|
|
|
// wrap 'Error' object instance as {__ERROR__: message},
|
|
|
|
// which will be unwrapped by sendMessage,
|
|
|
|
// and prevent exceptions on sending to a closed tab
|
|
|
|
const respond = data =>
|
|
|
|
tryCatch(sendResponse,
|
|
|
|
data instanceof Error ? {__ERROR__: data.message} : data);
|
|
|
|
|
|
|
|
const result = fn(msg, sender, respond);
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
result
|
|
|
|
.catch(e => ({__ERROR__: e instanceof Error ? e.message : e}))
|
|
|
|
.then(respond);
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
} else if (result === KEEP_CHANNEL_OPEN) {
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
} else if (result !== undefined) {
|
|
|
|
respond(result);
|
2017-09-01 10:21:45 +00:00
|
|
|
}
|
|
|
|
}
|