在使用 Enhancer for YouTube 插件时有一个功能 “自动暂停在后台标签中打开的视频” 特别好用,在国内的环境下,多少还是要用一下 BilBili 的,所以想着自己来实现这个功能。
对还不懂写代码的我来说,去单独写一个插件难度太大了,所以就选择去叫 GPT 写一个油猴脚本吧。
就得到了
0.5.2版本的代码不足之处:非焦点窗口加载视频时会让焦点窗口的视频停止。
叫GPT改了之后(v0.6)的新BUG:同一窗口的视频又不会自动暂停了😭。
怎么改都不对,就放弃折腾,v0.5.2已经够我用
v0.5.2的代码如下:
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 78 79 80 81 82 83 84 85 86
|
(function() { 'use strict';
let video = null; let channel = null; const CHANNEL_NAME = 'bilibili-video-control'; const TAB_ID = Date.now() + Math.random(); function init() { video = document.querySelector('video'); if (!video) return; channel = new BroadcastChannel(CHANNEL_NAME); video.addEventListener('play', onPlay); channel.addEventListener('message', onMessage); } function onPlay() { if (document.hidden) { video.pause(); return; } if (channel) { channel.postMessage({ action: 'pauseOthers', sender: TAB_ID }); } } function onMessage(event) { const data = event.data; if (!data || data.action !== 'pauseOthers') return; if (data.sender !== TAB_ID && video && !video.paused) { video.pause(); } } function mutationCallback(mutationsList) { for (let mutation of mutationsList) { if (mutation.type === 'childList') { const newVideo = document.querySelector('video'); if (newVideo && newVideo !== video) { if (video) { video.removeEventListener('play', onPlay); } video = newVideo; video.addEventListener('play', onPlay); } } } } const observer = new MutationObserver(mutationCallback); observer.observe(document.body, { childList: true, subtree: true }); init(); window.addEventListener('beforeunload', () => { if (channel) { channel.close(); } if (video) { video.removeEventListener('play', onPlay); } observer.disconnect(); }); })();
|
另附上我的v0.6.1的代码,如下:
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
(function() { 'use strict';
let video = null; let channel = null; const CHANNEL_NAME = 'bilibili-video-control'; const TAB_ID = Date.now() + Math.random();
function init() { video = document.querySelector('video'); if (!video) { return; }
channel = new BroadcastChannel(CHANNEL_NAME); video.addEventListener('play', onPlay); channel.addEventListener('message', onMessage); document.addEventListener('visibilitychange', onVisibilityChange); }
function onPlay() { if (document.visibilityState !== 'visible' || !document.hasFocus()) { video.pause(); return; }
if (channel) { channel.postMessage({ action: 'pauseOthers', sender: TAB_ID }); } }
function onMessage(event) { const data = event.data; if (!data || !data.action) return;
if (data.action === 'pauseOthers') { handlePauseOthers(data.sender); } }
function handlePauseOthers(senderId) { if (senderId === TAB_ID) return;
if (document.visibilityState === 'visible' && video && !video.paused) { video.pause(); } }
function onVisibilityChange() { if (document.visibilityState === 'visible' && video && !video.paused) { if (channel) { channel.postMessage({ action: 'pauseOthers', sender: TAB_ID }); } } }
function mutationCallback(mutationsList) { for (let mutation of mutationsList) { if (mutation.type === 'childList') { const newVideo = document.querySelector('video'); if (newVideo && newVideo !== video) { if (video) { video.removeEventListener('play', onPlay); } video = newVideo; video.addEventListener('play', onPlay); } } } }
const observer = new MutationObserver(mutationCallback); observer.observe(document.body, { childList: true, subtree: true });
init();
window.addEventListener('beforeunload', () => { if (channel) { channel.close(); } if (video) { video.removeEventListener('play', onPlay); } document.removeEventListener('visibilitychange', onVisibilityChange); observer.disconnect(); }); })();
|