2018-10-10 17:26:24 +00:00
|
|
|
/* global detectSloppyRegexps download prefs openURL FIREFOX CHROME VIVALDI
|
|
|
|
openEditor debounce URLS ignoreChromeError queryTabs getTab
|
2018-10-11 07:42:23 +00:00
|
|
|
usercss styleManager db msg navigatorUtil iconUtil
|
2018-02-16 15:05:25 +00:00
|
|
|
*/
|
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-10-04 04:46:19 +00:00
|
|
|
getSectionsByUrl: styleManager.getSectionsByUrl,
|
|
|
|
getSectionsById: styleManager.getSectionsById,
|
2018-10-04 17:03:40 +00:00
|
|
|
getStylesInfo: styleManager.getStylesInfo,
|
2018-10-05 10:47:52 +00:00
|
|
|
toggleStyle: styleManager.toggleStyle,
|
|
|
|
deleteStyle: styleManager.deleteStyle,
|
2018-10-06 05:45:37 +00:00
|
|
|
getStylesInfoByUrl: styleManager.getStylesInfoByUrl,
|
2018-10-07 14:59:31 +00:00
|
|
|
installStyle: styleManager.installStyle,
|
2018-10-10 08:14:51 +00:00
|
|
|
editSave: styleManager.editSave,
|
2018-01-01 17:02:49 +00:00
|
|
|
|
2018-10-10 19:21:38 +00:00
|
|
|
getTabUrlPrefix() {
|
|
|
|
return this.sender.tab.url.match(/^([\w-]+:\/\/[^/#]+)/)[1];
|
2018-10-10 17:39:10 +00:00
|
|
|
},
|
|
|
|
|
2018-10-02 12:22:18 +00:00
|
|
|
getStyleFromDB: id =>
|
2018-10-05 10:47:52 +00:00
|
|
|
db.exec('get', id).then(event => event.target.result),
|
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}) {
|
|
|
|
return usercss.invokeWorker({action: 'parse', code});
|
|
|
|
},
|
2018-07-05 12:42:55 +00:00
|
|
|
getPrefs: prefs.getAll,
|
2018-10-07 15:28:41 +00:00
|
|
|
|
|
|
|
// FIXME: who uses this?
|
|
|
|
healthCheck: () => db.exec().then(() => true),
|
2018-01-01 17:02:49 +00:00
|
|
|
|
|
|
|
detectSloppyRegexps,
|
|
|
|
openEditor,
|
2018-10-11 07:42:23 +00:00
|
|
|
|
|
|
|
updateIconBadge(count) {
|
|
|
|
return updateIconBadge(this.sender.tab.id, count);
|
|
|
|
},
|
2018-01-01 17:02:49 +00:00
|
|
|
|
2018-03-13 15:35:28 +00:00
|
|
|
// exposed for stuff that requires followup sendMessage() like popup::openSettings
|
|
|
|
// that would fail otherwise if another extension forced the tab to open
|
|
|
|
// in the foreground thus auto-closing the popup (in Chrome)
|
|
|
|
openURL,
|
|
|
|
|
2018-10-07 13:20:39 +00:00
|
|
|
// FIXME: who use this?
|
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));
|
|
|
|
}
|
|
|
|
});
|
2018-10-07 13:20:39 +00:00
|
|
|
return true;
|
2018-01-01 17:02:49 +00:00
|
|
|
},
|
2018-04-12 18:02:34 +00:00
|
|
|
|
|
|
|
optionsCustomizeHotkeys() {
|
|
|
|
return browser.runtime.openOptionsPage()
|
|
|
|
.then(() => new Promise(resolve => setTimeout(resolve, 100)))
|
2018-10-06 05:02:45 +00:00
|
|
|
.then(() => msg.broadcastExtension({method: 'optionsCustomizeHotkeys'}));
|
2018-04-12 18:02:34 +00:00
|
|
|
},
|
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
|
2018-10-06 05:02:45 +00:00
|
|
|
msg.on(onRuntimeMessage);
|
2017-03-27 09:11:29 +00:00
|
|
|
|
2018-10-10 17:26:24 +00:00
|
|
|
// if (FIREFOX) {
|
2018-08-09 17:33:04 +00:00
|
|
|
// see notes in apply.js for getStylesFallback
|
2018-10-10 17:26:24 +00:00
|
|
|
// const MSG_GET_STYLES = 'getStyles:';
|
|
|
|
// const MSG_GET_STYLES_LEN = MSG_GET_STYLES.length;
|
|
|
|
// chrome.runtime.onConnect.addListener(port => {
|
|
|
|
// if (!port.name.startsWith(MSG_GET_STYLES)) return;
|
|
|
|
// const tabId = port.sender.tab.id;
|
|
|
|
// const frameId = port.sender.frameId;
|
|
|
|
// const options = tryJSONparse(port.name.slice(MSG_GET_STYLES_LEN));
|
|
|
|
// port.disconnect();
|
2018-10-07 13:20:39 +00:00
|
|
|
// FIXME: getStylesFallback?
|
2018-10-10 17:26:24 +00:00
|
|
|
// getStyles(options).then(styles => {
|
|
|
|
// if (!styles.length) return;
|
|
|
|
// chrome.tabs.executeScript(tabId, {
|
|
|
|
// code: `
|
|
|
|
// applyOnMessage({
|
|
|
|
// method: 'styleApply',
|
|
|
|
// styles: ${JSON.stringify(styles)},
|
|
|
|
// })
|
|
|
|
// `,
|
|
|
|
// runAt: 'document_start',
|
|
|
|
// frameId,
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// }
|
2018-08-09 17:33:04 +00:00
|
|
|
|
2018-10-07 13:20:39 +00:00
|
|
|
navigatorUtil.onUrlChange(({tabId, frameId}, type) => {
|
|
|
|
if (type === 'committed') {
|
|
|
|
// styles would be updated when content script is injected.
|
|
|
|
return;
|
2017-11-24 16:33:50 +00:00
|
|
|
}
|
2018-10-10 08:14:51 +00:00
|
|
|
msg.sendTab(tabId, {method: 'urlChanged'}, {frameId})
|
2018-10-10 19:21:38 +00:00
|
|
|
.catch(msg.broadcastError);
|
2018-10-07 13:20:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (FIREFOX) {
|
|
|
|
// FF applies page CSP even to content scripts, https://bugzil.la/1267027
|
|
|
|
navigatorUtil.onCommitted(webNavUsercssInstallerFF, {
|
|
|
|
url: [
|
|
|
|
{hostSuffix: '.githubusercontent.com', urlSuffix: '.user.css'},
|
|
|
|
{hostSuffix: '.githubusercontent.com', urlSuffix: '.user.styl'},
|
|
|
|
]
|
|
|
|
});
|
|
|
|
// FF misses some about:blank iframes so we inject our content script explicitly
|
|
|
|
navigatorUtil.onDOMContentLoaded(webNavIframeHelperFF, {
|
|
|
|
url: [
|
|
|
|
{urlEquals: 'about:blank'},
|
|
|
|
]
|
|
|
|
});
|
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
|
|
|
|
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));
|
|
|
|
|
2018-10-04 11:20:36 +00:00
|
|
|
prefs.subscribe([
|
|
|
|
'disableAll',
|
|
|
|
'badgeDisabled',
|
|
|
|
'badgeNormal',
|
2018-10-11 07:42:23 +00:00
|
|
|
], () => debounce(refreshIconBadgeColor));
|
|
|
|
|
|
|
|
prefs.subscribe([
|
2018-10-11 10:13:24 +00:00
|
|
|
'show-badge'
|
|
|
|
], () => debounce(refreshIconBadgeText));
|
|
|
|
|
|
|
|
prefs.subscribe([
|
|
|
|
'disableAll',
|
2018-10-04 11:20:36 +00:00
|
|
|
'iconset',
|
2018-10-11 07:42:23 +00:00
|
|
|
], () => debounce(refreshAllIcons));
|
2018-10-04 11:20:36 +00:00
|
|
|
|
2018-10-11 10:13:24 +00:00
|
|
|
prefs.initializing.then(() => {
|
|
|
|
refreshIconBadgeColor();
|
|
|
|
refreshAllIconsBadgeText();
|
|
|
|
refreshAllIcons();
|
|
|
|
});
|
|
|
|
|
|
|
|
navigatorUtil.onUrlChange(({tabId, frameId}, type) => {
|
|
|
|
if (type === 'committed' && !frameId) {
|
|
|
|
// it seems that the tab icon would be reset when pressing F5. We
|
|
|
|
// invalidate the cache here so it would be refreshed.
|
|
|
|
tabIcons.delete(tabId);
|
|
|
|
}
|
|
|
|
});
|
2018-10-11 09:32:27 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
2018-08-06 15:20:57 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(({reason}) => {
|
|
|
|
if (reason !== 'update') return;
|
|
|
|
// translations may change
|
|
|
|
localStorage.L10N = JSON.stringify({
|
|
|
|
browserUIlanguage: chrome.i18n.getUILanguage(),
|
|
|
|
});
|
|
|
|
// themes may change
|
|
|
|
delete localStorage.codeMirrorThemes;
|
|
|
|
});
|
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) => {
|
2018-10-06 05:02:45 +00:00
|
|
|
msg.sendTab(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
|
|
|
item.title = chrome.i18n.getMessage(item.title);
|
2018-10-04 09:09:26 +00:00
|
|
|
if (!item.type && typeof prefs.defaults[id] === 'boolean') {
|
2017-04-29 16:54:16 +00:00
|
|
|
item.type = 'checkbox';
|
2018-10-04 09:09:26 +00:00
|
|
|
item.checked = prefs.get(id);
|
2017-04-29 16:54:16 +00:00
|
|
|
}
|
|
|
|
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);
|
2018-10-04 09:09:26 +00:00
|
|
|
prefs.subscribe(keys.filter(id => typeof prefs.defaults[id] === 'boolean'), toggleCheckmark);
|
2017-11-26 11:20:44 +00:00
|
|
|
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
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
if (!FIREFOX) {
|
|
|
|
reinjectContentScripts();
|
|
|
|
}
|
2017-09-03 18:25:19 +00:00
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
// FIXME: implement exposeIframes in apply.js
|
2017-09-03 18:25:19 +00:00
|
|
|
|
2018-10-11 07:42:23 +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-03 07:37:54 +00:00
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
function reinjectContentScripts() {
|
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) => {
|
2018-04-25 07:37:07 +00:00
|
|
|
ignoreChromeError();
|
2018-10-08 06:39:08 +00:00
|
|
|
for (const file of cs.js) {
|
|
|
|
chrome.tabs.executeScript(tabId, {
|
|
|
|
file,
|
|
|
|
runAt: cs.run_at,
|
|
|
|
allFrames: cs.all_frames,
|
|
|
|
matchAboutBlank: cs.match_about_blank,
|
|
|
|
}, ignoreChromeError);
|
|
|
|
}
|
2017-04-20 14:00:43 +00:00
|
|
|
};
|
2017-04-11 10:51:40 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
const pingCS = (cs, {id, url}) => {
|
|
|
|
cs.matches.some(match => {
|
2017-11-24 15:26:20 +00:00
|
|
|
if ((match === ALL_URLS || url.match(match)) &&
|
|
|
|
(!url.startsWith('chrome') || url === NTP)) {
|
2018-10-08 07:30:39 +00:00
|
|
|
// TODO: add a `msg.pingTab` API?
|
2018-10-06 05:02:45 +00:00
|
|
|
msg.sendTab(id, {method: 'ping'})
|
2018-10-08 07:30:39 +00:00
|
|
|
.catch(() => false)
|
2018-10-06 05:02:45 +00:00
|
|
|
.then(pong => !pong && injectCS(cs, id));
|
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));
|
|
|
|
}
|
|
|
|
}));
|
2018-10-04 09:04:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 16:33:50 +00:00
|
|
|
function webNavUsercssInstallerFF(data) {
|
|
|
|
const {tabId} = data;
|
|
|
|
Promise.all([
|
2018-10-08 07:30:39 +00:00
|
|
|
msg.sendTab(tabId, {method: 'ping'})
|
|
|
|
.catch(() => false),
|
2017-11-24 16:33:50 +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(([pong, tab]) => {
|
|
|
|
if (pong !== true && tab.url !== 'about:blank') {
|
2018-10-07 14:59:31 +00:00
|
|
|
window.API_METHODS.openUsercssInstallPage({direct: true}, {tab});
|
2017-11-24 16:33:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-08 16:19:23 +00:00
|
|
|
function webNavIframeHelperFF({tabId, frameId}) {
|
|
|
|
if (!frameId) return;
|
2018-10-06 05:02:45 +00:00
|
|
|
msg.sendTab(tabId, {method: 'ping'}, {frameId})
|
2018-10-08 07:30:39 +00:00
|
|
|
.catch(() => false)
|
2018-10-06 05:02:45 +00:00
|
|
|
.then(pong => {
|
|
|
|
if (pong) return;
|
2018-10-10 15:05:20 +00:00
|
|
|
// 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-10-06 05:02:45 +00:00
|
|
|
});
|
2018-03-08 16:19:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
function updateIconBadge(tabId, count) {
|
|
|
|
let tabIcon = tabIcons.get(tabId);
|
|
|
|
if (!tabIcon) tabIcons.set(tabId, (tabIcon = {}));
|
|
|
|
if (tabIcon.count === count) {
|
2017-04-11 10:51:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-11 10:13:24 +00:00
|
|
|
const oldCount = tabIcon.count;
|
2018-10-11 07:42:23 +00:00
|
|
|
tabIcon.count = count;
|
2018-10-11 10:13:24 +00:00
|
|
|
refreshIconBadgeText(tabId, tabIcon);
|
|
|
|
if (Boolean(oldCount) !== Boolean(count)) {
|
|
|
|
refreshIcon(tabId, tabIcon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshIconBadgeText(tabId, icon) {
|
2018-10-11 07:42:23 +00:00
|
|
|
iconUtil.setBadgeText({
|
2018-10-11 10:13:24 +00:00
|
|
|
text: prefs.get('show-badge') && icon.count ? String(icon.count) : '',
|
2018-10-11 07:42:23 +00:00
|
|
|
tabId
|
|
|
|
});
|
|
|
|
}
|
2018-01-03 07:39:40 +00:00
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
function refreshIcon(tabId, icon) {
|
|
|
|
const disableAll = prefs.get('disableAll');
|
|
|
|
const iconset = prefs.get('iconset') === 1 ? 'light/' : '';
|
|
|
|
const postfix = disableAll ? 'x' : !icon.count ? 'w' : '';
|
|
|
|
const iconType = iconset + postfix;
|
2018-01-03 07:39:40 +00:00
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
if (icon.iconType === iconType) {
|
|
|
|
return;
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
2018-10-11 07:42:23 +00:00
|
|
|
icon.iconType = iconset + postfix;
|
|
|
|
const sizes = FIREFOX || CHROME >= 2883 && !VIVALDI ? [16, 32] : [19, 38];
|
|
|
|
iconUtil.setIcon({
|
|
|
|
path: sizes.reduce(
|
|
|
|
(obj, size) => {
|
|
|
|
obj[size] = `/images/icon/${iconset}${size}${postfix}.png`;
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
),
|
|
|
|
tabId
|
|
|
|
});
|
|
|
|
}
|
2018-01-03 07:39:40 +00:00
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
function refreshIconBadgeColor() {
|
|
|
|
const color = prefs.get(prefs.get('disableAll') ? 'badgeDisabled' : 'badgeNormal');
|
|
|
|
iconUtil.setBadgeBackgroundColor({
|
|
|
|
color
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAllIcons() {
|
|
|
|
for (const [tabId, icon] of tabIcons) {
|
|
|
|
refreshIcon(tabId, icon);
|
2018-01-03 07:39:40 +00:00
|
|
|
}
|
2018-10-11 10:13:24 +00:00
|
|
|
refreshIcon(null, {}); // default icon
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAllIconsBadgeText() {
|
|
|
|
for (const [tabId, icon] of tabIcons) {
|
|
|
|
refreshIconBadgeText(tabId, icon);
|
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 05:02:45 +00:00
|
|
|
function onRuntimeMessage(msg, sender) {
|
2018-10-04 04:46:19 +00:00
|
|
|
if (msg.method !== 'invokeAPI') {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-06 05:02:45 +00:00
|
|
|
const fn = window.API_METHODS[msg.name];
|
|
|
|
if (!fn) {
|
|
|
|
throw new Error(`unknown API: ${msg.name}`);
|
2017-09-01 10:21:45 +00:00
|
|
|
}
|
2018-10-06 05:02:45 +00:00
|
|
|
const context = {msg, sender};
|
|
|
|
return fn.apply(context, msg.args);
|
2017-09-01 10:21:45 +00:00
|
|
|
}
|
2018-10-04 11:39:59 +00:00
|
|
|
|
2018-10-10 16:54:38 +00:00
|
|
|
function openEditor({id}) {
|
|
|
|
let url = '/edit.html';
|
|
|
|
if (id) {
|
|
|
|
url += `?id=${id}`;
|
|
|
|
}
|
|
|
|
if (chrome.windows && prefs.get('openEditInWindow')) {
|
|
|
|
chrome.windows.create(Object.assign({url}, prefs.get('windowPosition')));
|
|
|
|
} else {
|
|
|
|
openURL({url});
|
|
|
|
}
|
|
|
|
}
|