-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
246 lines (199 loc) · 7.37 KB
/
Copy pathcontent.js
File metadata and controls
246 lines (199 loc) · 7.37 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'use strict';
(function () {
// WeakSet is opaque to the page — unlike data-attributes, the page cannot
// read or manipulate it to bypass deduplication.
const processed = new WeakSet();
// --- Style helpers ---
function styleButton(link) {
Object.assign(link.style, {
display: 'inline-block',
background: '#1A73E8',
color: '#fff',
borderRadius: '4px',
padding: '6px 12px',
fontSize: '13px',
fontWeight: '500',
textDecoration: 'none',
border: 'none',
cursor: 'pointer',
fontFamily: 'sans-serif',
position: 'absolute',
top: '8px',
right: '8px',
zIndex: '9999',
});
}
function styleWarning(el) {
Object.assign(el.style, {
display: 'block',
background: '#FFF3CD',
color: '#856404',
padding: '8px 12px',
borderRadius: '4px',
fontSize: '13px',
fontFamily: 'sans-serif',
position: 'absolute',
top: '8px',
right: '8px',
zIndex: '9999',
});
}
// --- Vimeo oEmbed accessibility check ---
// Fallback-permissive: network errors / timeouts assume accessible.
// Cache<videoId, Promise<boolean>> shared across concurrent iframe matches.
const vimeoCache = new Map();
function isVimeoAccessible(videoId) {
if (vimeoCache.has(videoId)) return vimeoCache.get(videoId);
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 3000);
const promise = fetch(
`https://vimeo.com/api/oembed.json?url=https%3A%2F%2Fvimeo.com%2F${videoId}`,
{ signal: controller.signal }
)
.then(res => res.ok)
.catch(() => true) // timeout or CORS error -> assume accessible
.finally(() => clearTimeout(timer));
vimeoCache.set(videoId, promise);
return promise;
}
// --- Core injection ---
function injectLink(referenceElement, videoUrl) {
if (!videoUrl.startsWith('https://')) return;
const parent = referenceElement.parentNode;
if (!parent) return;
// Wrapper carries position:relative so the absolute button stays inside.
const wrapper = document.createElement('div');
Object.assign(wrapper.style, { position: 'relative' });
const link = document.createElement('a');
link.textContent = '⬇ Telecharger la video';
link.setAttribute('href', videoUrl);
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
styleButton(link);
link.addEventListener('mouseenter', () => { link.style.opacity = '0.85'; });
link.addEventListener('mouseleave', () => { link.style.opacity = '1'; });
wrapper.appendChild(link);
parent.insertBefore(wrapper, referenceElement);
}
function injectWarning(referenceElement) {
const parent = referenceElement.parentNode;
if (!parent) return;
const wrapper = document.createElement('div');
Object.assign(wrapper.style, { position: 'relative' });
const msg = document.createElement('span');
msg.textContent = '⚠ Video protegee - acces direct impossible';
styleWarning(msg);
wrapper.appendChild(msg);
parent.insertBefore(wrapper, referenceElement);
}
// --- Element processors ---
function processIframes(root) {
root.querySelectorAll('iframe[src*="player.vimeo.com/video/"]').forEach(iframe => {
if (processed.has(iframe)) return;
const src = iframe.getAttribute('src') || '';
// URL() is the single validation mechanism — extracts and validates
// origin + path in one pass without a secondary regex.
let videoId;
try {
const parsed = new URL(src);
if (parsed.origin !== 'https://player.vimeo.com') return;
const parts = parsed.pathname.split('/').filter(Boolean);
if (parts[0] !== 'video' || !/^\d+$/.test(parts[1])) return;
videoId = parts[1];
} catch (_) {
return;
}
const videoUrl = 'https://vimeo.com/' + videoId;
// Mark immediately to prevent duplicate async launches on the same node
// during React mutation bursts.
processed.add(iframe);
isVimeoAccessible(videoId).then(accessible => {
// Skip injection if React unmounted the node during the async check.
if (!document.contains(iframe)) return;
if (accessible) {
injectLink(iframe, videoUrl);
} else {
injectWarning(iframe);
}
});
});
}
function processVideos(root) {
root.querySelectorAll('video[src]').forEach(video => {
if (processed.has(video)) return;
processed.add(video);
const src = video.getAttribute('src') || '';
if (!src.startsWith('https://')) return;
if (!document.contains(video)) return;
injectLink(video, src);
});
}
function processAll(root) {
processIframes(root);
processVideos(root);
}
// --- Two-pass MutationObserver strategy ---
// Pass 1 (rootObserver): watches document.body until the course container appears,
// then disconnects to avoid observing the entire page indefinitely.
// Pass 2 (containerObserver): watches only the course subtree for video mutations.
let containerObserver = null;
let rafId = null;
function watchContainer(container) {
if (containerObserver) {
containerObserver.disconnect();
containerObserver = null;
}
// Cancel any rAF scheduled by the previous container observer to prevent
// it from running processAll on a stale container after reconnect.
if (rafId !== null) {
cancelAnimationFrame(rafId);
rafId = null;
}
processAll(container);
containerObserver = new MutationObserver(() => {
if (rafId !== null) return;
rafId = requestAnimationFrame(() => {
processAll(container);
rafId = null;
});
});
containerObserver.observe(container, { childList: true, subtree: true });
}
const rootObserver = new MutationObserver(() => {
const container = document.querySelector('main, [class*="course-"], article');
if (!container) return;
rootObserver.disconnect();
watchContainer(container);
});
const initialContainer = document.querySelector('main, [class*="course-"], article');
if (initialContainer) {
watchContainer(initialContainer);
} else {
processAll(document.body);
rootObserver.observe(document.body, { childList: true, subtree: true });
}
// --- SPA navigation detection ---
// Watches document.head for React Router title updates (childList only, no subtree)
// to avoid firing on every internal DOM mutation React makes in the body.
// popstate covers browser back/forward without patching history.pushState.
let lastHref = location.href;
new MutationObserver(() => {
if (location.href !== lastHref) {
lastHref = location.href;
onNavigation();
}
}).observe(document.head, { childList: true });
window.addEventListener('popstate', onNavigation);
function onNavigation() {
// Delay lets React hydrate the new route's video elements before scanning.
setTimeout(() => {
rootObserver.disconnect();
const c = document.querySelector('main, [class*="course-"], article');
if (c) {
watchContainer(c);
} else {
rootObserver.observe(document.body, { childList: true, subtree: true });
}
}, 300);
}
})();