-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
65 lines (59 loc) · 2.08 KB
/
Copy pathbackground.js
File metadata and controls
65 lines (59 loc) · 2.08 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
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
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete") {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => {
// 处理现有元素的函数
function enableSelection() {
document
.querySelectorAll(".no-select *:not(input):not(textarea)")
.forEach((el) => {
el.style.userSelect = "auto";
el.style.webkitUserSelect = "auto";
el.style.msUserSelect = "auto";
el.style.mozUserSelect = "auto";
});
}
// 初始处理
enableSelection();
// 创建 MutationObserver 监听 DOM 变化
const observer = new MutationObserver((mutations) => {
let shouldProcess = false;
mutations.forEach((mutation) => {
// 检查是否有新节点添加
if (
mutation.type === "childList" &&
mutation.addedNodes.length > 0
) {
mutation.addedNodes.forEach((node) => {
// 检查新添加的节点是否是元素节点
if (node.nodeType === Node.ELEMENT_NODE) {
// 检查是否包含 .no-select 元素
if (node.querySelector && node.querySelector(".no-select")) {
shouldProcess = true;
}
// 检查节点本身是否是 .no-select 元素
if (node.classList && node.classList.contains("no-select")) {
shouldProcess = true;
}
}
});
}
});
// 如果有相关变化,重新处理
if (shouldProcess) {
// 延迟执行以确保 DOM 完全更新
setTimeout(enableSelection, 100);
}
});
// 开始监听
observer.observe(document.body, {
childList: true,
subtree: true,
});
// 定期检查(作为备用方案)
setInterval(enableSelection, 2000);
},
});
}
});