/ src / entrypoints / pause-media.content.ts
pause-media.content.ts
 1  // Content script to pause all media elements on a page
 2  // Only injected into known video/audio streaming sites to minimize performance impact
 3  // Additional sites can be configured in extension settings (Options > Pause Media Sites)
 4  // Note: Firefox doesn't support ports in match patterns, so sites with custom ports
 5  // will use the programmatic fallback injection instead
 6  export default defineContentScript({
 7    matches: [
 8      // YouTube
 9      '*://*.youtube.com/*',
10      '*://youtube.com/*',
11      // YouTube Music
12      '*://*.music.youtube.com/*',
13      // Invidious instances (public instances without custom ports)
14      '*://*.invidious.io/*',
15      '*://*.invidious.snopyta.org/*',
16      '*://yewtu.be/*',
17      '*://invidio.us/*',
18      '*://inv.riverside.rocks/*',
19      '*://invidious.kavin.rocks/*',
20      // Other common video/audio sites
21      '*://*.vimeo.com/*',
22      '*://*.spotify.com/*',
23      '*://*.soundcloud.com/*',
24      '*://*.bandcamp.com/*',
25      '*://*.pandora.com/*',
26    ],
27    runAt: 'document_idle',
28    main() {
29      // Listen for pause command from background script
30      browser.runtime.onMessage.addListener((message, _sender, sendResponse) => {
31        if (message.type === 'PAUSE_MEDIA') {
32          let pausedCount = 0;
33  
34          // Find and pause all video elements
35          const videos = document.querySelectorAll('video');
36          videos.forEach((video) => {
37            try {
38              if (!video.paused) {
39                video.pause();
40                pausedCount++;
41              }
42            } catch (e) {
43              // Ignore errors - some videos may be protected
44            }
45          });
46  
47          // Find and pause all audio elements
48          const audios = document.querySelectorAll('audio');
49          audios.forEach((audio) => {
50            try {
51              if (!audio.paused) {
52                audio.pause();
53                pausedCount++;
54              }
55            } catch (e) {
56              // Ignore errors
57            }
58          });
59  
60          // YouTube specific: simulate pressing 'k' key (play/pause shortcut)
61          if (window.location.hostname.includes('youtube.com')) {
62            const player = document.querySelector('#movie_player') || document.querySelector('.html5-video-player');
63            if (player) {
64              (player as HTMLElement).focus();
65              const keyEvent = new KeyboardEvent('keydown', {
66                key: 'k',
67                code: 'KeyK',
68                keyCode: 75,
69                which: 75,
70                bubbles: true,
71                cancelable: true
72              });
73              player.dispatchEvent(keyEvent);
74              pausedCount++;
75            }
76          }
77  
78          sendResponse({ paused: pausedCount });
79          return true;
80        }
81      });
82    }
83  });