From 050cf89b455796c2b54fb08a149e551a90515c6f Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Sun, 6 Mar 2022 08:54:23 +0100 Subject: [PATCH] add kodi queue --- chrome/manifest.json | 2 +- chrome/popup.html | 5 +- chrome/script.js | 137 ++++++++++++++++++++++++------------------- 3 files changed, 80 insertions(+), 64 deletions(-) diff --git a/chrome/manifest.json b/chrome/manifest.json index b3df8f3..ae1b899 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -7,7 +7,7 @@ "48": "icon-48.png", "128": "icon-128.png" }, - "version": "108.0", + "version": "109.0", "manifest_version": 3, "host_permissions": ["http://127.0.0.1:4000/jsonrpc"], "permissions": ["tabs"], diff --git a/chrome/popup.html b/chrome/popup.html index a6ed532..f13deb4 100644 --- a/chrome/popup.html +++ b/chrome/popup.html @@ -4,7 +4,8 @@ Proxy Stream - - + + + diff --git a/chrome/script.js b/chrome/script.js index f6576a8..ce4f8e4 100644 --- a/chrome/script.js +++ b/chrome/script.js @@ -12,75 +12,90 @@ const random = () => { } document.addEventListener("DOMContentLoaded", () => { - let [proxyButton, kodiButton] = document.getElementsByTagName("button") - proxyButton.addEventListener("click", (ev) => { - chrome.tabs.query({currentWindow: true, active: true}, (tabs) => { - let oldurl = new URL(tabs[0].url) - let newurl = new URL("https://stream.purser.it") - let search = new URLSearchParams() - let hostname = oldurl.hostname.toLowerCase() - if(providers.has(hostname)) { - if(hostname.includes("youtube.com")) { - let newpath = oldurl.searchParams.get("v") - if((newpath instanceof String) || ((typeof newpath) === "string")) { - newurl.pathname = "/" + newpath - } - } else { - newurl.pathname = oldurl.pathname + 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 instanceof String) || ((typeof newpath) === "string")) { + newurl.pathname = "/" + newpath } + } else { + newurl.pathname = oldurl.pathname + } + search.append("provider", providers.get(hostname)) + } + 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") + let 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 } - newurl.search = search.toString() - let tab = {} - tab.url = newurl.href - chrome.tabs.create(tab) - }) - }) - kodiButton.addEventListener("click", (ev) => { - chrome.tabs.query({currentWindow: true, active: true}, (tabs) => { - let oldurl = new URL(tabs[0].url) - let newurl = new URL("https://stream.purser.it") - let search = new URLSearchParams() - let hostname = oldurl.hostname.toLowerCase() - if(providers.has(hostname)) { - if(hostname.includes("youtube.com")) { - newurl = new URL("plugin://plugin.video.youtube") - search.append("action", "play_video") - let 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 + } + + newurl.search = search.toString() + + const req = { + jsonrpc: "2.0", + method: method, + id: random(), + params: { + item: { + file: newurl.href } } + } - newurl.search = search.toString() + if(playlistid) { + req.params.playlistid = 1 + } - let req = { - jsonrpc: "2.0", - method: "Player.Open", - id: random(), - params: { - item: { - file: newurl.href - } - } - } - - try { - fetch("http://127.0.0.1:4000/jsonrpc", { - method: "POST", - body: JSON.stringify(req), - headers: { - "Content-Type": "application/json; charset=utf-8" - } - }).then(resp => console.log(resp)) - } catch(e) { - console.log(e) + 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) })