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-03-27 09:11:29 +00:00
|
|
|
chrome.webNavigation.onBeforeNavigate.addListener(data => {
|
|
|
|
webNavigationListener(null, data);
|
|
|
|
});
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.webNavigation.onCommitted.addListener(data => {
|
|
|
|
webNavigationListener('styleApply', data);
|
|
|
|
});
|
2017-03-27 09:11:29 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.webNavigation.onHistoryStateUpdated.addListener(data => {
|
|
|
|
webNavigationListener('styleReplaceAll', data);
|
|
|
|
});
|
2017-03-27 09:11:29 +00:00
|
|
|
|
|
|
|
chrome.webNavigation.onReferenceFragmentUpdated.addListener(data => {
|
|
|
|
webNavigationListener('styleReplaceAll', data);
|
2017-03-26 02:30:59 +00:00
|
|
|
});
|
|
|
|
|
2015-02-23 22:48:27 +00:00
|
|
|
function webNavigationListener(method, data) {
|
2017-03-26 02:30:59 +00:00
|
|
|
getStyles({matchUrl: data.url, enabled: true, asHash: true}, styles => {
|
2017-03-27 09:11:29 +00:00
|
|
|
// we can't inject chrome:// and chrome-extension:// pages
|
|
|
|
// so we'll only inform our page of the change
|
|
|
|
// and it'll retrieve the styles directly
|
2017-04-09 06:43:51 +00:00
|
|
|
if (method && !data.url.startsWith('chrome:') && data.tabId >= 0) {
|
|
|
|
const isOwnPage = data.url.startsWith(URLS.ownOrigin);
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.tabs.sendMessage(
|
|
|
|
data.tabId,
|
2017-03-27 09:11:29 +00:00
|
|
|
{method, styles: isOwnPage ? 'DIY' : styles},
|
2017-03-26 02:30:59 +00:00
|
|
|
{frameId: data.frameId});
|
|
|
|
}
|
|
|
|
// main page frame id is 0
|
|
|
|
if (data.frameId == 0) {
|
|
|
|
updateIcon({id: data.tabId, url: data.url}, styles);
|
|
|
|
}
|
|
|
|
});
|
2015-02-23 22:48:27 +00:00
|
|
|
}
|
2015-01-30 16:36:46 +00:00
|
|
|
|
2017-04-17 15:54:39 +00:00
|
|
|
// reset i18n cache on language change
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
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
|
|
|
// messaging
|
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
chrome.runtime.onMessage.addListener(onRuntimeMessage);
|
2017-03-18 22:35:27 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
function onRuntimeMessage(request, sender, sendResponse) {
|
2017-03-26 02:30:59 +00:00
|
|
|
switch (request.method) {
|
|
|
|
|
|
|
|
case 'getStyles':
|
2017-03-27 19:45:23 +00:00
|
|
|
getStyles(request, styles => {
|
|
|
|
sendResponse(styles);
|
|
|
|
// check if this is a main content frame style enumeration
|
|
|
|
if (request.matchUrl && !request.id
|
|
|
|
&& sender && sender.tab && sender.frameId == 0
|
|
|
|
&& sender.tab.url == request.matchUrl) {
|
|
|
|
updateIcon(sender.tab, styles);
|
|
|
|
}
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
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 'prefChanged':
|
2017-03-31 08:46:18 +00:00
|
|
|
for (var prefName in request.prefs) { // eslint-disable-line no-var
|
|
|
|
if (prefName in contextMenus) { // eslint-disable-line no-use-before-define
|
|
|
|
chrome.contextMenus.update(prefName, {
|
|
|
|
checked: request.prefs[prefName],
|
|
|
|
}, ignoreChromeError);
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-03-18 22:35:27 +00:00
|
|
|
}
|
2015-01-29 18:41:45 +00:00
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
// commands (global hotkeys)
|
2016-03-08 05:22:28 +00:00
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
const browserCommands = {
|
2017-03-26 02:30:59 +00:00
|
|
|
openManage() {
|
|
|
|
openURL({url: '/manage.html'});
|
|
|
|
},
|
|
|
|
styleDisableAll(state) {
|
|
|
|
prefs.set('disableAll',
|
|
|
|
typeof state == 'boolean' ? state : !prefs.get('disableAll'));
|
|
|
|
},
|
2017-03-18 22:35:27 +00:00
|
|
|
};
|
2016-03-08 05:22:28 +00:00
|
|
|
// Not available in Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1240350
|
2017-03-18 22:35:27 +00:00
|
|
|
if ('commands' in chrome) {
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.commands.onCommand.addListener(command => browserCommands[command]());
|
2016-03-08 05:22:28 +00:00
|
|
|
}
|
2015-03-24 14:07:59 +00:00
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
// context menus
|
2017-04-11 10:51:40 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var 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-03-18 22:35:27 +00:00
|
|
|
};
|
|
|
|
|
2017-03-21 22:29:07 +00:00
|
|
|
// detect browsers without Delete by looking at the end of UA string
|
|
|
|
// Google Chrome: Safari/#
|
|
|
|
// but skip CentBrowser: Safari/# plus Shockwave Flash in plugins
|
|
|
|
// Vivaldi: Vivaldi/#
|
|
|
|
if (/Vivaldi\/[\d.]+$/.test(navigator.userAgent)
|
2017-03-26 02:30:59 +00:00
|
|
|
|| /Safari\/[\d.]+$/.test(navigator.userAgent)
|
2017-04-11 10:51:40 +00:00
|
|
|
&& !Array.from(navigator.plugins).some(p => p.name == 'Shockwave Flash')) {
|
2017-03-26 02:30:59 +00:00
|
|
|
contextMenus.editDeleteText = {
|
|
|
|
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-03-21 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) =>
|
2017-03-26 02:30:59 +00:00
|
|
|
contextMenus[info.menuItemId].click(info, tab));
|
2017-03-18 22:35:27 +00:00
|
|
|
|
|
|
|
Object.keys(contextMenus).forEach(id => {
|
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);
|
2015-03-25 17:59:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
// Get the DB so that any first run actions will be performed immediately
|
|
|
|
// when the background page loads.
|
2017-04-19 16:22:13 +00:00
|
|
|
getDatabase(() => {}, (...args) => {
|
|
|
|
args.forEach(arg => 'message' in arg && console.error(arg.message));
|
|
|
|
});
|
|
|
|
|
2015-01-30 18:35:37 +00:00
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
// When an edit page gets attached or detached, remember its state
|
|
|
|
// so we can do the same to the next one to open.
|
2017-04-09 06:43:51 +00:00
|
|
|
const editFullUrl = URLS.ownOrigin + 'edit.html';
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.tabs.onAttached.addListener((tabId, data) => {
|
|
|
|
chrome.tabs.get(tabId, tabData => {
|
|
|
|
if (tabData.url.startsWith(editFullUrl)) {
|
|
|
|
chrome.windows.get(tabData.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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-01-30 18:35:37 +00:00
|
|
|
});
|
2015-03-13 22:45:38 +00:00
|
|
|
|
2017-04-11 10:51:40 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var codeMirrorThemes;
|
|
|
|
getCodeMirrorThemes().then(themes => {
|
|
|
|
codeMirrorThemes = themes;
|
|
|
|
});
|
2017-02-23 07:37:25 +00:00
|
|
|
|
2017-03-11 09:26:24 +00:00
|
|
|
// do not use prefs.get('version', null) as it might not yet be available
|
|
|
|
chrome.storage.local.get('version', prefs => {
|
2017-03-26 02:30:59 +00:00
|
|
|
// Open FAQs page once after installation to guide new users,
|
|
|
|
// https://github.com/schomery/stylish-chrome/issues/22#issuecomment-279936160
|
|
|
|
if (!prefs.version) {
|
|
|
|
// do not display the FAQs page in development mode
|
|
|
|
if ('update_url' in chrome.runtime.getManifest()) {
|
|
|
|
const version = chrome.runtime.getManifest().version;
|
|
|
|
chrome.storage.local.set({version}, () => {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
chrome.tabs.create({
|
|
|
|
url: `http://add0n.com/stylus.html?version=${version}&type=install`
|
|
|
|
});
|
|
|
|
}, 3000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 09:26:24 +00:00
|
|
|
});
|
2017-03-15 12:41:39 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-03-16 10:17:42 +00:00
|
|
|
injectContentScripts();
|
2017-03-15 18:18:17 +00:00
|
|
|
|
2017-03-16 10:17:42 +00:00
|
|
|
function injectContentScripts() {
|
2017-04-11 10:51:40 +00:00
|
|
|
// expand * as .*?
|
|
|
|
const wildcardAsRegExp = (s, flags) =>
|
|
|
|
new RegExp(s.replace(/[{}()[\]/\\.+?^$:=!|]/g, '\\$&').replace(/\*/g, '.*?'), flags);
|
2017-04-09 06:43:51 +00:00
|
|
|
const contentScripts = chrome.runtime.getManifest().content_scripts;
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const cs of contentScripts) {
|
|
|
|
cs.matches = cs.matches.map(m => (
|
|
|
|
m == '<all_urls>' ? m : wildcardAsRegExp(m)
|
|
|
|
));
|
|
|
|
}
|
2017-04-12 09:54:55 +00:00
|
|
|
chrome.tabs.query({}, tabs => {
|
2017-03-26 02:30:59 +00:00
|
|
|
for (const tab of tabs) {
|
|
|
|
for (const cs of contentScripts) {
|
|
|
|
for (const m of cs.matches) {
|
2017-04-12 09:54:55 +00:00
|
|
|
if ((m == '<all_urls>' || tab.url.match(m))
|
|
|
|
&& (!tab.url.startsWith('chrome') || tab.url == 'chrome://newtab/')) {
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.tabs.sendMessage(tab.id, {method: 'ping'}, pong => {
|
|
|
|
if (!pong) {
|
|
|
|
chrome.tabs.executeScript(tab.id, {
|
|
|
|
file: cs.js[0],
|
|
|
|
runAt: cs.run_at,
|
|
|
|
allFrames: cs.all_frames,
|
2017-04-12 09:54:55 +00:00
|
|
|
matchAboutBlank: cs.match_about_blank,
|
2017-03-26 02:30:59 +00:00
|
|
|
}, ignoreChromeError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// inject the content script just once
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-03-15 12:41:39 +00:00
|
|
|
}
|
2017-04-11 10:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
function refreshAllTabs() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
// list all tabs including chrome-extension:// which can be ours
|
|
|
|
chrome.tabs.query({}, tabs => {
|
|
|
|
const lastTab = tabs[tabs.length - 1];
|
|
|
|
for (const tab of tabs) {
|
|
|
|
getStyles({matchUrl: tab.url, enabled: true, asHash: true}, styles => {
|
|
|
|
const message = {method: 'styleReplaceAll', styles};
|
|
|
|
chrome.tabs.sendMessage(tab.id, message);
|
|
|
|
updateIcon(tab, styles);
|
|
|
|
if (tab == lastTab) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getCodeMirrorThemes() {
|
|
|
|
if (!chrome.runtime.getPackageDirectoryEntry) {
|
|
|
|
return Promise.resolve([
|
2017-04-13 18:30:24 +00:00
|
|
|
chrome.i18n.getMessage('defaultTheme'),
|
2017-04-11 10:51:40 +00:00
|
|
|
'3024-day',
|
|
|
|
'3024-night',
|
|
|
|
'abcdef',
|
|
|
|
'ambiance',
|
|
|
|
'ambiance-mobile',
|
|
|
|
'base16-dark',
|
|
|
|
'base16-light',
|
|
|
|
'bespin',
|
|
|
|
'blackboard',
|
|
|
|
'cobalt',
|
|
|
|
'colorforth',
|
|
|
|
'dracula',
|
|
|
|
'duotone-dark',
|
|
|
|
'duotone-light',
|
|
|
|
'eclipse',
|
|
|
|
'elegant',
|
|
|
|
'erlang-dark',
|
|
|
|
'hopscotch',
|
|
|
|
'icecoder',
|
|
|
|
'isotope',
|
|
|
|
'lesser-dark',
|
|
|
|
'liquibyte',
|
|
|
|
'material',
|
|
|
|
'mbo',
|
|
|
|
'mdn-like',
|
|
|
|
'midnight',
|
|
|
|
'monokai',
|
|
|
|
'neat',
|
|
|
|
'neo',
|
|
|
|
'night',
|
|
|
|
'panda-syntax',
|
|
|
|
'paraiso-dark',
|
|
|
|
'paraiso-light',
|
|
|
|
'pastel-on-dark',
|
|
|
|
'railscasts',
|
|
|
|
'rubyblue',
|
|
|
|
'seti',
|
|
|
|
'solarized',
|
|
|
|
'the-matrix',
|
|
|
|
'tomorrow-night-bright',
|
|
|
|
'tomorrow-night-eighties',
|
|
|
|
'ttcn',
|
|
|
|
'twilight',
|
|
|
|
'vibrant-ink',
|
|
|
|
'xq-dark',
|
|
|
|
'xq-light',
|
|
|
|
'yeti',
|
|
|
|
'zenburn',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return new Promise(resolve => {
|
|
|
|
chrome.runtime.getPackageDirectoryEntry(rootDir => {
|
|
|
|
rootDir.getDirectory('codemirror/theme', {create: false}, themeDir => {
|
|
|
|
themeDir.createReader().readEntries(entries => {
|
|
|
|
resolve([
|
|
|
|
chrome.i18n.getMessage('defaultTheme')
|
|
|
|
].concat(
|
|
|
|
entries.filter(entry => entry.isFile)
|
|
|
|
.sort((a, b) => (a.name < b.name ? -1 : 1))
|
|
|
|
.map(entry => entry.name.replace(/\.css$/, ''))
|
|
|
|
));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|