-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
38 lines (36 loc) · 1.23 KB
/
content.js
File metadata and controls
38 lines (36 loc) · 1.23 KB
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
// content.js
document.addEventListener("mouseup", function () {
const selectedText = window.getSelection().toString().trim();
if (selectedText) {
console.log("选中的文本:", selectedText); // 添加日志
// 如果有选中文本,发送到后台脚本
chrome.runtime.sendMessage(
{ action: "textSelected", text: selectedText },
function (response) {
console.log("后台脚本返回的响应:", response); // 添加日志
if (response && response.success) {
console.log("选中文本已发送到后台脚本");
} else {
console.error("发送选中文本失败");
}
}
);
}
});
// 监听来自后台脚本的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "highlightText") {
// 在网页中高亮指定的文本
highlightText(request.text);
sendResponse({ success: true });
}
});
// 在网页中高亮文本的函数
function highlightText(text) {
const bodyHTML = document.body.innerHTML;
const highlightedHTML = bodyHTML.replace(
new RegExp(text, "gi"),
(match) => `<span style="background-color: yellow;">${match}</span>`
);
document.body.innerHTML = highlightedHTML;
}