youtube跳过广告

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

当前为 2025-01-19 提交的版本,查看 最新版本

  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.250119
  12. // @author jack850628
  13. // @include https://*.youtube.com/*
  14. // @noframes
  15. // @run-at document-end
  16. // @license MIT
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. function skypeVideo(player){
  21. if(!player.dataset.adWatcher){
  22. player.dataset.adWatcher = true;
  23. player.addEventListener('loadeddata', function(e){
  24. setTimeout(function(){
  25. console.debug('影片來源更換了')
  26. for(let playerDiv of [document.querySelector('#player'), document.querySelector('#full-bleed-container')]){
  27. if(playerDiv?.querySelectorAll('.html5-video-player .ytp-ad-text, .html5-video-player .ad-simple-attributed-string')?.length > 0){
  28. console.log('發現廣告!')
  29. player.currentTime = player.duration;
  30. setTimeout(function(){
  31. playerDiv.querySelector('.html5-video-player .ytp-ad-skip-button-modern').click();
  32. });
  33. }
  34. }
  35. }, 5);
  36. });
  37. }
  38. }
  39. function observerPlayerRoot(doc){
  40. let player = doc.querySelector('video');
  41. if(player){
  42. console.debug('找到播放器', player);
  43. skypeVideo(player);
  44. }
  45. let ycpObserver = new MutationObserver((mutationdeList, observer) => {
  46. mutationdeList.flatMap(i => [...i.addedNodes]).flat().forEach(doc => {
  47. if(doc.tagName){
  48. let player = null;
  49. if(doc.tagName == 'VIDEO'){
  50. player = doc;
  51. }else if(!["SCRIPT", "STYLE", "LINK", "MATE"].includes(doc.tagName)){
  52. player = doc.querySelector('video');
  53. }
  54. if(player){
  55. console.debug('找到播放器', player);
  56. skypeVideo(player);
  57. }
  58. }
  59. });
  60. });
  61. ycpObserver.observe(
  62. doc,
  63. {
  64. childList: true,
  65. subtree: true
  66. }
  67. );
  68. }
  69. let playerRoot = document.querySelector('#player');
  70. if(playerRoot){
  71. observerPlayerRoot(playerRoot);
  72. }else{
  73. let rootObserver = new MutationObserver((mutationdeList, observer) => {
  74. mutationdeList.flatMap(i => [...i.addedNodes]).flat().forEach(doc => {
  75. if (doc.tagName && !["SCRIPT", "STYLE", "LINK", "MATE"].includes(doc.tagName)){
  76. let playerRoot = doc.querySelector('#player');
  77. if(playerRoot){
  78. observerPlayerRoot(playerRoot);
  79. rootObserver.disconnect();
  80. }
  81. }
  82. });
  83. });
  84. rootObserver.observe(
  85. document,
  86. {
  87. childList: true,
  88. subtree: true
  89. }
  90. );
  91. }
  92. })();
  93.  
  94.