| | |
| | |
| | |
| |
|
| | import { setting_VideoPlaybackOptions } from "./SettingsManager.js"; |
| |
|
| | |
| | |
| |
|
| | export class ImageAndVideoObserverOptions { |
| | playbackThreshold = 0.01; |
| | } |
| |
|
| | let observerOptions = new ImageAndVideoObserverOptions(); |
| |
|
| | const observedElements = new Set(); |
| |
|
| | async function tryPlayVideo(element) { |
| | if (element.paused) { |
| | try { |
| | await element.play(); |
| | element.currentTime = element.lastSeekTime; |
| | } catch (error) { |
| | console.error(error); |
| | } |
| | } |
| | } |
| |
|
| | function tryStopVideo(element) { |
| | if (element.readyState >= element.HAVE_ENOUGH_DATA && !element.paused) { |
| | try { |
| | element.pause(); |
| | } catch { } |
| | } |
| | } |
| |
|
| | export function observeVisualElement(element) { |
| | if (!element) { |
| | return; |
| | } |
| | |
| | if (!observedElements.has(element)) { |
| | |
| | observedElements.add(element); |
| | |
| | imageAndVideoObserver.observe(element); |
| | } |
| | } |
| |
|
| | export function unobserveVisualElement(element) { |
| | if (!element) { |
| | return; |
| | } |
| | observedElements.delete(element); |
| | imageAndVideoObserver.unobserve(element); |
| | } |
| |
|
| | const imageAndVideoObserver = new IntersectionObserver((entries) => { |
| | entries.forEach(async entry => { |
| |
|
| | const element = entry.target; |
| |
|
| | if (!element) { |
| | unobserveVisualElement(element); |
| | return; |
| | } |
| | |
| | if (entry.isIntersecting) { |
| | if (!element.src || element.src === '') { |
| | element.forceLoad(); |
| |
|
| | if (element.tagName !== 'VIDEO') { |
| | unobserveVisualElement(element); |
| | return; |
| | } |
| | } |
| |
|
| | if (setting_VideoPlaybackOptions.value.autoplay) { |
| | tryPlayVideo(element); |
| | } |
| | } else { |
| | if (element.tagName === 'VIDEO') { |
| | |
| | tryStopVideo(element); |
| |
|
| | if (element.currentTime) { |
| | element.lastSeekTime = element.currentTime; |
| | } |
| |
|
| | if ('src' in element) { |
| | element.removeAttribute('src'); |
| | try { |
| | if (element.load) { element.load(); } |
| | } catch { } |
| | } |
| | } |
| | } |
| | }); |
| | }, { threshold: observerOptions.playbackThreshold }); |