stream-clients/chrome/script.js

122 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2022-03-05 19:04:04 +00:00
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")
2021-06-15 11:38:01 +00:00
2022-03-05 19:33:15 +00:00
const random = () => {
const arr = new Uint8Array(32)
window.crypto.getRandomValues(arr)
2022-03-05 22:51:03 +00:00
return [...arr].map(x => x.toString(16).padStart(2, "0")).join("")
2022-03-05 19:33:15 +00:00
}
2021-06-15 11:38:01 +00:00
document.addEventListener("DOMContentLoaded", () => {
2022-03-06 07:54:23 +00:00
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")
2022-03-06 08:26:03 +00:00
if(newpath) {
2022-03-06 07:54:23 +00:00
newurl.pathname = "/" + newpath
2021-06-15 11:38:01 +00:00
}
2022-03-06 07:54:23 +00:00
} else {
newurl.pathname = oldurl.pathname
2021-06-15 11:38:01 +00:00
}
2022-03-06 07:54:23 +00:00
search.append("provider", providers.get(hostname))
2022-03-06 08:26:03 +00:00
} else {
return
2022-03-06 07:54:23 +00:00
}
2022-03-06 08:26:03 +00:00
2022-03-06 07:54:23 +00:00
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")
2022-03-06 08:26:03 +00:00
const video_id = oldurl.searchParams.get("v")
2022-03-06 07:54:23 +00:00
if(video_id) {
search.append("videoid", video_id)
2022-03-05 16:49:14 +00:00
}
2022-03-06 07:54:23 +00:00
} else {
search.append("provider", providers.get(hostname))
search.append("raw", "true")
newurl.pathname = oldurl.pathname
2022-03-05 16:49:14 +00:00
}
2022-03-06 08:26:03 +00:00
} 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
2022-03-06 07:54:23 +00:00
}
2022-03-05 19:33:15 +00:00
2022-03-06 07:54:23 +00:00
newurl.search = search.toString()
2022-03-05 19:33:15 +00:00
2022-03-06 07:54:23 +00:00
const req = {
jsonrpc: "2.0",
method: method,
id: random(),
params: {
item: {
file: newurl.href
2022-03-05 16:49:14 +00:00
}
}
2022-03-06 07:54:23 +00:00
}
if(playlistid) {
req.params.playlistid = 1
}
2022-03-05 19:04:04 +00:00
2022-03-06 07:54:23 +00:00
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"
2022-03-05 18:10:41 +00:00
}
2022-03-05 19:04:04 +00:00
})
2022-03-06 07:54:23 +00:00
console.log(resp)
}
kodiPlayButton.addEventListener("click", async ev => {
await handleKodiButton(ev, "Player.Open")
2022-03-05 19:04:04 +00:00
})
2022-03-06 07:54:23 +00:00
kodiQueueButton.addEventListener("click", async ev => {
await handleKodiButton(ev, "Playlist.Add", 1)
})
proxyButton.addEventListener("click", handleProxyButton)
2022-03-05 19:04:04 +00:00
})