2017-03-21 01:32:38 +00:00
|
|
|
/* globals openURL, wildcardAsRegExp, KEEP_CHANNEL_OPEN */
|
2017-03-15 12:41:39 +00:00
|
|
|
|
2015-01-30 16:36:46 +00:00
|
|
|
// This happens right away, sometimes so fast that the content script isn't even ready. That's
|
|
|
|
// why the content script also asks for this stuff.
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
chrome.webNavigation.onCommitted.addListener(webNavigationListener.bind(this, 'styleApply'));
|
|
|
|
chrome.webNavigation.onHistoryStateUpdated.addListener(webNavigationListener.bind(this, 'styleReplaceAll'));
|
2015-05-14 21:24:10 +00:00
|
|
|
chrome.webNavigation.onBeforeNavigate.addListener(webNavigationListener.bind(this, null));
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
|
2015-02-23 22:48:27 +00:00
|
|
|
function webNavigationListener(method, data) {
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
getStyles({matchUrl: data.url, enabled: true, asHash: true}, styles => {
|
|
|
|
// we can't inject chrome:// and chrome-extension:// pages except our own
|
|
|
|
// that request the styles on their own, so we'll only update the icon
|
|
|
|
if (method && !data.url.startsWith('chrome')) {
|
|
|
|
chrome.tabs.sendMessage(data.tabId, {method, styles}, {frameId: data.frameId});
|
2015-07-24 11:42:46 +00:00
|
|
|
}
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
// main page frame id is 0
|
2015-07-24 11:42:46 +00:00
|
|
|
if (data.frameId == 0) {
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
updateIcon({id: data.tabId, url: data.url}, styles);
|
2015-01-30 16:36:46 +00:00
|
|
|
}
|
|
|
|
});
|
2015-02-23 22:48:27 +00:00
|
|
|
}
|
2015-01-30 16:36:46 +00:00
|
|
|
|
2015-12-30 23:31:09 +00:00
|
|
|
// catch direct URL hash modifications not invoked via HTML5 history API
|
2017-03-18 22:35:27 +00:00
|
|
|
|
2015-12-30 23:31:09 +00:00
|
|
|
var tabUrlHasHash = {};
|
|
|
|
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
|
|
|
|
if (info.status == "loading" && info.url) {
|
|
|
|
if (info.url.indexOf('#') > 0) {
|
|
|
|
tabUrlHasHash[tabId] = true;
|
|
|
|
} else if (tabUrlHasHash[tabId]) {
|
|
|
|
delete tabUrlHasHash[tabId];
|
|
|
|
} else {
|
|
|
|
// do nothing since the tab neither had # before nor has # now
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
webNavigationListener("styleReplaceAll", {tabId: tabId, frameId: 0, url: info.url});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
chrome.tabs.onRemoved.addListener(function(tabId, info) {
|
|
|
|
delete tabUrlHasHash[tabId];
|
|
|
|
});
|
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
// messaging
|
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener(onBackgroundMessage);
|
|
|
|
|
|
|
|
function onBackgroundMessage(request, sender, sendResponse) {
|
2015-01-29 18:41:45 +00:00
|
|
|
switch (request.method) {
|
|
|
|
case "getStyles":
|
2015-05-14 21:24:10 +00:00
|
|
|
var styles = getStyles(request, sendResponse);
|
2015-05-21 09:34:44 +00:00
|
|
|
// check if this is a main content frame style enumeration
|
2015-10-05 15:06:05 +00:00
|
|
|
if (request.matchUrl && !request.id
|
|
|
|
&& sender && sender.tab && sender.frameId == 0
|
|
|
|
&& sender.tab.url == request.matchUrl) {
|
2015-05-14 21:24:10 +00:00
|
|
|
updateIcon(sender.tab, styles);
|
|
|
|
}
|
2017-03-14 23:27:52 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
2015-01-29 18:41:45 +00:00
|
|
|
case "saveStyle":
|
2017-03-14 23:27:52 +00:00
|
|
|
saveStyle(request).then(sendResponse);
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
2016-03-19 23:27:28 +00:00
|
|
|
case "invalidateCache":
|
|
|
|
if (typeof invalidateCache != "undefined") {
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
invalidateCache(false, request);
|
2016-03-19 23:27:28 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-01-29 18:41:45 +00:00
|
|
|
case "healthCheck":
|
|
|
|
getDatabase(function() { sendResponse(true); }, function() { sendResponse(false); });
|
2017-03-14 23:27:52 +00:00
|
|
|
return KEEP_CHANNEL_OPEN;
|
2015-03-13 22:45:38 +00:00
|
|
|
case "openURL":
|
|
|
|
openURL(request);
|
|
|
|
break;
|
2015-03-25 17:59:10 +00:00
|
|
|
case "styleDisableAll":
|
2017-03-18 22:35:27 +00:00
|
|
|
// fallthru to prefChanged
|
|
|
|
request = {prefName: 'disableAll', value: request.disableAll};
|
2015-03-25 17:59:10 +00:00
|
|
|
case "prefChanged":
|
2017-03-18 22:35:27 +00:00
|
|
|
if (typeof request.value == 'boolean' && contextMenus[request.prefName]) {
|
|
|
|
chrome.contextMenus.update(request.prefName, {checked: request.value});
|
2017-02-23 07:09:35 +00:00
|
|
|
}
|
2015-03-25 17:59:10 +00:00
|
|
|
break;
|
2017-03-14 23:27:52 +00:00
|
|
|
case "refreshAllTabs":
|
|
|
|
refreshAllTabs().then(sendResponse);
|
|
|
|
return KEEP_CHANNEL_OPEN;
|
2015-01-29 18:41:45 +00:00
|
|
|
}
|
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 = {
|
|
|
|
openManage() {
|
|
|
|
openURL({url: '/manage.html'});
|
|
|
|
},
|
|
|
|
styleDisableAll(state) {
|
|
|
|
prefs.set('disableAll',
|
|
|
|
typeof state == 'boolean' ? state : !prefs.get('disableAll'));
|
|
|
|
},
|
|
|
|
};
|
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) {
|
|
|
|
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
|
2015-08-01 12:06:47 +00:00
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
const contextMenus = {
|
|
|
|
'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-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)
|
|
|
|
|| /Safari\/[\d.]+$/.test(navigator.userAgent)
|
|
|
|
&& ![...navigator.plugins].some(p => p.name == 'Shockwave Flash')) {
|
|
|
|
contextMenus.editDeleteText = {
|
|
|
|
title: 'editDeleteText',
|
|
|
|
contexts: ['editable'],
|
|
|
|
documentUrlPatterns: [OWN_ORIGIN + 'edit*'],
|
|
|
|
click: (info, tab) => {
|
|
|
|
chrome.tabs.sendMessage(tab.id, {method: 'editDeleteText'});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-18 22:35:27 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) =>
|
|
|
|
contextMenus[info.menuItemId].click(info, tab));
|
|
|
|
|
|
|
|
Object.keys(contextMenus).forEach(id => {
|
|
|
|
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;
|
2015-03-25 17:59:10 +00:00
|
|
|
}
|
2017-03-18 22:35:27 +00:00
|
|
|
if (!item.contexts) {
|
|
|
|
item.contexts = ['browser_action'];
|
2017-02-20 13:55:56 +00:00
|
|
|
}
|
2017-03-18 22:35:27 +00:00
|
|
|
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.
|
2015-01-29 18:41:45 +00:00
|
|
|
getDatabase(function() {}, reportError);
|
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.
|
2015-01-30 18:35:37 +00:00
|
|
|
var editFullUrl = chrome.extension.getURL("edit.html");
|
|
|
|
chrome.tabs.onAttached.addListener(function(tabId, data) {
|
|
|
|
chrome.tabs.get(tabId, function(tabData) {
|
|
|
|
if (tabData.url.indexOf(editFullUrl) == 0) {
|
|
|
|
chrome.windows.get(tabData.windowId, {populate: true}, function(win) {
|
|
|
|
// If there's only one tab in this window, it's been dragged to new window
|
2015-10-05 11:27:17 +00:00
|
|
|
prefs.set("openEditInWindow", win.tabs.length == 1);
|
2015-01-30 18:35:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-03-13 22:45:38 +00:00
|
|
|
|
2015-05-05 15:21:45 +00:00
|
|
|
var codeMirrorThemes;
|
|
|
|
getCodeMirrorThemes(function(themes) {
|
|
|
|
codeMirrorThemes = themes;
|
2015-04-11 10:28:25 +00:00
|
|
|
});
|
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 => {
|
|
|
|
// Open FAQs page once after installation to guide new users,
|
|
|
|
// https://github.com/schomery/stylish-chrome/issues/22#issuecomment-279936160
|
|
|
|
if (!prefs.version) {
|
2017-03-14 09:14:10 +00:00
|
|
|
// do not display the FAQs page in development mode
|
|
|
|
if ('update_url' in chrome.runtime.getManifest()) {
|
|
|
|
let 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-02-23 07:37:25 +00:00
|
|
|
}
|
2017-03-11 09:26:24 +00:00
|
|
|
});
|
2017-03-15 12:41:39 +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-03-15 12:41:39 +00:00
|
|
|
const contentScripts = chrome.app.getDetails().content_scripts;
|
|
|
|
for (let cs of contentScripts) {
|
|
|
|
cs.matches = cs.matches.map(m => m == '<all_urls>' ? m : wildcardAsRegExp(m));
|
|
|
|
}
|
|
|
|
chrome.tabs.query({url: '*://*/*'}, tabs => {
|
|
|
|
for (let tab of tabs) {
|
|
|
|
for (let cs of contentScripts) {
|
|
|
|
for (let m of cs.matches) {
|
|
|
|
if (m == '<all_urls>' || tab.url.match(m)) {
|
|
|
|
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-03-18 22:35:27 +00:00
|
|
|
}, ignoreChromeError);
|
2017-03-15 12:41:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// inject the content script just once
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-18 22:35:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
function ignoreChromeError() {
|
|
|
|
chrome.runtime.lastError;
|
|
|
|
}
|