-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitscreen-script.js
More file actions
170 lines (149 loc) · 6.1 KB
/
splitscreen-script.js
File metadata and controls
170 lines (149 loc) · 6.1 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
const liveUrl = params.get('live');
const archiveUrl = params.get('archive');
const liveFrame = document.getElementById('liveFrame');
const archiveFrame = document.getElementById('archiveFrame');
const splitScreen = document.getElementById('splitScreen');
const archiveBlocked = document.getElementById('archiveBlocked');
const liveBlocked = document.getElementById('liveBlocked');
if (!liveFrame || !archiveFrame || !splitScreen) {
console.error('Critical DOM elements missing.');
return;
}
if (!liveUrl || !archiveUrl) {
const errorDiv = document.createElement('div');
errorDiv.className = 'w-full text-center text-red-600 p-8';
errorDiv.textContent = 'Missing URL parameters.';
splitScreen.replaceChildren(errorDiv);
return;
}
// --- URL Validation ---
function isValidHttpUrl(urlString) {
try {
const url = new URL(urlString);
return url.protocol === 'https:' || url.protocol === 'http:';
} catch { return false; }
}
function isArchiveUrl(urlString) {
try {
const url = new URL(urlString);
return url.hostname === 'web.archive.org' || url.hostname === 'archive.org';
} catch { return false; }
}
// --- Strip iframe-blocking headers (fire-and-forget) ---
const RULE_ID = 1;
function setupHeaderRules() {
if (!chrome?.declarativeNetRequest?.updateSessionRules) {
console.warn('declarativeNetRequest not available');
return Promise.resolve();
}
return chrome.tabs.getCurrent().then(tab => {
if (!tab?.id) return;
return chrome.declarativeNetRequest.updateSessionRules({
removeRuleIds: [RULE_ID],
addRules: [{
id: RULE_ID,
priority: 1,
action: {
type: 'modifyHeaders',
responseHeaders: [
{ header: 'x-frame-options', operation: 'remove' },
{ header: 'content-security-policy', operation: 'remove' },
]
},
condition: {
tabIds: [tab.id],
resourceTypes: ['sub_frame']
}
}]
});
}).catch(err => console.warn('Header rule setup failed:', err));
}
function cleanupRules() {
if (!chrome?.declarativeNetRequest?.updateSessionRules) return;
chrome.declarativeNetRequest.updateSessionRules({ removeRuleIds: [RULE_ID] }).catch(() => {});
}
// --- Load iframes ---
function loadIframes() {
if (isArchiveUrl(archiveUrl)) {
archiveFrame.src = archiveUrl;
} else {
archiveBlocked.classList.remove('hidden');
archiveBlocked.querySelector('p').textContent = 'Invalid archive URL.';
}
if (isValidHttpUrl(liveUrl)) {
liveFrame.src = liveUrl;
} else {
liveBlocked.classList.remove('hidden');
liveBlocked.querySelector('p').textContent = 'Invalid live URL.';
}
}
// Set up rules first, then load iframes (with timeout fallback)
let iframesLoaded = false;
setupHeaderRules().then(() => {
if (!iframesLoaded) {
iframesLoaded = true;
loadIframes();
}
});
// Fallback: load iframes after 300ms even if rule setup hangs
setTimeout(() => {
if (!iframesLoaded) {
iframesLoaded = true;
loadIframes();
}
}, 300);
window.addEventListener('beforeunload', cleanupRules);
// --- Detect blocked iframes ---
function detectBlockedIframe(frame, panel) {
setTimeout(() => {
try { void frame.contentDocument; } catch { return; }
if (frame.contentDocument?.body?.innerText?.trim() === '' && !frame.contentDocument.title) {
panel.classList.remove('hidden');
}
}, 3000);
}
detectBlockedIframe(liveFrame, liveBlocked);
detectBlockedIframe(archiveFrame, archiveBlocked);
// --- UI Controls ---
const hideLiveBtn = document.getElementById('hideLive');
const hideArchiveBtn = document.getElementById('hideArchive');
let liveVisible = true, archiveVisible = true;
const setLayout = (layout) => {
const isHorizontal = layout === 'horizontal';
splitScreen.style.flexDirection = isHorizontal ? 'row' : 'column';
splitScreen.querySelectorAll('iframe').forEach(iframe => {
iframe.style.borderRight = isHorizontal ? '1px solid #ddd' : 'none';
iframe.style.borderBottom = !isHorizontal ? '1px solid #ddd' : 'none';
});
chrome.storage.local.set({ layoutPreference: layout });
};
chrome.storage.local.get('layoutPreference', ({ layoutPreference }) => {
setLayout(layoutPreference || 'horizontal');
});
document.getElementById('horizontalBtn')?.addEventListener('click', () => setLayout('horizontal'));
document.getElementById('verticalBtn')?.addEventListener('click', () => setLayout('vertical'));
hideLiveBtn?.addEventListener('click', () => {
liveVisible = !liveVisible;
liveFrame.style.display = liveVisible ? 'block' : 'none';
hideLiveBtn.textContent = liveVisible ? 'Toggle Live' : 'Show Live';
});
hideArchiveBtn?.addEventListener('click', () => {
archiveVisible = !archiveVisible;
archiveFrame.style.display = archiveVisible ? 'block' : 'none';
hideArchiveBtn.textContent = archiveVisible ? 'Toggle Archive' : 'Show Archive';
});
document.getElementById('openLive')?.addEventListener('click', () => {
if (isValidHttpUrl(liveUrl)) window.open(liveUrl, '_blank', 'noopener,noreferrer');
});
document.getElementById('openArchive')?.addEventListener('click', () => {
if (isArchiveUrl(archiveUrl)) window.open(archiveUrl, '_blank', 'noopener,noreferrer');
});
document.addEventListener('keydown', (e) => {
if (e.altKey && e.key === 'h') setLayout('horizontal');
if (e.altKey && e.key === 'v') setLayout('vertical');
if (e.altKey && e.key === 'l') hideLiveBtn?.click();
if (e.altKey && e.key === 'a') hideArchiveBtn?.click();
});
});