2017-04-19 16:22:13 +00:00
|
|
|
/* global getDatabase, 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
|
|
|
|
getDatabase(() => {}, (...args) => {
|
|
|
|
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
|
|
|
// *************************************************************************
|
|
|
|
// Open FAQs page once after installation to guide new users.
|
|
|
|
// Do not display it in development mode.
|
|
|
|
if (chrome.runtime.getManifest().update_url) {
|
|
|
|
const openHomepageOnInstall = ({reason}) => {
|
|
|
|
chrome.runtime.onInstalled.removeListener(openHomepageOnInstall);
|
|
|
|
if (reason == 'install') {
|
|
|
|
const version = chrome.runtime.getManifest().version;
|
|
|
|
setTimeout(openURL, 100, {
|
|
|
|
url: `http://add0n.com/stylus.html?version=${version}&type=install`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// bind for 60 seconds max and auto-unbind if it's a normal run
|
|
|
|
chrome.runtime.onInstalled.addListener(openHomepageOnInstall);
|
|
|
|
setTimeout(openHomepageOnInstall, 60e3, {reason: 'unbindme'});
|
|
|
|
}
|
2017-04-17 15:54:39 +00:00
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
// *************************************************************************
|
|
|
|
// reset L10N cache on UI language change
|
|
|
|
{
|
2017-04-17 15:54:39 +00:00
|
|
|
const {browserUIlanguage} = tryJSONparse(localStorage.L10N) || {};
|
|
|
|
const UIlang = chrome.i18n.getUILanguage();
|
|
|
|
if (browserUIlanguage != UIlang) {
|
|
|
|
localStorage.L10N = JSON.stringify({
|
|
|
|
browserUIlanguage: UIlang,
|
|
|
|
});
|
|
|
|
}
|
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-20 14:00:43 +00:00
|
|
|
},
|
|
|
|
// detect browsers without Delete by looking at the end of UA string
|
|
|
|
/Vivaldi\/[\d.]+$/.test(navigator.userAgent) ||
|
|
|
|
// Chrome and co.
|
|
|
|
/Safari\/[\d.]+$/.test(navigator.userAgent) &&
|
|
|
|
// skip forks with Flash as those are likely to have the menu e.g. CentBrowser
|
|
|
|
!Array.from(navigator.plugins).some(p => p.name == 'Shockwave Flash')
|
|
|
|
&& {
|
|
|
|
'editDeleteText': {
|
2017-03-26 02:30:59 +00:00
|
|
|
title: 'editDeleteText',
|
|
|
|
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-20 14:00:43 +00:00
|
|
|
for (const id of Object.keys(contextMenus)) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const item = Object.assign({id}, contextMenus[id]);
|
|
|
|
const prefValue = prefs.readOnlyValues[id];
|
|
|
|
const isBoolean = typeof prefValue == 'boolean';
|
|
|
|
item.title = chrome.i18n.getMessage(item.title);
|
|
|
|
if (isBoolean) {
|
|
|
|
item.type = 'checkbox';
|
|
|
|
item.checked = prefValue;
|
|
|
|
}
|
|
|
|
if (!item.contexts) {
|
|
|
|
item.contexts = ['browser_action'];
|
|
|
|
}
|
|
|
|
delete item.click;
|
|
|
|
chrome.contextMenus.create(item, ignoreChromeError);
|
2017-04-20 14:00:43 +00:00
|
|
|
}
|
2017-02-23 07:37:25 +00:00
|
|
|
|
2017-04-20 18:27:10 +00:00
|
|
|
prefs.subscribe((id, checked) => {
|
|
|
|
chrome.contextMenus.update(id, {checked}, ignoreChromeError);
|
|
|
|
}, Object.keys(contextMenus));
|
2017-03-15 12:41:39 +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}) {
|
|
|
|
getStyles({matchUrl: url, enabled: true, asHash: true}, styles => {
|
|
|
|
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) {
|
|
|
|
// check for not-yet-existing tabs e.g. omnibox instant search
|
|
|
|
chrome.tabs.get(tab.id, () => {
|
|
|
|
if (!chrome.runtime.lastError) {
|
|
|
|
stylesReceived(styles);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 15:56:37 +00:00
|
|
|
getTabRealURL(tab).then(url =>
|
|
|
|
getStyles({matchUrl: url, enabled: true, asHash: true},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}, () => {
|
|
|
|
if (!chrome.runtime.lastError) {
|
|
|
|
// Vivaldi bug workaround: setBadgeText must follow setBadgeBackgroundColor
|
|
|
|
chrome.browserAction.setBadgeBackgroundColor({color});
|
|
|
|
chrome.browserAction.setBadgeText({text, tabId: tab.id});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-20 14:00:43 +00:00
|
|
|
function onRuntimeMessage(request, sender, sendResponse) {
|
|
|
|
switch (request.method) {
|
|
|
|
|
|
|
|
case 'getStyles':
|
|
|
|
getStyles(request, sendResponse);
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
case 'saveStyle':
|
|
|
|
saveStyle(request).then(sendResponse);
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
case 'healthCheck':
|
|
|
|
getDatabase(
|
|
|
|
() => sendResponse(true),
|
|
|
|
() => sendResponse(false));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|