tytd-chrome-extension/js/popup.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-07-01 04:55:42 +00:00
const url=document.getElementById('url_path');
const res=document.getElementById('res');
const list = document.getElementById('list');
const adddl = document.getElementById('adddl');
const dllocal = document.getElementById('dllocal');
const server = document.getElementById('server');
var tytd_url = "https://tytd.site.tesses.net/";
chrome.storage.sync.get('tytdurl', function(data) {
if(data.tytdurl != undefined)
{
tytd_url = data.tytdurl;
server.href = tytd_url;
server.innerText = tytd_url;
}
});
const tytd_res = ["Mux","PreMuxed","AudioOnly","VideoOnly"];
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
// and use that tab to fill in out title and url
var tab = tabs[0];
/*fetch(`http://192.168.0.142:3252/api/AddItem/${tab.url}`).then(e=>e.text()).then(e=>{
element.innerText = e;
});*/
url.value = tab.url;
});
function enable_waiting()
{
document.getElementById('p2').style="opacity: 100%;"
}
function get_tytd_url()
{
if(tytd_url.endsWith('/'))
{
return tytd_url.substring(0,tytd_url.length - 1);
}
return tytd_url;
}
function add_to_downloader()
{
enable_waiting();
if(list.value.length === 0)
{
var url2 = `${get_tytd_url()}/api/AddItemRes/${res.value}/${url.value}`;
fetch(url2).then(e=>e.text()).then(e=>{
alert("Added video to downloader");
close_window();
});
}
else
{
var url2 = `${get_tytd_url()}/api/v2/AddToList?name=${encodeURIComponent(list.value)}&v=${encodeURIComponent(url.value)}&res=${tytd_res[res.value]}`;
fetch(url2).then(e=>e.text()).then(e=>{
alert(`Added video to playlist: ${list.value}`);
close_window();
});
}
}
function download_local()
{
enable_waiting();
var url2 = `${get_tytd_url()}/api/Storage/VideoRes/${res.value}/${url.value}`;
chrome.downloads.download({url: url2},
function(id) {
close_window();
});
}
function close_window()
{
window.close();
}
adddl.onclick = ()=>{add_to_downloader()};
dllocal.onclick = ()=>{download_local()};