2017-04-25 21:48:27 +00:00
|
|
|
/* global dbExec, getStyles, saveStyle */
|
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
|
|
|
// *************************************************************************
|
|
|
|
// preload the DB and report errors
|
2017-04-25 21:48:27 +00:00
|
|
|
dbExec().catch((...args) => {
|
2017-04-20 14:00:43 +00:00
|
|
|
args.forEach(arg => 'message' in arg && console.error(arg.message));
|
2017-03-26 02:30:59 +00:00
|
|
|
});
|
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-04-20 14:00:43 +00:00
|
|
|
chrome.webNavigation.onBeforeNavigate.addListener(data =>
|
|
|
|
webNavigationListener(null, data));
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
chrome.webNavigation.onCommitted.addListener(data =>
|
|
|
|
webNavigationListener('styleApply', data));
|
|
|
|
|
|
|
|
chrome.webNavigation.onHistoryStateUpdated.addListener(data =>
|
|
|
|
webNavigationListener('styleReplaceAll', data));
|
|
|
|
|
|
|
|
chrome.webNavigation.onReferenceFragmentUpdated.addListener(data =>
|
|
|
|
webNavigationListener('styleReplaceAll', data));
|
|
|
|
|
|
|
|
chrome.tabs.onAttached.addListener((tabId, data) => {
|
|
|
|
// When an edit page gets attached or detached, remember its state
|
|
|
|
// so we can do the same to the next one to open.
|
|
|
|
chrome.tabs.get(tabId, tab => {
|
|
|
|
if (tab.url.startsWith(URLS.ownOrigin + 'edit.html')) {
|
|
|
|
chrome.windows.get(tab.windowId, {populate: true}, win => {
|
|
|
|
// If there's only one tab in this window, it's been dragged to new window
|
|
|
|
prefs.set('openEditInWindow', win.tabs.length == 1);
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
});
|
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-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.
|
|
|
|
if (reason == 'install' && manifest.update_url) {
|
2017-04-20 14:00:43 +00:00
|
|
|
setTimeout(openURL, 100, {
|
2017-04-24 13:26:59 +00:00
|
|
|
url: `http://add0n.com/stylus.html?version=${manifest.version}&type=install`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// reset L10N cache on UI language change or update
|
|
|
|
const {browserUIlanguage} = tryJSONparse(localStorage.L10N) || {};
|
|
|
|
const UIlang = chrome.i18n.getUILanguage();
|
|
|
|
if (reason == 'update' || browserUIlanguage != UIlang) {
|
|
|
|
localStorage.L10N = JSON.stringify({
|
|
|
|
browserUIlanguage: UIlang,
|
2017-04-20 14:00:43 +00:00
|
|
|
});
|
|
|
|
}
|
2017-05-03 15:37:47 +00:00
|
|
|
// TODO: remove in the future
|
|
|
|
// embed style digests
|
|
|
|
chrome.storage.local.get(null, data => {
|
|
|
|
const digestKeys = Object.keys(data).filter(key => key.startsWith('originalDigest'));
|
|
|
|
if (!digestKeys.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chrome.storage.local.remove(digestKeys);
|
|
|
|
getStyles().then(styles => {
|
|
|
|
for (const style of styles) {
|
|
|
|
const digest = data['originalDigest' + style.id];
|
|
|
|
if (!style.originalDigest && digest) {
|
|
|
|
style.originalDigest = digest;
|
|
|
|
dbExec('put', style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
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() {
|
|
|
|
openURL({url: '/manage.html'});
|
|
|
|
},
|
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-04-29 16:54:16 +00:00
|
|
|
}, prefs.get('editor.contextDelete') && {
|
|
|
|
'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);
|
|
|
|
if (!item.type && typeof prefValue == 'boolean') {
|
|
|
|
item.type = 'checkbox';
|
|
|
|
item.checked = prefValue;
|
|
|
|
}
|
|
|
|
if (!item.contexts) {
|
|
|
|
item.contexts = ['browser_action'];
|
|
|
|
}
|
|
|
|
delete item.click;
|
|
|
|
chrome.contextMenus.create(item, ignoreChromeError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
createContextMenus();
|
|
|
|
prefs.subscribe((id, checked) => {
|
|
|
|
if (id == 'editor.contextDelete') {
|
|
|
|
if (checked) {
|
|
|
|
createContextMenus([id]);
|
|
|
|
} else {
|
|
|
|
chrome.contextMenus.remove(id, ignoreChromeError);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chrome.contextMenus.update(id, {checked}, ignoreChromeError);
|
|
|
|
}
|
|
|
|
}, Object.keys(contextMenus).filter(key => typeof prefs.readOnlyValues[key] == 'boolean'));
|
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
|
|
|
|
{
|
|
|
|
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-04-20 14:00:43 +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 => {
|
|
|
|
if ((match == ALL_URLS || url.match(match))
|
|
|
|
&& (!url.startsWith('chrome') || url == NTP)) {
|
|
|
|
chrome.tabs.sendMessage(id, PING, pong => !pong && injectCS(cs, id));
|
|
|
|
return true;
|
2017-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
});
|
2017-04-20 14:00:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
chrome.tabs.query({}, tabs =>
|
|
|
|
tabs.forEach(tab =>
|
|
|
|
contentScripts.forEach(cs =>
|
|
|
|
pingCS(cs, tab))));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// *************************************************************************
|
|
|
|
|
|
|
|
function webNavigationListener(method, {url, tabId, frameId}) {
|
2017-04-25 21:48:27 +00:00
|
|
|
getStyles({matchUrl: url, enabled: true, asHash: true}).then(styles => {
|
2017-04-20 14:00:43 +00:00
|
|
|
if (method && !url.startsWith('chrome:') && tabId >= 0) {
|
|
|
|
chrome.tabs.sendMessage(tabId, {
|
|
|
|
method,
|
|
|
|
// ping own page so it retrieves the styles directly
|
|
|
|
styles: url.startsWith(URLS.ownOrigin) ? 'DIY' : styles,
|
|
|
|
}, {
|
|
|
|
frameId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// main page frame id is 0
|
|
|
|
if (frameId == 0) {
|
|
|
|
updateIcon({id: tabId, url}, styles);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
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');
|
|
|
|
const postfix = disableAll ? 'x' : numStyles == 0 ? 'w' : '';
|
|
|
|
const color = prefs.get(disableAll ? 'badgeDisabled' : 'badgeNormal');
|
|
|
|
const text = prefs.get('show-badge') && numStyles ? String(numStyles) : '';
|
|
|
|
chrome.browserAction.setIcon({
|
|
|
|
tabId: tab.id,
|
|
|
|
path: {
|
|
|
|
// Material Design 2016 new size is 16px
|
|
|
|
16: `images/icon/16${postfix}.png`,
|
|
|
|
32: `images/icon/32${postfix}.png`,
|
|
|
|
// Chromium forks or non-chromium browsers may still use the traditional 19px
|
|
|
|
19: `images/icon/19${postfix}.png`,
|
|
|
|
38: `images/icon/38${postfix}.png`,
|
|
|
|
// TODO: add Edge preferred sizes: 20, 25, 30, 40
|
|
|
|
},
|
|
|
|
}, () => {
|
2017-04-23 10:54:20 +00:00
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
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});
|
|
|
|
getTab(tab.id).then(() => {
|
|
|
|
chrome.browserAction.setBadgeText({text, tabId: tab.id});
|
|
|
|
});
|
2017-04-11 10:51:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
function onRuntimeMessage(request, sender, sendResponse) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-04-11 10:51:40 +00:00
|
|
|
}
|
|
|
|
}
|