-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
129 lines (107 loc) · 3.57 KB
/
content.js
File metadata and controls
129 lines (107 loc) · 3.57 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
(function () {
const STORAGE_KEY = 'webgation_settings';
function isContextValid() {
return !!chrome.runtime?.id;
}
async function loadSettings() {
if (!isContextValid()) return {};
try {
const data = await chrome.storage.local.get(STORAGE_KEY);
return data[STORAGE_KEY] || {};
} catch (e) {
return {};
}
}
function getSiteKey() {
try {
return location.origin;
} catch (e) {
return 'unknown';
}
}
function injectModeScript(mode) {
if (!isContextValid()) return;
const file = mode === 'floating' ? 'floating.js' : 'static.js';
const url = chrome.runtime.getURL(file);
removeInjectedUI();
const s = document.createElement('script');
s.src = url;
s.id = 'webgation-injected-script';
(document.head || document.documentElement).appendChild(s);
}
function removeInjectedUI() {
const existingScript = document.getElementById('webgation-injected-script');
if (existingScript) existingScript.remove();
window.postMessage({ __webgation: true, type: 'destroyUI' }, '*');
}
function reportNavigation() {
if (!isContextValid()) return;
if (window.navigation && window.navigation.currentEntry) {
const entry = window.navigation.currentEntry;
chrome.runtime.sendMessage({
type: 'report_nav',
payload: {
url: location.href,
title: document.title,
key: entry.key || entry.id
}
}).catch(() => { });
}
}
if (window.navigation) {
window.navigation.addEventListener('currententrychange', () => {
setTimeout(reportNavigation, 50);
});
}
window.addEventListener('message', async (ev) => {
if (!isContextValid() || !ev.data || ev.source !== window || !ev.data.__webgation) return;
const msg = ev.data;
const siteKey = getSiteKey();
try {
if (msg.type === 'getSettings') {
const settings = await loadSettings();
const siteSettings = settings[siteKey] || { enabled: true, mode: 'static', position: null };
window.postMessage({ __webgation: true, type: 'receiveSettings', payload: siteSettings }, '*');
}
else if (msg.type === 'savePosition') {
const settings = await loadSettings();
settings[siteKey] = settings[siteKey] || { enabled: true, mode: 'static' };
settings[siteKey].position = msg.payload;
await chrome.storage.local.set({ [STORAGE_KEY]: settings });
}
else if (msg.type === 'openHome') {
chrome.runtime.sendMessage({ type: 'open_new_tab' });
}
else if (msg.type === 'getHistory') {
reportNavigation();
chrome.runtime.sendMessage({ type: 'get_tab_history' }, (response) => {
window.postMessage({ __webgation: true, type: 'receiveHistory', payload: response }, '*');
});
}
} catch (e) {
console.log("webgation: Connection lost (extension reloaded). Please refresh the page.");
}
});
if (isContextValid()) {
chrome.runtime.onMessage.addListener((message) => {
if (isContextValid() && message.type === 'settingsUpdated') {
init();
}
});
}
async function init() {
if (!isContextValid()) return;
reportNavigation();
const settings = await loadSettings();
const siteSettings = settings[getSiteKey()] || { enabled: true, mode: 'static' };
if (siteSettings.enabled) {
injectModeScript(siteSettings.mode);
} else {
removeInjectedUI();
}
}
window.addEventListener('pageshow', () => {
reportNavigation();
});
init();
})();