- SignalDesk1小时前
油猴脚本,在其他页面浏览可以控制抖音页面 按 Ctrl + S 保存油猴脚本。 分别在 【抖音标签页】 和 【查资料标签页】 按 F5 刷新。 测试完整流程 : 在查资料页按住 Alt + 滚轮向下 切换到下一个视频。 按住 Alt + 点击鼠标左键 暂停视频。 再次按住 Alt + 点击鼠标左键 :这次即使在后台,也能秒级恢复正常播放,来回暂停播放多少次都毫无问题! // ==UserScript== // @name 跨标签控制抖音(精准追踪与后台起播终极版) // http://tampermonkey.net/ // 8.0 // @description 利用原生进度监听锁定当前视频,通过Smart Unmute突破后台播放限制 // :/// // GM_setValue // GM_addValueChangeListener // @run-at document-start // ==/UserScript== (function() { 'use strict'; const isDouyin = location.hostname.includes('douyin.com'); // ================= 1. 接收端:抖音标签页 ================= if (isDouyin) { console.log('[抖音助手] 接收端已就绪,正在精确追踪当前视频...'); // 全局动态变量:永远精准锁定当前正在观看的视频对象 let currentActiveVideo = null; // 利用底层事件:哪个视频有进度在走、在播放,它就是当前视频!绝不会找错预加载视频 document.addEventListener('timeupdate', (e) => { if (e.target && e.target.tagName === 'VIDEO' && !e.target.paused) { currentActiveVideo = e.target; } }, true); document.addEventListener('play', (e) => { if (e.target && e.target.tagName === 'VIDEO') { currentActiveVideo = e.target; } }, true); // 获取当前被锁定的目标视频 function getTargetVideo() { if (currentActiveVideo && document.body.contains(currentActiveVideo)) { return currentActiveVideo; } const videos = Array.from(document.querySelectorAll('video')); // 兜底找进度大于0的 return videos.find(v => v.currentTime > 0) || videos[0]; } // 停止播放 (Pause) function execPause() { console.log('[抖音助手] 执行【暂停】'); const target = getTargetVideo(); if (target) target.pause(); // 页面所有视频统统执行暂停,确保完全停下 document.querySelectorAll('video').forEach(v => v.pause()); } // 恢复播放 (Play) - 攻克后台拦截的核心逻辑 function execPlay() { console.log('[抖音助手] 执行【恢复播放】'); const target = getTargetVideo(); if (!target) return; // 1. 尝试直接播放 const playPromise = target.play(); if (playPromise !== undefined) { playPromise.then(() => { console.log('[抖音助手] 播放成功!'); }).catch(err => { // 2. 如果被 Chrome 后台策略拦截,启动 Smart Unmute 机制 console.warn('[抖音助手] 触发浏览器后台拦截,启用静音起播解禁法:', err); target.muted = true; // 静音状态下浏览器允许后台 100% 播放 target.play().then(() => { target.muted = false; // 起播成功后立刻打开声音 console.log('[抖音助手] 已解除限制并恢复声音!'); }).catch(e => console.error(e)); }); } // 3. 同步点击西瓜播放器界面上的大播放按钮(同步UI) const startBtn = document.querySelector('.xgplayer-start, [data-e2e="video-player-play"]'); if (startBtn) { startBtn.click(); } // 4. 同步调用实例 API const p = target.__player || (target.parentElement && target.parentElement.__player); if (p && typeof p.play === 'function') { try { p.play(); } catch (e) {} } } // 切换暂停与播放 function execToggle() { const target = getTargetVideo(); // 如果目标视频存在且正在播放,则暂停;否则播放 if (target && !target.paused) { execPause(); } else { execPlay(); } } // 切换视频逻辑(已稳定验证) function triggerSwitch(dir) { const nextSelector = '.xgplayer-playswitch-next, [data-e2e="video-switch-next"], .switch-next, button[aria-label="下一个"]'; const nextBtn = document.querySelector(nextSelector); if (dir === 'next') { if (nextBtn) { nextBtn.click(); return; } } else if (dir === 'prev') { let prevBtn = null; if (nextBtn) { prevBtn = nextBtn.previousElementSibling || Array.from(nextBtn.parentElement.children).find(el => el !== nextBtn); } if (!prevBtn) { prevBtn = document.querySelector('.xgplayer-playswitch-prev, [data-e2e="video-switch-prev"], .switch-prev'); } if (prevBtn) { prevBtn.click(); return; } } const keyName = dir === 'next' ? 'ArrowDown' : 'ArrowUp'; const keyCode = dir === 'next' ? 40 : 38; document.dispatchEvent(new KeyboardEvent('keydown', { key: keyName, code: keyName, keyCode: keyCode, bubbles: true })); } // 监听跨标签指令 GM_addValueChangeListener('douyin_remote_cmd', (name, oldValue, newValue, remote) => { if (remote && newValue && newValue.action) { if (newValue.action === 'next' || newValue.action === 'prev') { triggerSwitch(newValue.action); } else if (newValue.action === 'toggle_play') { execToggle(); } } }); } // ================= 2. 控制端:查资料页面 ================= else { let lastActionTime = 0; function sendCommand(actionName) { const now = Date.now(); if (now - lastActionTime > 250) { lastActionTime = now; GM_setValue('douyin_remote_cmd', { action: actionName, time: now }); console.log('[抖音助手] 已发送指令:', actionName); } } // 1. 滚轮切视频 (Alt + 滚轮) window.addEventListener('wheel', (e) => { if (e.altKey) { e.preventDefault(); sendCommand(e.deltaY > 0 ? 'next' : 'prev'); } }, { passive: false }); // 2. 鼠标左键切换开始/停止 (Alt + 鼠标左键) window.addEventListener('click', (e) => { if (e.altKey && e.button === 0) { e.preventDefault(); e.stopPropagation(); sendCommand('toggle_play'); } }, true); // 3. 鼠标滚轮中键切换开始/停止 (Alt + 滚轮中键) window.addEventListener('auxclick', (e) => { if (e.altKey && e.button === 1) { e.preventDefault(); e.stopPropagation(); sendCommand('toggle_play'); } }, true); // 4. 空格键切换开始/停止 (Alt + 空格) window.addEventListener('keydown', (e) => { if (e.altKey && e.code === 'Space') { e.preventDefault(); sendCommand('toggle_play'); } }, true); } })(); 1 个帖子 - 1 位参与者 阅读完整话题
- 情报分类:技术学习与提效
- 分类依据:分享油猴脚本实现跨标签控制抖音,属技术提效工具
- 信息来源:服务器 / LINUX DO - 最新话题
- 发布时间:2026/9/21 17:27:07
- 暂无回复