Skip sponsors - using Sponsorblock API on m.youtube.com (for Kiwi browser and others)

Skip sponsors/SelfPromo, adds a highlight.

  1. // ==UserScript==
  2. // @name Skip sponsors - using Sponsorblock API on m.youtube.com (for Kiwi browser and others)
  3. // @namespace http://your-namespace.com
  4. // @version 2.17
  5. // @description Skip sponsors/SelfPromo, adds a highlight.
  6. // @author Sp0kz
  7. // @match https://m.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant none
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. //sha256 library
  16. var sha256=function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math.pow,g=f(2,32),h="length",i="",j=[],k=8*b[h],l=a.h=a.h||[],m=a.k=a.k||[],n=m[h],o={},p=2;64>n;p++)if(!o[p]){for(d=0;313>d;d+=p)o[d]=p;l[n]=f(p,.5)*g|0,m[n++]=f(p,1/3)*g|0}for(b+="\x80";b[h]%64-56;)b+="\x00";for(d=0;d<b[h];d++){if(e=b.charCodeAt(d),e>>8)return;j[d>>2]|=e<<(3-d)%4*8}for(j[j[h]]=k/g|0,j[j[h]]=k,e=0;e<j[h];){var q=j.slice(e,e+=16),r=l;for(l=l.slice(0,8),d=0;64>d;d++){var s=q[d-15],t=q[d-2],u=l[0],v=l[4],w=l[7]+(c(v,6)^c(v,11)^c(v,25))+(v&l[5]^~v&l[6])+m[d]+(q[d]=16>d?q[d]:q[d-16]+(c(s,7)^c(s,18)^s>>>3)+q[d-7]+(c(t,17)^c(t,19)^t>>>10)|0),x=(c(u,2)^c(u,13)^c(u,22))+(u&l[1]^u&l[2]^l[1]&l[2]);l=[w+x|0].concat(l),l[4]=l[4]+w|0}for(d=0;8>d;d++)l[d]=l[d]+r[d]|0}for(d=0;8>d;d++)for(e=3;e+1;e--){var y=l[d]>>8*e&255;i+=(16>y?0:"")+y.toString(16)}return i}; /*https://geraintluff.github.io/sha256/sha256.min.js (public domain)*/
  17.  
  18. let index3=false;
  19. const info_elem = document.createElement('div'); let gradientStops = [];let gradientStophighlight = [];let skipSegments = [];let intervalId;let highlightSpan = document.createElement('span');const skipButton = document.createElement('button');skipButton.style.width="50px";skipButton.style.fontSize="40px";skipButton.textContent = '>>';let cu=0;
  20. skipButton.className=('skip-button-S');skipButton.style.zIndex='8888';skipButton.style.background='transparent';skipButton.style.display="none";skipButton.style.textShadow = ` -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000`;
  21. function getYouTubeVideoID(url) { const match = url.match(/(?:v=|\/)([a-zA-Z0-9_-]{11})/);
  22. return match ? match[1] : null;
  23. }
  24. let previousUrl = window.location.href;
  25. ///////
  26. setInterval(() => {
  27. if ((window.location.href !== previousUrl) && !window.location.href.includes('#searching') && !window.location.href.includes('#bottom-sheet')){
  28. previousUrl = window.location.href;
  29. clearInterval(intervalId);
  30. if(document.querySelector('div[role="slider"]')){ document.querySelector('div[role="slider"]').style.background="none";document.querySelector('div[role="slider"]').parentNode.style.background="none";}
  31. if(window.location.href.includes('watch?v=')){
  32. gradientStops = [];gradientStophighlight = []; skipSegments = [];index3=false;highlightSpan.style.opacity='0';skipButton.style.display='none';
  33. }
  34. else {skipButton.style.display='none';} }
  35. /////////
  36. if(document.querySelector('video')){
  37. if (document.querySelector('video').currentTime>0 && document.querySelector('div[role="slider"]') &&window.location.href.includes('watch?v=') &&!index3) {
  38. index3=true;
  39. const videoID = getYouTubeVideoID(window.location.href);
  40. const hash = sha256(videoID).substr(0,4);
  41. const video_obj = document.querySelector("video");
  42. let url = `https://sponsor.ajay.app/api/skipSegments/${hash}?service=YouTube&categories=%5B%22sponsor%22,%22poi_highlight%22,%22selfpromo%22%5D`;
  43. (async () => {
  44. try {
  45. if(document.querySelector('div[role="slider"]')){ document.querySelector('div[role="slider"]').style.background="none";document.querySelector('div[role="slider"]').parentNode.style.background="none";}
  46. const response = await fetch(url);
  47. if (!response.ok) {
  48. throw new Error(`HTTP error! Sponsorblock server down! Status: ${response.status}`);
  49. }
  50. const data = await response.json();
  51. if (!Array.isArray(data)) {
  52. throw new Error("Unexpected response format");
  53. }
  54. for (const video of data) {
  55. if (video.videoID != videoID) continue;
  56. info_elem.innerText = `(${video.segments.length} segments)`;
  57. const cat_n = video.segments.map(e=>e.category).sort()
  58. .reduce((acc,e) => (acc[e]=(acc[e]||0)+1, acc), {});
  59. info_elem.title = Object.entries(cat_n).map(e=>e.join(': ')).join(', ');
  60. for (const segment of video.segments) {
  61. const [start, stop] = segment.segment;
  62. let startPosition = ((start ) / (video_obj.duration)) * 100;
  63. let stopPosition = ((stop) / (video_obj.duration)) * 100;
  64. if (segment.category === "sponsor" ){
  65. gradientStops.push(`transparent ${startPosition+ 0.5}%, green ${startPosition + 0.5}%, green ${stopPosition}%, transparent ${stopPosition}%, transparent ${stopPosition}%`);
  66. skipSegments.push({ start, stop });
  67. }
  68. else if (segment.category === "selfpromo" ){
  69. gradientStops.push(`transparent ${startPosition}%, yellow ${startPosition}%, yellow ${stopPosition}%, transparent ${stopPosition}%, transparent ${stopPosition}%`);
  70. skipSegments.push({ start, stop });
  71. }
  72. else if (segment.category === "poi_highlight"){
  73. highlightSpan.textContent = 'Highlight';highlightSpan.style.position = 'absolute';highlightSpan.style.bottom = '32px';highlightSpan.style.left = `${startPosition}%`;highlightSpan.style.marginLeft="5px";highlightSpan.style.transform = 'translateX(-50%)';highlightSpan.style.backgroundColor = 'black';highlightSpan.style.zIndex="5555";highlightSpan.style.fontSize="13px";highlightSpan.style.color = 'white';highlightSpan.style.padding = '2px 5px';highlightSpan.style.display="none";highlightSpan.style.opacity='1';
  74. gradientStophighlight.push(`transparent ${((start ) / (video_obj.duration)) * 100}%, red ${((start ) / (video_obj.duration)) * 100}%, red ${((start ) / (video_obj.duration)) * 100+1}%, transparent ${((start ) / (video_obj.duration)) * 100 +1}%, transparent ${((start ) / (video_obj.duration)) * 100+1}%`);
  75. skipButton.style.display="block";
  76. skipButton.style.display="block";
  77. skipButton.addEventListener('touchstart', () => {document.querySelector('video').currentTime = start;});
  78. skipButton.addEventListener('click', () => {document.querySelector('video').currentTime = start;});
  79. skipButton.addEventListener('dblclick', () => {document.querySelector('video').currentTime = start;});
  80. }
  81. if (segment.category != "sponsor" && segment.category != "selfpromo") continue;
  82. }
  83. intervalId = setInterval(() => {
  84. if(document.querySelector('div[role="slider"]')){document.querySelector('div[role="slider"]').style.backgroundSize = `100% 25%`;document.querySelector('div[role="slider"]').style.backgroundPosition = `center center`;document.querySelector('div[role="slider"]').style.backgroundRepeat = 'no-repeat';document.querySelector('div[role="slider"]').style.backgroundImage = `linear-gradient(to right, ${gradientStops.join(', ')})`;document.querySelector('div[role="slider"]').parentNode.style.backgroundPosition = `center center`;document.querySelector('div[role="slider"]').parentNode.style.backgroundRepeat = 'no-repeat';document.querySelector('div[role="slider"]').parentNode.style.backgroundSize = `100% 25%`;document.querySelector('div[role="slider"]').parentNode.style.backgroundImage = `linear-gradient(to right, ${gradientStophighlight})`;
  85. document.querySelector('div[role="slider"]')?.parentNode.appendChild(highlightSpan);
  86. skipButton.title="Skip To Highlight";
  87. if (document.querySelector('.player-controls-top')) document.querySelector('.player-controls-top').insertBefore(skipButton,document.querySelector('.player-controls-top').firstChild);
  88. document.querySelector('div[role="slider"]')?.addEventListener('mousemove', function(event) {
  89. highlightSpan.style.display = "block";
  90. });
  91. document.querySelector('div[role="slider"]')?.parentNode.addEventListener('mouseout', function(event) {highlightSpan.style.display = "none";});
  92. }
  93. for (const { start, stop } of skipSegments) {
  94. if (video_obj.currentTime >= start && video_obj.currentTime < stop - 1) {video_obj.currentTime = stop;}
  95. }
  96. }, 1000);
  97. }
  98. } catch (error) {
  99. console.error("Error fetching data:", error);
  100. }
  101. })();
  102. }
  103. }
  104. }, 1000);
  105. })();