-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
146 lines (126 loc) · 4.29 KB
/
content.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
144
145
146
(function() {
if (window.hasOwnProperty('colorExtractorSidebarInjected')) {
return;
}
window.colorExtractorSidebarInjected = true;
let sidebarInjected = false;
function injectSidebar() {
if (sidebarInjected) return;
const sidebar = document.createElement('iframe');
sidebar.id = 'colorExtractorSidebar';
sidebar.src = chrome.runtime.getURL('sidebar.html');
sidebar.style.width = '360px'; // 更新宽度
document.body.appendChild(sidebar);
sidebarInjected = true;
setTimeout(() => {
sidebar.classList.add('open');
}, 100);
}
function toggleSidebar() {
const sidebar = document.getElementById('colorExtractorSidebar');
if (sidebar) {
sidebar.classList.toggle('open');
} else {
injectSidebar();
}
}
function extractColorsAndGradients() {
const elements = document.getElementsByTagName('*');
const colorMap = new Map();
const gradients = new Set();
function addColor(map, color) {
map.set(color, (map.get(color) || 0) + 1);
}
function rgbToHex(color) {
// 匹配RGB或RGBA值
const rgbRegex = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/;
const rgbaRegex = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/;
let rgb, a;
if (rgbRegex.test(color)) {
rgb = color.match(rgbRegex).slice(1);
} else if (rgbaRegex.test(color)) {
const match = color.match(rgbaRegex);
rgb = match.slice(1, 4);
a = parseFloat(match[4]);
} else {
return color; // 如果不是RGB或RGBA格式,直接返原始颜色值
}
// 将RGB值转换为16进制
const hex = rgb.map(x => {
const hex = parseInt(x).toString(16);
return hex.length === 1 ? '0' + hex : hex;
});
// 如果是RGBA,添加alpha通道
if (a !== undefined) {
const alpha = Math.round(a * 255).toString(16);
hex.push(alpha.length === 1 ? '0' + alpha : alpha);
}
return '#' + hex.join('');
}
for (let element of elements) {
const style = window.getComputedStyle(element);
const color = style.color;
const backgroundColor = style.backgroundColor;
const backgroundImage = style.backgroundImage;
if (color !== 'rgba(0, 0, 0, 0)') {
const hexColor = rgbToHex(color);
addColor(colorMap, hexColor);
}
if (backgroundColor !== 'rgba(0, 0, 0, 0)') {
const hexBackgroundColor = rgbToHex(backgroundColor);
addColor(colorMap, hexBackgroundColor);
}
if (backgroundImage !== 'none' && backgroundImage.includes('gradient')) {
gradients.add(backgroundImage);
}
}
const sortedColors = Array.from(colorMap.entries())
.sort((a, b) => b[1] - a[1])
.map(entry => entry[0]);
return {
colors: sortedColors,
gradients: Array.from(gradients)
};
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggleSidebar") {
toggleSidebar();
sendResponse({status: "Sidebar toggled"});
}
});
// 监听来自 sidebar 的消息
window.addEventListener('message', function(event) {
if (event.data.action === 'closeSidebar') {
const sidebar = document.getElementById('colorExtractorSidebar');
if (sidebar) {
sidebar.classList.remove('open');
}
} else if (event.data.action === 'extractColors') {
const results = extractColorsAndGradients();
const sidebar = document.getElementById('colorExtractorSidebar');
if (sidebar) {
sidebar.contentWindow.postMessage({
action: 'displayColors',
colors: results.colors,
gradients: results.gradients
}, '*');
}
} else if (event.data.action === 'copyToClipboard') {
// 执行复制操作
const textArea = document.createElement("textarea");
textArea.value = event.data.text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
// 通知 sidebar 复制成功
const sidebar = document.getElementById('colorExtractorSidebar');
if (sidebar) {
sidebar.contentWindow.postMessage({
action: 'copySuccess'
}, '*');
}
}
});
console.log('Content script loaded and ready');
})();