2017-04-25 21:48:27 +00:00
|
|
|
/* global dbExec, getStyles, saveStyle */
|
2017-09-03 08:12:18 +00:00
|
|
|
/* global handleCssTransitionBug */
|
2017-09-18 03:34:12 +00:00
|
|
|
/* global usercssHelper */
|
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
|
|
|
{
|
|
|
|
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-04-20 14:00:43 +00:00
|
|
|
|
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) =>
|
|
|
|
contextMenus[info.menuItemId].click(info, tab));
|
|
|
|
|
|
|
|
if ('commands' in chrome) {
|
|
|
|
// 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-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-04-20 14:00:43 +00:00
|
|
|
setTimeout(openURL, 100, {
|
2017-07-09 19:29:54 +00:00
|
|
|
url: 'http://add0n.com/stylus.html'
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// 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) => {
|
|
|
|
chrome.tabs.sendMessage(tab.id, {method: 'editDeleteText'});
|
|
|
|
},
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
|
|
|
});
|
2017-03-18 22:35:27 +00:00
|
|
|
|
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 PING = {method: 'ping'};
|
|
|
|
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}) => {
|
|
|
|
cs.matches.some(match => {
|
2017-07-16 18:02:00 +00:00
|
|
|
if ((match === ALL_URLS || url.match(match))
|
|
|
|
&& (!url.startsWith('chrome') || url === NTP)) {
|
2017-06-10 18:33:37 +00:00
|
|
|
chrome.tabs.sendMessage(id, PING, pong => {
|
|
|
|
if (!pong) {
|
|
|
|
injectCS(cs, id);
|
|
|
|
}
|
|
|
|
ignoreChromeError();
|
|
|
|
});
|
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
|
|
|
|
if (!FIREFOX || tab.width) {
|
|
|
|
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-04-20 14:00:43 +00:00
|
|
|
chrome.tabs.sendMessage(tabId, {
|
|
|
|
method,
|
|
|
|
// ping own page so it retrieves the styles directly
|
|
|
|
styles: url.startsWith(URLS.ownOrigin) ? 'DIY' : styles,
|
|
|
|
}, {
|
|
|
|
frameId
|
2017-08-28 11:37:49 +00:00
|
|
|
}, ignoreChromeError);
|
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) {
|
|
|
|
// 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-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-04-20 14:00:43 +00:00
|
|
|
function onRuntimeMessage(request, sender, sendResponse) {
|
2017-08-26 04:51:28 +00:00
|
|
|
// prevent browser exception bug on sending a response to a closed tab
|
2017-08-30 15:05:22 +00:00
|
|
|
sendResponse = (send => data => tryCatch(send, data))(sendResponse);
|
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-12 10:28:16 +00:00
|
|
|
case 'injectContent':
|
2017-09-12 10:34:48 +00:00
|
|
|
injectContent(sender.tab.id, request).then(sendResponse);
|
2017-09-01 10:21:45 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 10:34:48 +00:00
|
|
|
function injectContent(tabId, {files}) {
|
|
|
|
return Promise.all(files.map(inject))
|
2017-09-01 10:21:45 +00:00
|
|
|
.then(() => ({status: 'success'}))
|
|
|
|
.catch(err => ({status: 'error', error: err.message}));
|
|
|
|
|
2017-09-12 10:34:48 +00:00
|
|
|
function inject(file) {
|
2017-09-01 10:21:45 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-12 10:34:48 +00:00
|
|
|
const method = file.endsWith('.js') ? 'executeScript' : 'insertCSS';
|
|
|
|
chrome.tabs[method](tabId, {
|
|
|
|
file,
|
2017-09-01 10:21:45 +00:00
|
|
|
runAt: 'document_start'
|
|
|
|
}, () => {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
reject(chrome.runtime.lastError);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
}
|