stream-clients/chrome/script.js

87 lines
3.1 KiB
JavaScript
Raw 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)
return btoa(arr)
}
2021-06-15 11:38:01 +00:00
document.addEventListener("DOMContentLoaded", () => {
2022-03-05 19:04:04 +00:00
let [proxyButton, kodiButton] = document.getElementsByTagName("button")
2022-03-05 16:49:14 +00:00
proxyButton.addEventListener("click", (ev) => {
2021-06-15 11:38:01 +00:00
chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
2022-03-05 19:04:04 +00:00
let oldurl = new URL(tabs[0].url)
let newurl = new URL("https://stream.purser.it")
let search = new URLSearchParams()
let hostname = oldurl.hostname.toLowerCase()
2021-06-15 11:38:01 +00:00
if(providers.has(hostname)) {
if(hostname.includes("youtube.com")) {
2022-03-05 19:04:04 +00:00
let newpath = oldurl.searchParams.get("v")
2021-06-15 11:38:01 +00:00
if((newpath instanceof String) || ((typeof newpath) === "string")) {
2022-03-05 19:04:04 +00:00
newurl.pathname = "/" + newpath
2021-06-15 11:38:01 +00:00
}
} else {
2022-03-05 19:04:04 +00:00
newurl.pathname = oldurl.pathname
2021-06-15 11:38:01 +00:00
}
2022-03-05 19:04:04 +00:00
search.append("provider", providers.get(hostname))
2021-06-15 11:38:01 +00:00
}
2022-03-05 19:04:04 +00:00
newurl.search = search.toString()
let tab = {}
tab.url = newurl.href
chrome.tabs.create(tab)
})
})
2022-03-05 16:49:14 +00:00
kodiButton.addEventListener("click", (ev) => {
chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
2022-03-05 19:04:04 +00:00
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
2022-03-05 16:49:14 +00:00
}
}
2022-03-05 19:33:15 +00:00
2022-03-05 19:04:04 +00:00
newurl.search = search.toString()
2022-03-05 19:33:15 +00:00
2022-03-05 16:49:14 +00:00
let req = {
jsonrpc: "2.0",
method: "Player.Open",
2022-03-05 19:33:15 +00:00
id: random(),
2022-03-05 16:49:14 +00:00
params: {
item: {
file: newurl.href
}
}
}
2022-03-05 19:04:04 +00:00
2022-03-05 18:10:41 +00:00
try {
2022-03-05 19:04:04 +00:00
fetch("http://127.0.0.1:4000/jsonrpc", {
2022-03-05 18:10:41 +00:00
method: "POST",
body: JSON.stringify(req),
headers: {
2022-03-05 19:04:04 +00:00
"Content-Type": "application/json charset=utf-8"
2022-03-05 18:10:41 +00:00
}
}).then(resp => console.log(resp))
} catch(e) {
console.log(e)
}
2022-03-05 19:04:04 +00:00
})
})
})