2017-03-14 23:27:52 +00:00
|
|
|
// keep message channel open for sendResponse in chrome.runtime.onMessage listener
|
|
|
|
const KEEP_CHANNEL_OPEN = true;
|
2017-03-15 11:55:20 +00:00
|
|
|
const OWN_ORIGIN = chrome.runtime.getURL('');
|
2017-03-14 23:27:52 +00:00
|
|
|
|
2012-04-16 01:56:12 +00:00
|
|
|
function notifyAllTabs(request) {
|
2017-03-15 17:15:32 +00:00
|
|
|
// list all tabs including chrome-extension:// which can be ours
|
|
|
|
chrome.tabs.query({}, tabs => {
|
|
|
|
for (let tab of tabs) {
|
|
|
|
if (request.codeIsUpdated !== false || tab.url.startsWith(OWN_ORIGIN)) {
|
|
|
|
chrome.tabs.sendMessage(tab.id, request);
|
|
|
|
updateIcon(tab);
|
|
|
|
}
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
});
|
2015-04-07 17:07:45 +00:00
|
|
|
// notify all open popups
|
2017-03-15 11:58:59 +00:00
|
|
|
const reqPopup = Object.assign({}, request, {method: 'updatePopup', reason: request.method});
|
2016-01-30 23:08:10 +00:00
|
|
|
chrome.runtime.sendMessage(reqPopup);
|
2017-03-14 12:18:58 +00:00
|
|
|
// notify self: the message no longer is sent to the origin in new Chrome
|
|
|
|
if (typeof applyOnMessage !== 'undefined') {
|
|
|
|
applyOnMessage(reqPopup);
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 23:27:52 +00:00
|
|
|
function refreshAllTabs() {
|
|
|
|
return new Promise(resolve => {
|
2017-03-15 17:15:32 +00:00
|
|
|
// list all tabs including chrome-extension:// which can be ours
|
|
|
|
chrome.tabs.query({}, tabs => {
|
|
|
|
const lastTab = tabs[tabs.length - 1];
|
|
|
|
for (let tab of tabs) {
|
|
|
|
getStyles({matchUrl: tab.url, enabled: true, asHash: true}, styles => {
|
|
|
|
const message = {method: 'styleReplaceAll', styles};
|
|
|
|
if (tab.url == location.href && typeof applyOnMessage !== 'undefined') {
|
|
|
|
applyOnMessage(message);
|
|
|
|
} else {
|
|
|
|
chrome.tabs.sendMessage(tab.id, message);
|
|
|
|
}
|
|
|
|
updateIcon(tab, styles);
|
|
|
|
if (tab == lastTab) {
|
|
|
|
resolve();
|
|
|
|
}
|
2017-03-14 23:27:52 +00:00
|
|
|
});
|
2017-03-15 17:15:32 +00:00
|
|
|
}
|
2017-03-14 23:27:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:24:10 +00:00
|
|
|
function updateIcon(tab, styles) {
|
2015-05-21 09:34:44 +00:00
|
|
|
// while NTP is still loading only process the request for its main frame with a real url
|
|
|
|
// (but when it's loaded we should process style toggle requests from popups, for example)
|
|
|
|
if (tab.url == "chrome://newtab/" && tab.status != "complete") {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-14 21:24:10 +00:00
|
|
|
if (styles) {
|
|
|
|
// check for not-yet-existing tabs e.g. omnibox instant search
|
|
|
|
chrome.tabs.get(tab.id, function() {
|
|
|
|
if (!chrome.runtime.lastError) {
|
|
|
|
// for 'styles' asHash:true fake the length by counting numeric ids manually
|
|
|
|
if (styles.length === undefined) {
|
|
|
|
styles.length = 0;
|
|
|
|
for (var id in styles) {
|
|
|
|
styles.length += id.match(/^\d+$/) ? 1 : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stylesReceived(styles);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-05-21 09:34:44 +00:00
|
|
|
getTabRealURL(tab, function(url) {
|
|
|
|
// if we have access to this, call directly. a page sending a message to itself doesn't seem to work right.
|
|
|
|
if (typeof getStyles != "undefined") {
|
|
|
|
getStyles({matchUrl: url, enabled: true}, stylesReceived);
|
|
|
|
} else {
|
2016-01-30 23:08:10 +00:00
|
|
|
chrome.runtime.sendMessage({method: "getStyles", matchUrl: url, enabled: true}, stylesReceived);
|
2015-05-21 09:34:44 +00:00
|
|
|
}
|
|
|
|
});
|
2012-04-16 01:56:12 +00:00
|
|
|
|
2015-05-14 21:24:10 +00:00
|
|
|
function stylesReceived(styles) {
|
2015-10-05 11:27:17 +00:00
|
|
|
var disableAll = "disableAll" in styles ? styles.disableAll : prefs.get("disableAll");
|
2017-03-18 23:00:03 +00:00
|
|
|
var postfix = disableAll ? "x" : styles.length == 0 ? "w" : "";
|
2015-05-14 21:24:10 +00:00
|
|
|
chrome.browserAction.setIcon({
|
2016-08-22 11:01:27 +00:00
|
|
|
path: {
|
|
|
|
// Material Design 2016 new size is 16px
|
|
|
|
16: "16" + postfix + ".png", 32: "32" + postfix + ".png",
|
|
|
|
// Chromium forks or non-chromium browsers may still use the traditional 19px
|
|
|
|
19: "19" + postfix + ".png", 38: "38" + postfix + ".png",
|
|
|
|
},
|
2015-05-14 21:24:10 +00:00
|
|
|
tabId: tab.id
|
2015-10-11 14:09:49 +00:00
|
|
|
}, function() {
|
|
|
|
// if the tab was just closed an error may occur,
|
|
|
|
// e.g. 'windowPosition' pref updated in edit.js::window.onbeforeunload
|
|
|
|
if (!chrome.runtime.lastError) {
|
|
|
|
var t = prefs.get("show-badge") && styles.length ? ("" + styles.length) : "";
|
|
|
|
chrome.browserAction.setBadgeText({text: t, tabId: tab.id});
|
2017-02-14 15:35:53 +00:00
|
|
|
chrome.browserAction.setBadgeBackgroundColor({
|
|
|
|
color: prefs.get(disableAll ? 'badgeDisabled' : 'badgeNormal')
|
|
|
|
});
|
2015-10-11 14:09:49 +00:00
|
|
|
}
|
2015-05-14 21:24:10 +00:00
|
|
|
});
|
|
|
|
//console.log("Tab " + tab.id + " (" + tab.url + ") badge text set to '" + t + "'.");
|
|
|
|
}
|
2012-04-16 01:56:12 +00:00
|
|
|
}
|
2015-05-21 09:34:44 +00:00
|
|
|
|
|
|
|
function getActiveTab(callback) {
|
|
|
|
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
|
|
|
|
callback(tabs[0]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getActiveTabRealURL(callback) {
|
|
|
|
getActiveTab(function(tab) {
|
|
|
|
getTabRealURL(tab, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTabRealURL(tab, callback) {
|
|
|
|
if (tab.url != "chrome://newtab/") {
|
|
|
|
callback(tab.url);
|
2015-07-24 11:36:43 +00:00
|
|
|
} else {
|
|
|
|
chrome.webNavigation.getFrame({tabId: tab.id, frameId: 0, processId: -1}, function(frame) {
|
|
|
|
frame && callback(frame.url);
|
2015-05-21 09:34:44 +00:00
|
|
|
});
|
2015-07-24 11:36:43 +00:00
|
|
|
}
|
2015-05-21 09:34:44 +00:00
|
|
|
}
|
2017-03-15 12:41:39 +00:00
|
|
|
|
|
|
|
function stringAsRegExp(s, flags) {
|
|
|
|
return new RegExp(s.replace(/[{}()\[\]\/\\.+?^$:=*!|]/g, "\\$&"), flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// expands * as .*?
|
|
|
|
function wildcardAsRegExp(s, flags) {
|
|
|
|
return new RegExp(s.replace(/[{}()\[\]\/\\.+?^$:=!|]/g, "\\$&").replace(/\*/g, '.*?'), flags);
|
|
|
|
}
|
2017-03-16 13:36:33 +00:00
|
|
|
|
|
|
|
var configureCommands = {
|
|
|
|
get url () {
|
|
|
|
return navigator.userAgent.indexOf('OPR') > -1 ?
|
|
|
|
'opera://settings/configureCommands' :
|
|
|
|
'chrome://extensions/configureCommands'
|
|
|
|
},
|
|
|
|
open: () => {
|
|
|
|
chrome.tabs.create({
|
|
|
|
'url': configureCommands.url
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|