-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
132 lines (106 loc) · 3.43 KB
/
content.js
File metadata and controls
132 lines (106 loc) · 3.43 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
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
(function () {
let titleObserver = null;
let suppressObserver = false; // 防止 observer 自触发
function initTabTitleModifier() {
if (window.__TAB_TITLE_MODIFIER__) return;
window.__TAB_TITLE_MODIFIER__ = true;
const panel = document.createElement("div");
panel.id = "tab-title-panel";
panel.innerHTML = `
<div class="header">Tab Title</div>
<input type="text" placeholder="Enter new title" />
<button>Apply</button>
`;
document.body.appendChild(panel);
const input = panel.querySelector("input");
const button = panel.querySelector("button");
input.value = computeExpectedTitle(document.title);
button.addEventListener("click", () => {
const v = input.value.trim();
if (v) applyTitle(v);
});
// 拖拽
let dragging = false, offsetX = 0, offsetY = 0;
const header = panel.querySelector(".header");
header.addEventListener("mousedown", (e) => {
dragging = true;
offsetX = e.clientX - panel.offsetLeft;
offsetY = e.clientY - panel.offsetTop;
});
document.addEventListener("mousemove", (e) => {
if (!dragging) return;
panel.style.left = e.clientX - offsetX + "px";
panel.style.top = e.clientY - offsetY + "px";
});
document.addEventListener("mouseup", () => {
dragging = false;
});
startTitleObserver();
}
/** 每次都基于“当前 title”计算期望值 */
function computeExpectedTitle(currentTitle) {
const isChatGPT = location.host === "chatgpt.com";
const isConversation = location.pathname.startsWith("/g/");
if (!isChatGPT || !isConversation) return currentTitle;
const prefix = "ChatGPT - ";
if (currentTitle.startsWith(prefix)) {
return currentTitle.slice(prefix.length);
}
return currentTitle;
}
function applyTitle(title) {
suppressObserver = true;
document.title = title;
const input = document.querySelector("#tab-title-panel input");
if (input && input.value !== title) {
input.value = title;
}
// 下一轮微任务后恢复监听
queueMicrotask(() => {
suppressObserver = false;
});
}
function startTitleObserver() {
const titleEl = document.querySelector("title");
if (!titleEl || titleObserver) return;
titleObserver = new MutationObserver(() => {
if (suppressObserver) return;
const current = document.title;
const expected = computeExpectedTitle(current);
if (expected !== current) {
applyTitle(expected);
} else {
// 同步 UI(例如 ChatGPT 切换会话)
const input = document.querySelector("#tab-title-panel input");
if (input && input.value !== current) {
input.value = current;
}
}
});
titleObserver.observe(titleEl, {
childList: true,
subtree: true,
characterData: true,
});
const current = document.title;
const expected = computeExpectedTitle(current);
if (expected !== current) {
applyTitle(expected);
}
}
function startAfterLoad() {
const isChatGPT = location.host === "chatgpt.com";
if (isChatGPT) {
// 仅 ChatGPT 页面等待 5s
setTimeout(initTabTitleModifier, 5000);
} else {
// 非 ChatGPT 页面直接执行
initTabTitleModifier();
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", startAfterLoad);
} else {
startAfterLoad();
}
})();