youtube继续播放

当出现"影片已暂停,要继续观赏吗?"时忽略它继续播放

目前为 2021-05-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name youtube continue play
  3. // @name:zh-CN youtube继续播放
  4. // @name:zh-TW youtube繼續播放
  5. // @name:ja youtube再生自動継続
  6. // @description When "Video paused, do you want to continue watching?" Appears, ignore it and continue playing automatically
  7. // @description:zh-TW 當出現"影片已暫停,要繼續觀賞嗎?"時忽略它繼續播放
  8. // @description:zh-CN 当出现"影片已暂停,要继续观赏吗?"时忽略它继续播放
  9. // @description:ja 「動画が一時停止されました。続きを視聴しますか?」が表示されても無視して再生を続けます
  10. // @namespace https://greasyfork.org/zh-TW/users/461233-jack850628
  11. // @version 1.20.0520
  12. // @author jack850628
  13. // @include https://*.youtube.com/*
  14. // @noframes
  15. // @run-at document-end
  16. // @license MIT
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. let pausedFun = function({target: videoPlayer}){
  21. console.debug('暫停播放');
  22. setTimeout(function(){
  23. let ytConfirmDialog = document.querySelector('yt-confirm-dialog-renderer') || document.querySelector('dialog');
  24. if(
  25. ytConfirmDialog &&
  26. (
  27. ytConfirmDialog.parentElement.style.display != 'none' ||
  28. (
  29. document.hidden &&
  30. videoPlayer.currentTime < videoPlayer.duration//防止重複播放
  31. )//當網頁不可見時,DOM元件不會即時渲染,所以對話方塊的display還會是none
  32. )
  33. ){
  34. console.debug('被暫停了,但是我要繼續播放');
  35. //ytConfirmDialog.querySelector('yt-button-renderer[dialog-confirm]').click();//當網頁不可見時,觸發click是不會繼續播放的,因為要等到網頁可見時觸發UI渲染後才會把對話方塊關掉,對話方塊關掉後才會出發video的play事件
  36. videoPlayer.play();
  37. console.debug('按下"是"');
  38. }else console.debug('對話方塊找不到或是隱藏了', ytConfirmDialog && ytConfirmDialog.parentElement, document.hidden, videoPlayer.currentTime, videoPlayer.duration);
  39. }, 500);//確保在暫停時對話方塊一定找得到
  40. }
  41. let listenerVideoPlayer = function(doc){
  42. let pathname = new URL(location.href).pathname;
  43. let videoPlayer = doc.querySelector('video');
  44.  
  45. console.debug(pathname, pathname.startsWith('/watch'))
  46. if(!videoPlayer || !pathname.startsWith('/watch')){
  47. console.debug('找不到播放器');
  48. return false;
  49. }
  50. videoPlayer.addEventListener('pause', pausedFun);
  51. console.debug('找到播放器,開始監聽', videoPlayer);
  52. return true;
  53. }
  54. let ycpObserver = new MutationObserver(([{target: doc}], observer) => {
  55. console.debug('頁面更動', ycpObserver);
  56. if(listenerVideoPlayer(doc)) ycpObserver.disconnect();
  57. });
  58. ycpObserver.observe(
  59. document,
  60. {
  61. childList: true,
  62. subtree: true
  63. }
  64. );
  65. })();