Compare commits
3 Commits
v0.0.5
...
81f731ad77
Author | SHA1 | Date | |
---|---|---|---|
81f731ad77 | |||
e13a5b4c3b | |||
23564b3c29 |
34
src/bg.js
34
src/bg.js
@@ -1,25 +1,13 @@
|
|||||||
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';
|
||||||
|
|
||||||
var changeIcon = function(color) {
|
var changeIcon = function(color, currentIcon) {
|
||||||
var getFavicon = function(){
|
|
||||||
var favicon = undefined;
|
|
||||||
var nodeList = document.getElementsByTagName("link");
|
|
||||||
for (var i = 0; i < nodeList.length; i++)
|
|
||||||
{
|
|
||||||
if ((nodeList[i].getAttribute("rel") == "icon") || (nodeList[i].getAttribute("rel") == "shortcut icon")) {
|
|
||||||
favicon = nodeList[i].getAttribute("href");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return favicon;
|
|
||||||
};
|
|
||||||
|
|
||||||
var canvas = document.createElement('canvas');
|
var canvas = document.createElement('canvas');
|
||||||
canvas.width = 16;
|
canvas.width = 16;
|
||||||
canvas.height = 16;
|
canvas.height = 16;
|
||||||
var ctx = canvas.getContext('2d');
|
var ctx = canvas.getContext('2d');
|
||||||
var img = new Image();
|
var img = new Image();
|
||||||
img.src = getFavicon();
|
img.src = currentIcon;
|
||||||
img.onload = function() {
|
img.onload = function() {
|
||||||
ctx.drawImage(img, 0, 0);
|
ctx.drawImage(img, 0, 0);
|
||||||
ctx.fillStyle = color;
|
ctx.fillStyle = color;
|
||||||
@@ -34,7 +22,7 @@ var changeIcon = function(color) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var generateRandomColor = function(seed) {
|
var generateRandomColor = function(seed) {
|
||||||
var selectColor = function (colorNum, colors){
|
var selectColor = function (colorNum, colors) {
|
||||||
if (colors < 1) {
|
if (colors < 1) {
|
||||||
colors = 1; // defaults to one color - avoid divide by zero
|
colors = 1; // defaults to one color - avoid divide by zero
|
||||||
}
|
}
|
||||||
@@ -68,7 +56,10 @@ var getTabParent = (tabs, id, currentTopLevelTabPos) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (tabs[i].id === id) {
|
if (tabs[i].id === id) {
|
||||||
return currentTopLevelTabPos;
|
return {
|
||||||
|
parentIndex: currentTopLevelTabPos,
|
||||||
|
faviconUrl: tabs[i].faviconUrl
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
if (tabs[i].children && tabs[i].children.length > 0) {
|
if (tabs[i].children && tabs[i].children.length > 0) {
|
||||||
let ret = getTabParent(tabs[i].children, id, currentTopLevelTabPos);
|
let ret = getTabParent(tabs[i].children, id, currentTopLevelTabPos);
|
||||||
@@ -90,8 +81,10 @@ var updateAllColorsOnAllTabs = async () => {
|
|||||||
|
|
||||||
for (let i = 0; i < tstTabs.length; i++) {
|
for (let i = 0; i < tstTabs.length; i++) {
|
||||||
if (tstTabs[i].status === 'complete') {
|
if (tstTabs[i].status === 'complete') {
|
||||||
|
let t = getTabParent(tstTabs, tstTabs[i].id);
|
||||||
browser.tabs.executeScript(tstTabs[i].id, {
|
browser.tabs.executeScript(tstTabs[i].id, {
|
||||||
code: '(' + changeIcon.toString() + ')("' + generateRandomColor(getTabParent(tstTabs, tstTabs[i].id)) + '")'
|
code: '(' + changeIcon.toString() + ')' +
|
||||||
|
'("' + generateRandomColor(t.parentIndex) + '", "' + t.faviconUrl + '")'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,14 +106,17 @@ browser.runtime.onMessageExternal.addListener((aMessage, aSender) => {
|
|||||||
|
|
||||||
//when a tab is created
|
//when a tab is created
|
||||||
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
||||||
var tstTab = await browser.runtime.sendMessage(kTST_ID, {
|
var tstTabs = await browser.runtime.sendMessage(kTST_ID, {
|
||||||
type: 'get-tree',
|
type: 'get-tree',
|
||||||
window: tab.windowId
|
window: tab.windowId
|
||||||
});
|
});
|
||||||
|
|
||||||
if (tab.status === 'complete') {
|
if (tab.status === 'complete') {
|
||||||
|
var t = getTabParent(tstTabs, tab.id);
|
||||||
|
|
||||||
browser.tabs.executeScript(tab.id, {
|
browser.tabs.executeScript(tab.id, {
|
||||||
code: '(' + changeIcon.toString() + ')("' + generateRandomColor(getTabParent(tstTab, tab.id)) + '")'
|
code: '(' + changeIcon.toString() + ')' +
|
||||||
|
'("' + generateRandomColor(t.parentIndex) + '", "' + t.faviconUrl + '")'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"description": "color tabs based on their parent.",
|
"description": "Color tabs based on their parent.",
|
||||||
"manifest_version": 2,
|
"manifest_version": 2,
|
||||||
"name": "tab-groupcolor",
|
"name": "Color Tab Group",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"homepage_url": "https://git.e.tern.al/s2/tab-groupcolor",
|
"homepage_url": "https://git.e.tern.al/s2/tab-groupcolor",
|
||||||
"icons": {
|
"icons": {
|
||||||
|
Reference in New Issue
Block a user