stream-clients/chrome/script.js

67 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2021-06-15 11:38:01 +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");
2022-03-05 16:49:14 +00:00
providers.set("seafile.purser.it", "seafile");
2021-06-15 11:38:01 +00:00
document.addEventListener("DOMContentLoaded", () => {
2022-03-05 16:49:14 +00:00
let [proxyButton, kodiButton] = document.getElementsByTagName("button");
proxyButton.addEventListener("click", (ev) => {
2021-06-15 11:38:01 +00:00
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;
}
search.append("provider", providers.get(hostname));
}
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) => {
let oldurl = new URL(tabs[0].url);
let newurl = new URL(tabs[0].url);
let search = new URLSearchParams();
let hostname = oldurl.hostname.toLowerCase();
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(newpath) {
search.append("videoid", video_id)
}
}
newurl.search = search.toString();
let req = {
jsonrpc: "2.0",
method: "Player.Open",
id: secrets.token_hex(),
params: {
item: {
file: newurl.href
}
}
}
fetch("http://127.0.0.1:8083/jsonrpc", {
method: "POST",
body: JSON.stringify(req)
}).then(resp => console.log(resp))
});
});
2021-06-15 11:38:01 +00:00
});