youtube跳过广告

当播放广告时直接将广告跳到最后

当前为 2024-10-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name youtube skip ad
  3. // @name:zh-CN youtube跳过广告
  4. // @name:zh-TW youtube跳過廣告
  5. // @name:ja youtube広告をスキップする
  6. // @description if play ad then jump to ad video end
  7. // @description:zh-TW 當播放廣告時直接將廣告跳到最後
  8. // @description:zh-CN 当播放广告时直接将广告跳到最后
  9. // @description:ja 広告の再生中に最後に直接ジャンプします
  10. // @namespace https://greasyfork.org/zh-TW/users/461233-jack850628
  11. // @version 1.0.241025
  12. // @author jack850628
  13. // @include https://*.youtube.com/*
  14. // @match https://*.youtube.com/*
  15. // @noframes
  16. // @run-at document-end
  17. // @license MIT
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. function skypeVideo(player){
  22. if(!player.dataset.adWatcher){
  23. player.dataset.adWatcher = true;
  24. player.addEventListener('loadeddata', function(e){
  25. setTimeout(function(){
  26. console.debug('影片來源更換了')
  27. for(let playerDiv of [document.querySelector('#player'), document.querySelector('#full-bleed-container')]){
  28. if(playerDiv?.querySelector('.html5-video-player')?.querySelectorAll('.ytp-ad-text, .ytp-ad-hover-text-button')?.length > 0){
  29. console.log('發現廣告!')
  30. player.currentTime = player.duration;
  31. setTimeout(function(){
  32. playerDiv.querySelector('.html5-video-player .ytp-ad-skip-button-modern').click();
  33. });
  34. }
  35. }
  36. }, 5);
  37. });
  38. }
  39. }
  40. function observerPlayerRoot(doc){
  41. let player = doc.querySelector('video');
  42. if(player){
  43. console.debug('找到播放器', player);
  44. skypeVideo(player);
  45. }
  46. let ycpObserver = new MutationObserver((mutationdeList, observer) => {
  47. mutationdeList.flatMap(i => [...i.addedNodes]).flat().forEach(doc => {
  48. if(doc.tagName){
  49. let player = null;
  50. if(doc.tagName == 'VIDEO'){
  51. player = doc;
  52. }else if(!["SCRIPT", "STYLE", "LINK", "MATE"].includes(doc.tagName)){
  53. player = doc.querySelector('video');
  54. }
  55. if(player){
  56. console.debug('找到播放器', player);
  57. skypeVideo(player);
  58. }
  59. }
  60. });
  61. });
  62. ycpObserver.observe(
  63. doc,
  64. {
  65. childList: true,
  66. subtree: true
  67. }
  68. );
  69. }
  70. let playerRoot = document.querySelector('#player');
  71. if(playerRoot){
  72. observerPlayerRoot(playerRoot);
  73. }else{
  74. let rootObserver = new MutationObserver((mutationdeList, observer) => {
  75. mutationdeList.flatMap(i => [...i.addedNodes]).flat().forEach(doc => {
  76. if (doc.tagName && !["SCRIPT", "STYLE", "LINK", "MATE"].includes(doc.tagName)){
  77. let playerRoot = doc.querySelector('#player');
  78. if(playerRoot){
  79. observerPlayerRoot(playerRoot);
  80. rootObserver.disconnect();
  81. }
  82. }
  83. });
  84. });
  85. rootObserver.observe(
  86. document,
  87. {
  88. childList: true,
  89. subtree: true
  90. }
  91. );
  92. }
  93. })();
  94.  
  95.