-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
110 lines (90 loc) · 3.05 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
// 检查全局变量是否已经定义
if (typeof isSelecting === 'undefined') {
var isSelecting = false;
}
if (typeof selectedElement === 'undefined') {
var selectedElement = null;
}
if (typeof currentContentType === 'undefined') {
var currentContentType = null;
}
// 监听消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'startSelect') {
if (isSelecting) {
cancelSelection(); // 取消当前的选择
}
isSelecting = true;
currentContentType = message.contentType;
startSelectingElement(currentContentType);
}
});
// 取消选择操作的函数
function cancelSelection() {
isSelecting = false;
currentContentType = null;
// 移除事件监听器
document.removeEventListener('mousemove', highlightElement);
document.removeEventListener('click', selectElement);
// 清除高亮
if (selectedElement) {
selectedElement.style.backgroundColor = '';
selectedElement.style.outline = '';
}
}
function startSelectingElement(contentType) {
// 鼠标悬停时高亮显示
document.addEventListener('mousemove', highlightElement);
// 鼠标点击选择元素
document.addEventListener('click', selectElement);
}
function highlightElement(event) {
if (!isSelecting) return;
// 移除之前的高亮
if (selectedElement) {
selectedElement.style.backgroundColor = '';
selectedElement.style.outline = '';
}
selectedElement = event.target;
// 高亮整个选择区域
selectedElement.style.backgroundColor = 'rgba(255, 255, 0, 0.5)';
selectedElement.style.outline = '3px solid red';
}
function selectElement(event) {
event.preventDefault();
event.stopPropagation();
if (!isSelecting) return;
const selectedElement = event.target;
if (selectedElement) {
selectedElement.style.backgroundColor = '';
selectedElement.style.outline = '';
}
const selectedHtmlWithStyles = getHtmlWithInlineStyles(selectedElement);
const elementData = {
html: selectedHtmlWithStyles
};
chrome.runtime.sendMessage({
type: 'elementSelected',
elemNumber: currentContentType === 'content1' ? 1 : 2,
element: elementData
});
cancelSelection();
alert(currentContentType===null?'success':(currentContentType === 'content1' ? '内容1已选择' : '内容2已选择'));
}
// 提取HTML并将所有计算的样式转换为内联样式
function getHtmlWithInlineStyles(element) {
const clone = element.cloneNode(true);
applyInlineStyles(clone);
return clone.outerHTML;
}
// 将计算的样式应用为内联样式
function applyInlineStyles(element) {
const computedStyle = getComputedStyle(element);
for (let i = 0; i < computedStyle.length; i++) {
const property = computedStyle[i];
element.style[property] = computedStyle.getPropertyValue(property);
}
for (let i = 0; i < element.children.length; i++) {
applyInlineStyles(element.children[i]);
}
}