Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
95 lines (84 loc) · 2.84 KB
/
Copy pathcontent.js
File metadata and controls
95 lines (84 loc) · 2.84 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
let originalTitle = document.title;
let observer = null;
let isCustomTitle = false;
// Listen for messages from popup or background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'renameTab') {
// Store original title if this is first override
if (!isCustomTitle) {
originalTitle = document.title;
isCustomTitle = true;
}
// Force update the title
document.title = message.name;
startObserving();
// Some websites aggressively reset the title, so we'll reapply after short delays
setTimeout(() => document.title = message.name, 50);
setTimeout(() => document.title = message.name, 250);
setTimeout(() => document.title = message.name, 1000);
sendResponse({ success: true });
} else if (message.action === 'getOriginalTitle') {
sendResponse({ title: originalTitle });
} else if (message.action === 'resetTitle') {
document.title = originalTitle;
isCustomTitle = false;
stopObserving();
sendResponse({ success: true });
}
return true;
});
// Start observing title changes
function startObserving() {
if (observer) {
observer.disconnect();
}
observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
// If the page is trying to update the title and we have a custom title set
if ((mutation.type === 'childList' || mutation.type === 'characterData') &&
isCustomTitle && document.title !== originalTitle) {
// Don't update if it's our own change
const currentTitle = document.querySelector('title')?.textContent;
if (currentTitle && currentTitle !== document.title) {
originalTitle = currentTitle;
}
}
}
});
// Observe title element changes
const titleElement = document.querySelector('title');
if (titleElement) {
observer.observe(titleElement, {
characterData: true,
childList: true,
subtree: true,
characterDataOldValue: true
});
// Also observe head for title replacements
const headElement = document.querySelector('head');
if (headElement) {
observer.observe(headElement, {
childList: true
});
}
}
}
// Stop observing title changes
function stopObserving() {
if (observer) {
observer.disconnect();
observer = null;
}
}
// Check if this URL has a saved name
chrome.runtime.sendMessage({ action: 'checkSavedUrl', url: window.location.href });
// Add a listener for page visibility changes to reapply custom title when tab is focused
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible' && isCustomTitle) {
chrome.runtime.sendMessage({ action: 'getTabName' }, (response) => {
if (response && response.name) {
document.title = response.name;
}
});
}
});