Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (72 loc) · 2.67 KB
/
Copy pathscript.js
File metadata and controls
77 lines (72 loc) · 2.67 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
const copyButton = document.querySelector('#copy-citation');
copyButton.addEventListener('click', async () => {
const citation = document.querySelector('#bibtex').textContent;
const status = document.querySelector('#copy-status');
try {
await navigator.clipboard.writeText(citation);
copyButton.textContent = 'Copied!';
status.textContent = 'Citation copied to clipboard.';
setTimeout(() => { copyButton.textContent = 'Copy citation'; }, 2200);
} catch {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(document.querySelector('#bibtex'));
selection.removeAllRanges();
selection.addRange(range);
status.textContent = 'Citation selected. Use your browser’s copy command.';
copyButton.textContent = 'Selected — copy manually';
}
});
const teaser = document.querySelector('.teaser video');
const soundToggle = document.querySelector('.sound-toggle');
if (teaser) {
// The button shows the current state, like a video player's speaker icon.
const showSound = () => {
soundToggle.querySelector('span').textContent = teaser.muted ? 'Sound off' : 'Sound on';
soundToggle.title = teaser.muted ? 'Turn sound on' : 'Turn sound off';
soundToggle.setAttribute('aria-pressed', String(!teaser.muted));
};
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
// Reduced motion: no autoplay; the still frame and the player's own controls instead.
teaser.muted = false;
teaser.controls = true;
soundToggle.hidden = true;
} else {
let started = false;
let mutedByVisitor = false;
// Play from the start, with sound unless the visitor turned it off. Browsers refuse
// sound before the visitor has clicked or tapped on the page; then play muted instead.
const play = async () => {
if (!started) {
teaser.currentTime = 0;
started = true;
}
teaser.muted = mutedByVisitor;
try {
await teaser.play();
} catch (error) {
if (error.name !== 'NotAllowedError') return;
teaser.muted = true;
await teaser.play().catch(() => {});
}
showSound();
};
// Play only while at least half of the video is on screen.
new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) play();
else teaser.pause();
}, { threshold: 0.5 }).observe(teaser);
soundToggle.addEventListener('click', () => {
if (teaser.muted) {
mutedByVisitor = false;
teaser.muted = false;
teaser.currentTime = 0;
teaser.play();
} else {
mutedByVisitor = true;
teaser.muted = true;
}
showSound();
});
}
}