From 7babe2cd1f4cb2656b556856d27dc7e6c64203c6 Mon Sep 17 00:00:00 2001 From: larsjohnsen Date: Sat, 4 May 2024 19:28:11 +0200 Subject: [PATCH] showImages: Fix audio playing in the background (#5521) --- lib/modules/showImages.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/modules/showImages.js b/lib/modules/showImages.js index b860e00d91..ae99e561b4 100644 --- a/lib/modules/showImages.js +++ b/lib/modules/showImages.js @@ -18,12 +18,14 @@ import type { GenericMedia, } from '../core/host'; import { Host } from '../core/host'; +import { loadOptions } from '../core/init'; import { Module } from '../core/module'; import { positiveModulo, downcast, filterMap, Thing, + PagePhases, SelectedThing, addCSS, batch, @@ -36,6 +38,7 @@ import { frameThrottle, isPageType, isAppType, + stopPageContextScript, string, waitForEvent, watchForElements, @@ -54,6 +57,7 @@ import { Permissions, Storage, } from '../environment'; +import * as Modules from '../core/modules'; import * as Options from '../core/options'; import * as __hosts from './hosts'; import * as Notifications from './notifications'; @@ -74,6 +78,7 @@ import { expandos, activeExpandos, } from './showImages/expando'; +import vreddit from './hosts/vreddit'; const siteModules: Map> = new Map( Object.values(__hosts).map(host => [host.moduleID, downcast(host, Host)]), // ensure that all hosts are instances of `Host` @@ -452,6 +457,31 @@ module.options = { }, {}), }; +const localStorageKeyRemoveNativePlayer = 'RES_forceReplaceNativeExpando'; +// $FlowIgnore +export const cachedRemoveNativePlayer = () => localStorage?.getItem(localStorageKeyRemoveNativePlayer) === 'true'; + +module.onInit = () => { + if (isAppType('r2')) { + // Reddit loads scripts which initializes the video player, which will cause a slowdown if not blocked + // It may also start playing the video, even if replace the expando + const cachedValue = cachedRemoveNativePlayer() + if (cachedValue) { + console.log('Removing Reddit\'s native video player'); + stopPageContextScript(script => (/^\/?videoplayer\./).test(new URL(script.src, location.origin).pathname), 'head', true); + stopPageContextScript(script => !!script.innerHTML.match('RedditVideoPlayer'), PagePhases.contentStart.then(() => document.querySelector('#siteTable')), false); + } + + loadOptions.then(() => { + const actualValue = Modules.isRunning(module) && isSiteModuleEnabled(vreddit) && vreddit.options && vreddit.options.forceReplaceNativeExpando.value; + if (actualValue !== cachedValue) { + console.warn('The localStorage value for site module `forceReplaceNativeExpando` was outdated. The video player may not work.'); + localStorage.setItem(localStorageKeyRemoveNativePlayer, String(actualValue)); + } + }); + } +}; + module.exclude = [ /^\/ads\/[\-\w\._\?=]*/i, 'submit',