let providers = new Map() providers.set("www.youtube.com", "youtube") providers.set("youtube.com", "youtube") providers.set("youtu.be", "youtube") providers.set("tv.nrk.no", "nrk") providers.set("seafile.purser.it", "seafile") const random = () => { const arr = new Uint8Array(32) window.crypto.getRandomValues(arr) return [...arr].map(x => x.toString(16).padStart(2, "0")).join("") } document.addEventListener("DOMContentLoaded", () => { const proxyButton = document.getElementById("proxy-button") const kodiPlayButton = document.getElementById("kodi-play-button") const kodiQueueButton = document.getElementById("kodi-queue-button") async function handleProxyButton(ev) { const [tab] = chrome.tabs.query({currentWindow: true, active: true}) const oldurl = new URL(tab.url) const search = new URLSearchParams() const hostname = oldurl.hostname.toLowerCase() let newurl = new URL("https://stream.purser.it") if(providers.has(hostname)) { if(hostname.includes("youtube.com")) { let newpath = oldurl.searchParams.get("v") if(newpath) { newurl.pathname = "/" + newpath } } else { newurl.pathname = oldurl.pathname } search.append("provider", providers.get(hostname)) } else { return } newurl.search = search.toString() const newtab = {} newtab.url = newurl.href chrome.tabs.create(newtab) } async function handleKodiButton(ev, method, playlistid) { const [tab] = await chrome.tabs.query({currentWindow: true, active: true}) const oldurl = new URL(tab.url) const search = new URLSearchParams() const hostname = oldurl.hostname.toLowerCase() let newurl = new URL("https://stream.purser.it") if(providers.has(hostname)) { if(hostname.includes("youtube.com")) { newurl = new URL("plugin://plugin.video.youtube") search.append("action", "play_video") const video_id = oldurl.searchParams.get("v") if(video_id) { search.append("videoid", video_id) } } else { search.append("provider", providers.get(hostname)) search.append("raw", "true") newurl.pathname = oldurl.pathname } } else if(hostname == "stream.purser.it") { const provider = oldurl.searchParams.get("provider") if(provider) { if(provider == "youtube") { newurl = new URL("plugin://plugin.video.youtube") search.append("action", "play_video") const video_id = oldurl.pathname.replace("/", "") if(video_id) { search.append("videoid", video_id) } } else { search.append("provider", provider) search.append("raw", "true") } } } else { return } newurl.search = search.toString() const req = { jsonrpc: "2.0", method: method, id: random(), params: { item: { file: newurl.href } } } if(playlistid) { req.params.playlistid = 1 } const resp = await fetch("http://127.0.0.1:4000/jsonrpc", { method: "POST", body: JSON.stringify(req), headers: { "Content-Type": "application/json; charset=utf-8" } }) console.log(resp) } kodiPlayButton.addEventListener("click", async ev => { await handleKodiButton(ev, "Player.Open") }) kodiQueueButton.addEventListener("click", async ev => { await handleKodiButton(ev, "Playlist.Add", 1) }) proxyButton.addEventListener("click", handleProxyButton) })