2017-03-15 12:41:39 +00:00
|
|
|
/* globals wildcardAsRegExp, KEEP_CHANNEL_OPEN */
|
|
|
|
|
2015-07-24 11:42:46 +00:00
|
|
|
var frameIdMessageable;
|
2015-08-01 12:06:47 +00:00
|
|
|
runTryCatch(function() {
|
2015-07-24 11:42:46 +00:00
|
|
|
chrome.tabs.sendMessage(0, {}, {frameId: 0}, function() {
|
|
|
|
var clearError = chrome.runtime.lastError;
|
|
|
|
frameIdMessageable = true;
|
|
|
|
});
|
2015-08-01 12:06:47 +00:00
|
|
|
});
|
2015-07-24 11:42:46 +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.
|
2015-02-23 22:48:27 +00:00
|
|
|
chrome.webNavigation.onCommitted.addListener(webNavigationListener.bind(this, "styleApply"));
|
2016-01-31 00:17:25 +00:00
|
|
|
// Not supported in Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1239349
|
|
|
|
if ("onHistoryStateUpdated" in chrome.webNavigation) {
|
|
|
|
chrome.webNavigation.onHistoryStateUpdated.addListener(webNavigationListener.bind(this, "styleReplaceAll"));
|
|
|
|
}
|
2015-05-14 21:24:10 +00:00
|
|
|
chrome.webNavigation.onBeforeNavigate.addListener(webNavigationListener.bind(this, null));
|
2015-02-23 22:48:27 +00:00
|
|
|
function webNavigationListener(method, data) {
|
2015-02-17 18:52:32 +00:00
|
|
|
// Until Chrome 41, we can't target a frame with a message
|
|
|
|
// (https://developer.chrome.com/extensions/tabs#method-sendMessage)
|
|
|
|
// so a style affecting a page with an iframe will affect the main page as well.
|
2015-07-24 11:42:46 +00:00
|
|
|
// Skip doing this for frames in pre-41 to prevent page flicker.
|
|
|
|
if (data.frameId != 0 && !frameIdMessageable) {
|
2015-02-17 18:52:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-30 16:36:46 +00:00
|
|
|
getStyles({matchUrl: data.url, enabled: true, asHash: true}, function(styleHash) {
|
2015-05-14 21:24:10 +00:00
|
|
|
if (method) {
|
2015-07-24 11:42:46 +00:00
|
|
|
chrome.tabs.sendMessage(data.tabId, {method: method, styles: styleHash},
|
|
|
|
frameIdMessageable ? {frameId: data.frameId} : undefined);
|
|
|
|
}
|
|
|
|
if (data.frameId == 0) {
|
|
|
|
updateIcon({id: data.tabId, url: data.url}, styleHash);
|
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
|
|
|
|
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];
|
|
|
|
});
|
|
|
|
|
2016-01-31 00:06:04 +00:00
|
|
|
chrome.runtime.onMessage.addListener(function(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") {
|
|
|
|
invalidateCache(false);
|
|
|
|
}
|
|
|
|
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":
|
|
|
|
chrome.contextMenus.update("disableAll", {checked: request.disableAll});
|
|
|
|
break;
|
|
|
|
case "prefChanged":
|
|
|
|
if (request.prefName == "show-badge") {
|
|
|
|
chrome.contextMenus.update("show-badge", {checked: request.value});
|
|
|
|
}
|
2017-02-23 07:09:35 +00:00
|
|
|
else if (request.prefName === 'disableAll') {
|
|
|
|
chrome.contextMenus.update("disableAll", {checked: request.value});
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-08 05:22:28 +00:00
|
|
|
|
|
|
|
// Not available in Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1240350
|
2016-03-08 05:24:35 +00:00
|
|
|
if ("commands" in chrome) {
|
2016-03-08 05:22:28 +00:00
|
|
|
chrome.commands.onCommand.addListener(function(command) {
|
|
|
|
switch (command) {
|
|
|
|
case "openManage":
|
|
|
|
openURL({url: chrome.extension.getURL("manage.html")});
|
|
|
|
break;
|
|
|
|
case "styleDisableAll":
|
|
|
|
disableAllStylesToggle();
|
|
|
|
chrome.contextMenus.update("disableAll", {checked: prefs.get("disableAll")});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-03-24 14:07:59 +00:00
|
|
|
|
2015-04-22 17:13:21 +00:00
|
|
|
// contextMenus API is present in ancient Chrome but it throws an exception
|
|
|
|
// upon encountering the unsupported parameter value "browser_action", so we have to catch it.
|
2015-08-01 12:06:47 +00:00
|
|
|
runTryCatch(function() {
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
id: "show-badge", title: chrome.i18n.getMessage("menuShowBadge"),
|
2015-10-05 11:27:17 +00:00
|
|
|
type: "checkbox", contexts: ["browser_action"], checked: prefs.get("show-badge")
|
2015-08-01 12:06:47 +00:00
|
|
|
}, function() { var clearError = chrome.runtime.lastError });
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
id: "disableAll", title: chrome.i18n.getMessage("disableAllStyles"),
|
2015-10-05 11:27:17 +00:00
|
|
|
type: "checkbox", contexts: ["browser_action"], checked: prefs.get("disableAll")
|
2015-08-01 12:06:47 +00:00
|
|
|
}, function() { var clearError = chrome.runtime.lastError });
|
2017-02-20 13:55:56 +00:00
|
|
|
chrome.contextMenus.create({
|
|
|
|
id: "open-manager", title: chrome.i18n.getMessage("openStylesManager"),
|
|
|
|
type: "normal", contexts: ["browser_action"]
|
|
|
|
}, function() {var clearError = chrome.runtime.lastError});
|
2015-08-01 12:06:47 +00:00
|
|
|
});
|
|
|
|
|
2015-03-25 17:59:10 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener(function(info, tab) {
|
|
|
|
if (info.menuItemId == "disableAll") {
|
|
|
|
disableAllStylesToggle(info.checked);
|
2017-02-14 15:35:53 +00:00
|
|
|
}
|
2017-02-20 13:55:56 +00:00
|
|
|
else if (info.menuItemId === 'show-badge') {
|
2015-10-05 11:27:17 +00:00
|
|
|
prefs.set(info.menuItemId, info.checked);
|
2015-03-25 17:59:10 +00:00
|
|
|
}
|
2017-02-20 13:55:56 +00:00
|
|
|
else if (info.menuItemId === 'open-manager') {
|
|
|
|
openURL({url: chrome.extension.getURL("manage.html")});
|
|
|
|
}
|
2015-03-25 17:59:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function disableAllStylesToggle(newState) {
|
|
|
|
if (newState === undefined || newState === null) {
|
2015-10-05 11:27:17 +00:00
|
|
|
newState = !prefs.get("disableAll");
|
2015-03-25 17:59:10 +00:00
|
|
|
}
|
2015-10-05 11:27:17 +00:00
|
|
|
prefs.set("disableAll", newState);
|
2015-03-25 17:59:10 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 18:41:45 +00:00
|
|
|
// Get the DB so that any first run actions will be performed immediately when the background page loads.
|
|
|
|
getDatabase(function() {}, reportError);
|
2015-01-30 18:35:37 +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.
|
|
|
|
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
|
|
|
|
|
|
|
function openURL(options) {
|
|
|
|
chrome.tabs.query({currentWindow: true, url: options.url}, function(tabs) {
|
|
|
|
// switch to an existing tab with the requested url
|
|
|
|
if (tabs.length) {
|
|
|
|
chrome.tabs.highlight({windowId: tabs[0].windowId, tabs: tabs[0].index}, function (window) {});
|
|
|
|
} else {
|
|
|
|
delete options.method;
|
2015-05-21 09:34:44 +00:00
|
|
|
getActiveTab(function(tab) {
|
2015-03-13 22:45:38 +00:00
|
|
|
// re-use an active new tab page
|
2015-05-21 09:34:44 +00:00
|
|
|
chrome.tabs[tab.url == "chrome://newtab/" ? "update" : "create"](options);
|
2015-03-13 22:45:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-04-11 10:28:25 +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
|
|
|
|
|
|
|
// after the extension was enabled or just installed
|
|
|
|
injectContentScripts({reason: 'update'});
|
|
|
|
// after an actual update
|
|
|
|
chrome.runtime.onInstalled.addListener(injectContentScripts);
|
|
|
|
|
|
|
|
function injectContentScripts({reason, previousVersion, id, checkFirst} = {}) {
|
|
|
|
// reason: install, update, chrome_update, shared_module_update
|
|
|
|
// the "install" case is ignored because it was already handled by explicit invocation of this function
|
|
|
|
if (!/update/.test(reason)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
}, result => chrome.runtime.lastError); // ignore lastError just in case
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// inject the content script just once
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|