const categories=["sponsor","selfpromo","interaction","intro","outro","preview","music_offtopic","exclusive_access","poi_highlight"],actionTypes=["skip","mute","full","poi"],skipThreshold=[.2,1],serverEndpoint="https://sponsor.ajay.app",skipTracking=!0,highlightKey="Enter",VERSION="1.3.2";let video,videoID,muteEndTime,skipSegments=new Map,muteSegments=new Map;const getVideoID=()=>new URL(window.location.href).searchParams.get("v");function getJSON(e,t){const o=new XMLHttpRequest;o.open("GET",e),o.responseType="json",o.onload=()=>200==o.status?t(null,o.response):t(o.status),o.send()}const trackSkip=e=>{var t;skipTracking&&((t=new XMLHttpRequest).open("POST",serverEndpoint+"/api/viewedVideoSponsorTime?UUID="+e),t.send())};function fetch(e){e=`${serverEndpoint}/api/skipSegments?videoID=${e}&categories=${JSON.stringify(categories)}&actionTypes=`+JSON.stringify(actionTypes);const o=e=>[e.segment[0],{end:e.segment[1],uuid:e.UUID}];getJSON(e,(e,t)=>{if(e)return console.error("[SB.js]","error fetching segments",e);t.forEach(e=>{"skip"===e.actionType?skipSegments.set(...o(e)):"mute"===e.actionType?muteSegments.set(...o(e)):"full"===e.actionType?createVideoLabel(e):"poi"===e.actionType&&createPOILabel(e)}),console.log("[SB.js] Loaded Segments")})}function skipOrMute(){var e=video.currentTime,t=(video.muted&&e>=muteEndTime&&(video.muted=!1,muteEndTime=0),findEndTime(e,skipSegments)),t=(t&&(video.currentTime=t),findEndTime(e,muteSegments));t&&(video.muted=!0,muteEndTime=t)}function findEndTime(e,t){let o;for(const s of t.keys())if(e+skipThreshold[0]>=s&&e-s<=skipThreshold[1]){var i,n=t.get(s);o=n.end,trackSkip(n.uuid),t.delete(s);for(const r of t.keys())o>=r&&r>=e&&(i=t.get(r),o=i.end,trackSkip(i.uuid),t.delete(r));return o}return o}function createPOILabel(t){createVideoLabel(t,"poi");const o=e=>{e.key===highlightKey&&(video.currentTime=t.segment[1],trackSkip(t.UUID),document.querySelector("#sbjs-label-poi").style.display="none",document.removeEventListener("keydown",o))};document.addEventListener("keydown",o)}function createVideoLabel(e,t="full"){var o,i,n,s=document.querySelector("#title h1, h1.title.ytd-video-primary-info-renderer");s?(o=e.category,i={sponsor:["#0d0","#111",(i=e=>`The entire video is ${e} and is too tightly integrated to be able to seperate`)("sponsor")],selfpromo:["#ff0","#111",i("selfpromo")],exclusive_access:["#085","#fff","This video showcases a product, service or location that they've received free or subsidized access to"],poi_highlight:["#f18","#fff",`Press ${highlightKey} to skip to the highlight`]}[o],(n=document.createElement("span")).title=i[2],n.innerText=o,n.id="sbjs-label-"+t,n.style=`color: ${i[1]}; background-color: ${i[0]}; display: flex; margin: 0 5px;`,s.style="display: flex;",s.prepend(n)):setTimeout(createVideoLabel,200,e)}const reset=()=>{video=void 0,videoID=void 0,muteEndTime=0,skipSegments=new Map,muteSegments=new Map};function setup(){if(videoID!==getVideoID())return console.log(`@mchangrh/SB.js ${VERSION} Loaded`),console.log("Uses SponsorBlock data licensed used under CC BY-NC-SA 4.0 from https://sponsor.ajay.app/"),document.querySelector("#previewbar")?console.log("[SB.js] Extension Present, Exiting"):(video=document.querySelector("video"),fetch(videoID=getVideoID()),video?void video.addEventListener("timeupdate",skipOrMute):console.log("[SB.js] no video"))}document.addEventListener("yt-navigate-start",reset),document.addEventListener("yt-navigate-finish",setup),setup();