|
|
@@ -1,10 +1,9 @@
|
|
|
|
const kTST_ID = 'treestyletab@piro.sakura.ne.jp';
|
|
|
|
const kTST_ID = 'treestyletab@piro.sakura.ne.jp';
|
|
|
|
const MY_EXTENSION_NAME = 'tab-groupcolor';
|
|
|
|
const MY_EXTENSION_NAME = 'tab-groupcolor';
|
|
|
|
|
|
|
|
let tabIcons = {};
|
|
|
|
|
|
|
|
|
|
|
|
var changeIcon = function(color, currentIcon, options) {
|
|
|
|
var changeIcon = function(color, currentIcon, options) {
|
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
canvas.width = 16;
|
|
|
|
|
|
|
|
canvas.height = 16;
|
|
|
|
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
var img = new Image();
|
|
|
|
var img = new Image();
|
|
|
|
|
|
|
|
|
|
|
@@ -15,19 +14,26 @@ var changeIcon = function(color, currentIcon, options) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
img.onload = function() {
|
|
|
|
img.onload = function() {
|
|
|
|
|
|
|
|
canvas.width = this.naturalWidth;
|
|
|
|
|
|
|
|
canvas.height = this.naturalHeight;
|
|
|
|
ctx.drawImage(img, 0, 0);
|
|
|
|
ctx.drawImage(img, 0, 0);
|
|
|
|
ctx.fillStyle = color;
|
|
|
|
ctx.fillStyle = color;
|
|
|
|
if (options.globals.orientation === 'vertical') {
|
|
|
|
if (options.globals.orientation === 'vertical') {
|
|
|
|
ctx.fillRect(0, 0, options.globals.width, 16);
|
|
|
|
ctx.fillRect(0, 0, (options.globals.width / 16) * canvas.width, canvas.height);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ctx.fillRect(0, 0, 16, options.globals.width);
|
|
|
|
ctx.fillRect(0, 0, canvas.width, (options.globals.width / 16) * canvas.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var link = document.createElement('link');
|
|
|
|
var link = document.createElement('link');
|
|
|
|
link.type = 'image/x-icon';
|
|
|
|
link.type = 'image/x-icon';
|
|
|
|
link.rel = 'shortcut icon';
|
|
|
|
link.rel = 'shortcut icon';
|
|
|
|
link.href = canvas.toDataURL("image/x-icon");
|
|
|
|
link.href = canvas.toDataURL("image/x-icon");
|
|
|
|
|
|
|
|
link.id = 'tab-groupcolor' + '-favicon';
|
|
|
|
|
|
|
|
var oldicon = document.getElementById('tab-groupcolor' + '-favicon');
|
|
|
|
|
|
|
|
if (oldicon) {
|
|
|
|
|
|
|
|
oldicon.outerHTML = '';
|
|
|
|
|
|
|
|
delete oldicon;
|
|
|
|
|
|
|
|
}
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(link);
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
@@ -58,21 +64,21 @@ var registerToTST = async function() {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//returns the index of the tabs array object that contains the tab with the passed id
|
|
|
|
//returns the index of the tabs array object that contains the tab with the passed id
|
|
|
|
var getTabParent = async (tabs, id, currentTopLevelTabPos) => {
|
|
|
|
var getTabParent = (tabs, id, currentTopLevelTabPos) => {
|
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
|
|
|
|
|
|
|
|
//on a root node because there is no opener
|
|
|
|
//on a root node because there is no opener
|
|
|
|
if (typeof(tabs[i].openerTabId) === 'undefined' || tabs[i].openerTabId === tabs[i].id) {
|
|
|
|
if (!tabs[i].openerTabId || typeof(tabs[i].openerTabId) === 'undefined' || tabs[i].openerTabId === tabs[i].id) {
|
|
|
|
currentTopLevelTabPos = i;
|
|
|
|
currentTopLevelTabPos = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tabs[i].id === id) {
|
|
|
|
if (tabs[i].id === id) {
|
|
|
|
|
|
|
|
|
|
|
|
//get original favicon for this tab
|
|
|
|
//get original favicon for this tab
|
|
|
|
let orginalFavIconUrl = await browser.sessions.getTabValue(id, 'orginalFavIconUrl');
|
|
|
|
let orginalFavIconUrl = tabIcons[id];
|
|
|
|
if (!orginalFavIconUrl) {
|
|
|
|
if (!orginalFavIconUrl) {
|
|
|
|
orginalFavIconUrl = tabs[i].favIconUrl;
|
|
|
|
orginalFavIconUrl = tabs[i].favIconUrl;
|
|
|
|
await browser.sessions.setTabValue(id, 'orginalFavIconUrl', orginalFavIconUrl);
|
|
|
|
tabIcons[id] = orginalFavIconUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
@@ -81,7 +87,7 @@ var getTabParent = async (tabs, id, currentTopLevelTabPos) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (tabs[i].children && tabs[i].children.length > 0) {
|
|
|
|
if (tabs[i].children && tabs[i].children.length > 0) {
|
|
|
|
let ret = await getTabParent(tabs[i].children, id, currentTopLevelTabPos);
|
|
|
|
let ret = getTabParent(tabs[i].children, id, currentTopLevelTabPos);
|
|
|
|
if (ret) {
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -95,7 +101,7 @@ var updateAllColorsOnAllTabs = async () => {
|
|
|
|
allWindows.forEach(async w => {
|
|
|
|
allWindows.forEach(async w => {
|
|
|
|
var tstTabs = await browser.runtime.sendMessage(kTST_ID, {
|
|
|
|
var tstTabs = await browser.runtime.sendMessage(kTST_ID, {
|
|
|
|
type: 'get-tree',
|
|
|
|
type: 'get-tree',
|
|
|
|
window: w.id
|
|
|
|
window: w.id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var changeTabs = async (tabs) => {
|
|
|
|
var changeTabs = async (tabs) => {
|
|
|
@@ -103,14 +109,18 @@ var updateAllColorsOnAllTabs = async () => {
|
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
|
|
|
|
|
|
|
|
if (tabs[i].status === 'complete') {
|
|
|
|
if (tabs[i].status === 'complete') {
|
|
|
|
let t = await getTabParent(tstTabs, tabs[i].id);
|
|
|
|
let t = getTabParent(tstTabs, tabs[i].id);
|
|
|
|
|
|
|
|
let options = await loadOptions();
|
|
|
|
loadOptions().then((options) => {
|
|
|
|
let tabHost = new URL(tabs[i].url).host;
|
|
|
|
|
|
|
|
if (tabHost) {
|
|
|
|
|
|
|
|
tabHost = tabHost.toLowerCase();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(options.ignore && options.ignore.split('\n').includes(tabHost))) {
|
|
|
|
browser.tabs.executeScript(tabs[i].id, {
|
|
|
|
browser.tabs.executeScript(tabs[i].id, {
|
|
|
|
code: '(' + changeIcon.toString() + ')' +
|
|
|
|
code: '(' + changeIcon.toString() + ')' +
|
|
|
|
'("' + generateRandomColor(t.parentIndex) + '", "' + t.faviconUrl + '", ' + JSON.stringify(options) + ')'
|
|
|
|
'("' + generateRandomColor(t.parentIndex) + '", "' + t.faviconUrl + '", ' + JSON.stringify(options) + ')'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tabs[i].children && tabs[i].children.length > 0) {
|
|
|
|
if (tabs[i].children && tabs[i].children.length > 0) {
|
|
|
@@ -138,13 +148,20 @@ browser.runtime.onMessageExternal.addListener((aMessage, aSender) => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//when something happens with a tab
|
|
|
|
//when something happens with a tab
|
|
|
|
let tabEvents = ['onUpdated', 'onAttached', 'onDetached', 'onMoved', 'onRemoved']
|
|
|
|
let tabEvents = ['onAttached', 'onDetached', 'onMoved', 'onRemoved']
|
|
|
|
tabEvents.forEach((ev) => {
|
|
|
|
tabEvents.forEach((ev) => {
|
|
|
|
browser.tabs[ev].addListener(async (tabId, changeInfo, tab) => {
|
|
|
|
browser.tabs[ev].addListener((tabId, changeInfo, tab) => {
|
|
|
|
updateAllColorsOnAllTabs();
|
|
|
|
updateAllColorsOnAllTabs();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//when a tab reloads
|
|
|
|
|
|
|
|
browser.webNavigation.onCompleted.addListener((details) => {
|
|
|
|
|
|
|
|
delete tabIcons[details.tabId];
|
|
|
|
|
|
|
|
updateAllColorsOnAllTabs();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//when the options are updated
|
|
|
|
//when the options are updated
|
|
|
|
browser.storage.onChanged.addListener((changes) => {
|
|
|
|
browser.storage.onChanged.addListener((changes) => {
|
|
|
|
updateAllColorsOnAllTabs();
|
|
|
|
updateAllColorsOnAllTabs();
|
|
|
|