feat: add disableMediaControls function to prevent media key actions

This commit is contained in:
2026-05-30 00:06:16 +02:00
parent dc06b53089
commit 67bc968497
2 changed files with 39 additions and 1 deletions
+2 -1
View File
@@ -1,5 +1,5 @@
import { active_custom_theme, custom_themes } from './global';
import { setGlobalVolume } from './utils';
import { disableMediaControls, setGlobalVolume } from './utils';
import { betterFetch } from '@better-fetch/fetch';
import { convertFileSrc } from '@tauri-apps/api/core';
import { invoke } from '@tauri-apps/api/core';
@@ -268,6 +268,7 @@ export const loadTheme = async (theme: Theme, themeContainer: HTMLElement, volum
themeScript.mountTheme(themeContainer, { assets: theme.assets });
}
setGlobalVolume(volume);
disableMediaControls();
};
export const deleteTheme = async (themeToUninstall: Theme) => {
+37
View File
@@ -203,6 +203,43 @@ export function trackMediaContainer(node: HTMLElement) {
};
}
const MEDIA_ACTIONS: MediaSessionAction[] = [
'play',
'pause',
'stop',
'previoustrack',
'nexttrack',
'seekbackward',
'seekforward',
];
export function disableMediaControls() {
const mediaElements: NodeListOf<HTMLMediaElement> = document.querySelectorAll('audio, video');
if ('mediaSession' in navigator) {
MEDIA_ACTIONS.forEach((action) => {
try {
navigator.mediaSession.setActionHandler(action, () => {});
} catch {}
});
}
document.addEventListener(
'keydown',
(e: KeyboardEvent) => {
if (e.key.startsWith('Media')) {
e.preventDefault();
e.stopPropagation();
}
},
true
);
mediaElements.forEach((el) => {
el.disableRemotePlayback = true;
});
}
export function calculateGitBlobSha(fileBuffer: Buffer): string {
const header = Buffer.from(`blob ${fileBuffer.length}\0`);
const store = Buffer.concat([header, fileBuffer]);