tpbproxy/inject.html

155 lines
4.9 KiB
HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div id="dialog" title="Torrents">
<h3>
Torrents
</h3>
<ul>
</ul>
</div>
<script type="text/javascript">
function post(url, data, callback){
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function(res, text){
callback(
text !== 'success' ? res.statusText : null,
JSON.parse(res.responseText),
res.status
)
}
});
}
var torrents = (function(){
var torrents = {};
torrents.list = JSON.parse(window.localStorage.getItem('torrents') || '{}' ).list || [];
var in_list = function(data){
return false;
for(let torrent of torrents.list){
if(torrent.hash === data.hash) return true;
}
};
torrents.add = function(data){
if(in_list(data)) return false;
torrents.list.push(data);
torrents.updateStorage();
};
torrents.updateStorage = function(){
window.localStorage.setItem('torrents', JSON.stringify({list: torrents.list}))
};
torrents.statusAll = function(callback){
var list_count = torrents.list.length;
var count = 0;
for(let torrent of torrents.list){
if(torrent.percentDone === 1){
count++;
if(count === list_count){
torrents.updateStorage();
(callback || function(){})(torrents.list)
}
continue;
}
$.getJSON('/__api/torrent/'+torrent.id, function(data){
$.extend(true, torrent, data.cres.torrents[0])
count++;
if(count === list_count){
torrents.updateStorage();
(callback || function(){})(torrents.list)
}
})
}
};
torrents.updateDialog = function(){
var $ol = $($('#dialog').find('ul')[0]);
$ol.html('');
torrents.list.forEach(function(torrent){
let li = `<li>${torrent.name}`;
if(torrent.percentDone < 1){
li += `
<div id="progressbar-${torrent.hashString}"></div>
ETA: ${moment().seconds(torrent.eta).fromNow()} Rate: ${Math.floor(torrent.rateDownload/1024/1024)}mb
`
}else{
li += `<br /> Done! <a href="https://stuff.718it.biz/torrents/${torrent.name}" target="_blank"> HTTP Link</a>`
}
li += `<hr /></li>`
$ol.prepend(li)
$("#progressbar-"+torrent.hashString).progressbar({
value: torrent.percentDone*100
});
});
}
$( document ).ready(function() {
torrents.statusAll();
torrents.timmer = setInterval(function(){
console.log('auto update')
torrents.statusAll(function(){
torrents.updateDialog()
});
}, 2000)
});
return torrents;
})()
$( document ).ready(function() {
$($('input[name="q"]')[0]).before($('<img src="https://chocolatey.org/content/packageimages/transmission.2.92.svg" height=24 width=24/>').on('click', function(){
torrents.statusAll(function(){
console.log('stats all')
$("#dialog").dialog();
torrents.updateDialog()
});
}))
$('a').each(function(idx, el){
var $el = $(el);
if($el.attr('href') && $el.attr('href').match("magnet:?")){
$el.before('<img class="718link" src="https://chocolatey.org/content/packageimages/transmission.2.92.svg" height=24 width=24 data-link="'+$el.attr('href')+'"/>')
}
})
$("body").on('click', 'img.718link', function(el){
post('/__api/torrent', {
magnet: window.btoa($(this).data('link')),
password: prompt('password?')
}, function(err, data){
torrents.add(data);
// console.log(data);
});
});
});
</script>