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

Skip sponsors/SelfPromo, adds a highlight.

La data de 07-09-2024. Vezi ultima versiune.

// ==UserScript==
// @name         Skip sponsors - using Sponsorblock API on m.youtube.com (for Kiwi browser and others)
// @namespace    http://your-namespace.com
// @version      1.9
// @description  Skip sponsors/SelfPromo, adds a highlight.
// @author       Sp0kz
// @match        https://m.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';
 //sha256 library
	 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)*/

let index3=false;
let previousUrl = window.location.href;
  const info_elem = document.createElement('div');
	  let seekInput;
	  let gradientStops = [];
	  let skipSegments = [];
	  let redDiv = document.createElement('div');
    let highlightSpan = document.createElement('span');
	const skipButton = document.createElement('button');
skipButton.style.width="50px";
skipButton.style.fontSize="40px";
	skipButton.textContent = '>>';
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`; 
///////
	    setInterval(() => {
			    if ((window.location.href !== previousUrl)){
					      previousUrl = window.location.href;
					if(window.location.href.includes('watch?v=')){
  if(document.querySelectorAll('.segmentdivs').length > 0){
   document.querySelectorAll('.segmentdivs').forEach(div => {
div.remove();
  });
  }
gradientStops = [];
 skipSegments = [];
 index3=false;
 redDiv.style.display='none';
 highlightSpan.style.opacity='0';
 skipButton.style.display='none';
 }
 else {
	  skipButton.style.display='none';
	 }
  }
  /////////
 if(document.querySelector('div[role="slider"]') && window.location.href.includes('watch?v=') && !index3) {
	   if(document.querySelectorAll('.segmentdivs').length > 0){
   document.querySelectorAll('.segmentdivs').forEach(div => {
div.remove();
  });
  }
  		  index3=true;
  function getYouTubeVideoID(url) {
  const match = url.match(/(?:v=|\/)([a-zA-Z0-9_-]{11})/);
  return match ? match[1] : null;
}
	const videoID = getYouTubeVideoID(window.location.href);
if(document.querySelector('div[role="slider"]')) document.querySelector('div[role="slider"]').style.background="none";
  const hash = sha256(videoID).substr(0,4);
  const video_obj = document.querySelector("video");
  let url = `https://sponsor.ajay.app/api/skipSegments/${hash}?service=YouTube&categories=%5B%22sponsor%22,%22poi_highlight%22,%22selfpromo%22%5D`;
    (async () => {
		 try {
        const response = await fetch(url);   
        if (!response.ok) {
            throw new Error(`HTTP error! Sponsorblock server down! Status: ${response.status}`);
        }    
        const data = await response.json();
        if (!Array.isArray(data)) {
            throw new Error("Unexpected response format");
        }     
        for (const video of data) {
      if (video.videoID != videoID) continue;
      info_elem.innerText = `(${video.segments.length} segments)`;
      const cat_n = video.segments.map(e=>e.category).sort()
          .reduce((acc,e) => (acc[e]=(acc[e]||0)+1, acc), {});
      info_elem.title = Object.entries(cat_n).map(e=>e.join(': ')).join(', ');
      for (const segment of video.segments) {
        const [start, stop] = segment.segment;
		let startPosition = ((start ) / (video_obj.duration)) * 100;
            let stopPosition = ((stop) / (video_obj.duration)) * 100;
		   		  if (segment.category === "sponsor" ){
		  const greendiv = document.createElement('div');       
			 greendiv.style.position = 'absolute';
  greendiv.style.left = `${startPosition+0.5}%`;
  greendiv.style.width = `${stopPosition - (startPosition)}%`;
  greendiv.style.height = '10%';
  greendiv.style.top='46%';  
  greendiv.style.pointerEvents = 'none';
  greendiv.className="segmentdivs";
  greendiv.style.zIndex="55";
      greendiv.style.backgroundColor = 'green';
	      greendiv.style.display="none";
	  	     document.body.appendChild(greendiv);
		  skipSegments.push({ start, stop });
		  }
		  else if (segment.category === "selfpromo" ){
	 skipSegments.push({ start, stop });
		              const yellowdiv = document.createElement('div');       
			 yellowdiv.style.position = 'absolute';
  yellowdiv.style.left = `${startPosition}%`;
  yellowdiv.style.width = `${stopPosition - startPosition}%`;
  yellowdiv.style.height = '10%'; 
  yellowdiv.style.pointerEvents = 'none';
  yellowdiv.className="segmentdivs";
   yellowdiv.style.zIndex="55";
        yellowdiv.style.top='46%';
  yellowdiv.style.backgroundColor = 'yellow';
      yellowdiv.style.display="none";
    	      document.body.appendChild(yellowdiv);
		 }
		   		else  if (segment.category === "poi_highlight"){
				   stopPosition+=1;
				      seekInput = document.querySelector('div[role="slider"]');
    redDiv.style.position = 'absolute';
    redDiv.style.left = `${startPosition}%`;
    redDiv.style.width = "4px";
    redDiv.style.height = '15px';
	redDiv.style.pointerEvents='none';
	redDiv.title="Highlight";
	 redDiv.style.display='block';
	redDiv.style.zIndex="666";
    redDiv.style.backgroundColor = 'red';
        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';
			 skipButton.style.display="block";	
	skipButton.addEventListener('touchstart', () => {
		document.querySelector('video').pause();
    document.querySelector('video').currentTime = start;
	document.querySelector('video').play();
});			
	skipButton.addEventListener('click', () => {
		document.querySelector('video').pause();
    document.querySelector('video').currentTime = start;
  document.querySelector('video').play();
});		
	skipButton.addEventListener('dblclick', () => {
		document.querySelector('video').pause();
    document.querySelector('video').currentTime = start;
	document.querySelector('video').play();
});					 
		     }
       if (segment.category != "sponsor" && segment.category != "selfpromo") continue;
      }
	  			  		  video_obj.removeEventListener("timeupdate", handletimeupdate);
		  video_obj.addEventListener("timeupdate", handletimeupdate);
		  			   function handletimeupdate() {
  for (const { start, stop } of skipSegments) {
    if (video_obj.currentTime >= start && video_obj.currentTime < stop - 1) {
      video_obj.currentTime = stop;
    }
  }
  	 setTimeout(() => {
	   		 seekInput = document.querySelector('div[role="slider"]');
			 if(seekInput){
			      if(document.querySelectorAll('.segmentdivs').length > 0){
   document.querySelectorAll('.segmentdivs').forEach(div => {
	   	    div.style.display="block";
    document.querySelector('div[role="slider"]')?.appendChild(div);
  });
  }
  document.querySelector('div[role="slider"')?.parentNode.appendChild(redDiv);
document.querySelector('div[role="slider"]')?.parentNode.appendChild(highlightSpan);
			 			 skipButton.title="Skip To Highlight";
						  document.querySelector('.player-controls-top')?.insertBefore(skipButton, document.querySelector('.player-controls-top')?.firstChild);
 document.querySelector('div[role="slider"]')?.addEventListener('mousemove', function(event) {
            highlightSpan.style.display = "block";

    });

    document.querySelector('div[role="slider"]')?.parentNode.addEventListener('mouseout', function(event) {
            highlightSpan.style.display = "none";
    });
  }
  	}, 600);
     }
        }
    } catch (error) {
        console.error("Error fetching data:", error);
    }
})();
  }
  }, 1000);
})();