forked from DavidBevi/separator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_tabs_bb.js
143 lines (120 loc) · 5.17 KB
/
color_tabs_bb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(function color_tabs() {
"use strict";
const WHITE = chroma('#FFF');
const BLACK = chroma('#000');
class ColorTabs {
constructor() {
this.#addListeners();
}
#addListeners() {
chrome.tabs.onCreated.addListener(() => this.#colorTabsDelayed());
chrome.tabs.onActivated.addListener(() => this.#colorTabsDelayed());
vivaldi.tabsPrivate.onThemeColorChanged.addListener(() => this.#colorTabsDelayed());
vivaldi.prefs.onChanged.addListener((info) => {
if (info.path.startsWith('vivaldi.themes')) {
this.#colorTabsDelayed();
}
});
}
#colorTabsDelayed() {
this.#colorTabs();
setTimeout(() => this.#colorTabs(), 100);
}
async #colorTabs() {
const tabs = document.querySelectorAll('div.tab');
const domain = 'davidbevi.github.io';
tabs.forEach(async (tab) => {
const chromeTab = await this.#getChromeTab(this.#getTabId(tab));
const isMatchingDomain = chromeTab.url.includes(domain);
if (isMatchingDomain) {
const favicon = tab.querySelector('img');
if (favicon) {
const color = await this.#getColorFromFavicon(favicon);
if (color) {
tab.style.backgroundColor = color;
tab.style.color = color.luminance() > 0.4 ? BLACK.css() : WHITE.css();
this.#hideFavicon(favicon);
}
}
}
});
}
async #getColorFromFavicon(favicon) {
return new Promise((resolve, reject) => {
const image = new Image();
image.crossOrigin = 'Anonymous';
image.src = favicon.src;
image.onload = () => {
const palette = this.#getPalette(image);
if (palette && palette.length > 0) {
resolve(chroma(palette[0]));
} else {
resolve(null);
}
};
image.onerror = reject;
});
}
#getPalette(image) {
const w = image.width;
const h = image.height;
const canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
const context = canvas.getContext('2d');
context.imageSmoothingEnabled = false;
context.drawImage(image, 0, 0, w, h);
const pixelData = context.getImageData(0, 0, w, h).data;
const pixelCount = pixelData.length / 4;
const colorPalette = [];
for (let pixelIndex = 0; pixelIndex < pixelCount; pixelIndex++) {
const offset = 4 * pixelIndex;
const red = pixelData[offset];
const green = pixelData[offset + 1];
const blue = pixelData[offset + 2];
let colorIndex;
if (!(red === 0 || red > 240 && green > 240 && blue > 240)) {
for (let colorIndexIterator = 0; colorIndexIterator < colorPalette.length; colorIndexIterator++) {
const currentColor = colorPalette[colorIndexIterator];
if (red === currentColor[0] && green === currentColor[1] && blue === currentColor[2]) {
colorIndex = colorIndexIterator;
break;
}
}
if (colorIndex === undefined) {
colorPalette.push([red, green, blue, 1]);
} else {
colorPalette[colorIndex][3]++;
}
}
}
colorPalette.sort((a, b) => b[3] - a[3]);
const topColors = colorPalette.slice(0, Math.min(10, colorPalette.length));
return topColors.map(color => [color[0], color[1], color[2]]);
}
#getTabId(tab) {
return tab.getAttribute('data-id').slice(4);
}
async #getChromeTab(tabId) {
return tabId.length < 16 ? await chrome.tabs.get(Number(tabId)) : await this.#getFirstChromeTabInGroup(tabId);
}
async #getFirstChromeTabInGroup(groupId) {
const tabs = await chrome.tabs.query({currentWindow: true});
return tabs.find((tab) => {
const vivExtData = JSON.parse(tab.vivExtData);
return vivExtData.group === groupId;
});
}
#hideFavicon(favicon) {
favicon.style.display = 'none';
}
};
setTimeout(() => {
var interval = setInterval(() => {
if (document.querySelector('#browser')) {
window.colorTabs = new ColorTabs();
clearInterval(interval);
}
}, 100);
}, 1000);
})();