-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-script.js
1 lines (1 loc) · 7.36 KB
/
content-script.js
1
"use strict";var page,url=location.href.split("?")[0],title=!1,importing=!1;function exportPlay(){let e=[];e.push(document.querySelector('div[class="dynamic-text-container style-scope yt-dynamic-sizing-formatted-string"]').querySelector("yt-formatted-string").innerHTML);let t=document.querySelectorAll('a[id="video-title"]');for(let l of t)l.href=l.href.split("&list=")[0],e.push(title?l.title+","+l.href:l.href);let r=e.join("\n"),i=new Blob([r],{type:"text/csv"}),s=window.document.createElement("a");s.href=window.URL.createObjectURL(i),s.download=e[0].replace(/\s/g,"")+".csv",document.body.appendChild(s),s.click(),document.body.removeChild(s)}function waitForElm(e){return new Promise(t=>{if(document.querySelector(e))return t(document.querySelector(e));let l=new MutationObserver(r=>{document.querySelector(e)&&(l.disconnect(),t(document.querySelector(e)))});l.observe(document.body,{childList:!0,subtree:!0})})}function timeout(e){return new Promise(t=>{setTimeout(t,e)})}async function savePlaylist(e,t,l){(await waitForElm("[aria-label='Save to playlist']")).click(),await waitForElm("ytd-add-to-playlist-create-renderer"),document.querySelectorAll('button[aria-label="Create"')[1];let r=document.getElementsByClassName("tp-yt-paper-input")[3],i=document.querySelector(`yt-formatted-string[title='${e}']`);if(null!=i){let s=i.parentElement.parentElement.parentElement.parentElement,n=s.checked;if(void 0===n&&void 0===(n=s.getAttribute("aria-checked"))){let o=new Promise(e=>{let t=new MutationObserver(l=>{let r=s.getAttribute("aria-checked");void 0!=r&&(t.disconnect(),e(r))});t.observe(s,{attributes:!0})});n=await o}if("false"==n)i.click();else if("true"==n){let a=parseInt(l)+1;a>=t.length?(chrome.runtime.sendMessage({type:"importdone",data:{name:e}}),sessionStorage.setItem("importindex",0)):(sessionStorage.setItem("importindex",a),window.location.href=t[a]);return}else{console.error(`ERROR: ${n} of type ${typeof n} not recognized`);return}}else{await waitForElm('ytd-compact-link-renderer[class="style-scope ytd-add-to-playlist-create-renderer"]');document.getElementsByClassName("ytd-add-to-playlist-create-renderer")[0].click(),r.value=e,r.parentElement.dispatchEvent(new Event("input")),document.querySelectorAll('ytd-button-renderer[class="style-scope ytd-add-to-playlist-create-renderer"]')[1].click()}let c=new MutationObserver(r=>{let i=document.querySelectorAll("tp-yt-paper-toast");for(let s=0;s<i.length;s++)if(i[s].children[1].firstElementChild.firstElementChild&&("Saved to "===i[s].children[1].firstElementChild.firstElementChild.innerHTML||"Added to "===i[s].children[1].firstElementChild.firstElementChild.innerHTML)){c.disconnect();let n=parseInt(l)+1;if(n>=t.length)chrome.runtime.sendMessage({type:"importdone",data:{name:e}}),sessionStorage.setItem("importindex",0);else{sessionStorage.setItem("importindex",n),window.location.href=t[n];return}}});c.observe(document.querySelector("ytd-popup-container"),{childList:!0,subtree:!0})}function injectplaylist(e){!document.getElementById("scrollb")&&waitForElm('div[class="metadata-buttons-wrapper style-scope ytd-playlist-header-renderer"]').then(()=>{function e(){if(null===document.querySelector('div[id="contents"][class=" style-scope ytd-playlist-video-list-renderer style-scope ytd-playlist-video-list-renderer"]').querySelector("ytd-continuation-item-renderer"))exportPlay(),r.click();else{document.querySelector('div[id="contents"][class=" style-scope ytd-playlist-video-list-renderer style-scope ytd-playlist-video-list-renderer"]').querySelector("ytd-continuation-item-renderer").scrollIntoView();let t=new MutationObserver(e=>{null===document.querySelector('div[id="contents"][class=" style-scope ytd-playlist-video-list-renderer style-scope ytd-playlist-video-list-renderer"]').querySelector("ytd-continuation-item-renderer")?(t.disconnect(),"true"==r.getAttribute("auto")&&exportPlay(),r.click()):document.querySelector('div[id="contents"][class=" style-scope ytd-playlist-video-list-renderer style-scope ytd-playlist-video-list-renderer"]').querySelector("ytd-continuation-item-renderer").scrollIntoView()});t.observe(document.querySelector('div[id="contents"][class=" style-scope ytd-playlist-video-list-renderer style-scope ytd-playlist-video-list-renderer"]'),{childList:!0,subtree:!0}),r.addEventListener("click",()=>{t.disconnect(),r.addEventListener("click",e,{once:!0}),r.setAttribute("scroll",!1)},{once:!0}),r.setAttribute("scroll",!0)}}let t=document.querySelector('div[class="metadata-buttons-wrapper style-scope ytd-playlist-header-renderer"]'),l=t.querySelector("ytd-menu-renderer"),r=document.createElement("button");r.className="yt-spec-button-shape-next yt-spec-button-shape-next--tonal yt-spec-button-shape-next--overlay yt-spec-button-shape-next--size-m yt-spec-button-shape-next--icon-button",r.id="scrollb",r.setAttribute("auto",!0),r.addEventListener("click",e,{once:!0});let i=document.createElement("ytd-button-renderer");i.className="style-scope ytd-playlist-header-renderer",t.insertBefore(i,l);i.querySelector("yt-button-shape").appendChild(r);i.querySelector("tp-yt-paper-tooltip").innerHTML='<div id="tooltip" class="style-scope tp-yt-paper-tooltip hidden" style-target="tooltip">Export</div>';let s=document.createElement("div");s.className="yt-spec-button-shape-next__icon";let n=document.createElement("yt-icon"),o=document.createElement("yt-icon-shape");o.className="style-scope yt-icon",r.appendChild(s),s.appendChild(n),n.appendChild(o),o.innerHTML='<icon-shape class="yt-spec-icon-shape"><div style="width: 100%; height: 100%; fill: currentcolor;"><svg width="24" height="24" viewBox="0 0 30 30" xmlns="http://www.w3.org/2000/svg" focusable="false" style="pointer-events: none; display: block; width: 100%; height: 100%;"><path d="M16,1A15,15,0,1,0,31,16,15.007,15.007,0,0,0,16,1Zm0,2A13,13,0,1,1,3,16,13.006,13.006,0,0,1,16,3Z" transform="translate(-1 -1)" fill-rule="evenodd"/><path d="M21.293,12.293,16,17.586l-5.293-5.293a1,1,0,0,0-1.414,1.414l6,6a1,1,0,0,0,1.414,0l6-6a1,1,0,1,0-1.414-1.414Z" transform="translate(-1 -1)" fill - rule="evenodd"/></svg></div></icon-shape>';let a=new MutationObserver(e=>{"true"==r.getAttribute("scroll")&&r.click(),start()});a.observe(document.querySelector('div[class="dynamic-text-container style-scope yt-dynamic-sizing-formatted-string"]').querySelector("yt-formatted-string"),{childList:!0,subtree:!0})})}function start(){if("https://www.youtube.com/playlist"===url)page="playlist",injectplaylist(!1);else if("https://www.youtube.com/watch"===url){if(page="video",!0!=importing){importing=!0;sessionStorage.getItem("importindex")>=1&&savePlaylist(sessionStorage.getItem("importname"),JSON.parse(sessionStorage.getItem("importvideos")),sessionStorage.getItem("importindex"))}}else page=null}start(),chrome.runtime.onMessage.addListener((e,t,l)=>{let{type:r,data:i}=e;"exportplay"===r?"playlist"!=page?alert("This page is not a playlist"):exportPlay():"scroll"===r?"playlist"!=page?alert("This page is not a playlist"):document.getElementById("scrollb").click():"startimport"===r?"video"!=page?alert("This page is not a video"):(sessionStorage.setItem("importvideos",JSON.stringify(i.arr)),sessionStorage.setItem("importname",i.name),sessionStorage.setItem("importindex",1),savePlaylist(i.name,i.arr,1),importing=!0):"update"===r?(document.getElementById("scrollb").setAttribute("auto",i.auto),title=i.title):"count"===r&&l({text:document.querySelectorAll('a[id="video-title"]').length.toString(),scroll:document.getElementById("scrollb").getAttribute("scroll")})});