From 4f1b8178696fa654b5a8f626d1dfca7e3e7065ba Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 2 Jun 2024 08:22:45 +0300 Subject: [PATCH] replace getScript to ajax method by CSP update #354 --- js/interface_home.js | 141 +++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 65 deletions(-) diff --git a/js/interface_home.js b/js/interface_home.js index 0fbde4d..68e5576 100644 --- a/js/interface_home.js +++ b/js/interface_home.js @@ -344,79 +344,90 @@ function initWebTorrent() { twister.torrentIds = $.localStorage.get('torrentIds'); WEBTORRENT_ANNOUNCE = $.Options.WebTorrentTrackers.val.split(/[ ,]+/) - $.getScript('js/webtorrent.min.js', function() { - WebTorrentClient = new WebTorrent(); - console.log("WebTorrent started") - WebTorrentClient.on('error', function (err) { - console.error('ERROR: ' + err.message); - }); - WebTorrentClient.on('warning', function (err) { - console.error('WARNING: ' + err.message); - }); - $.getScript('js/localforage.min.js', function() { - localforage.setDriver([localforage.INDEXEDDB,localforage.WEBSQL]).then(function() { - for (var torrentId in twister.torrentIds) { - if( twister.torrentIds[torrentId] ) { - if (typeof(twister.torrentIds[torrentId]) === "string") { - // get blob file to restart seeding this file - var onGetItem = function(torrentId, err, data) { - console.log("onget:", torrentId, err, data) - if (err || data === null) { - // error reading blob, just add torrentId - console.log("WebTorrent auto-download: " + torrentId + - " (previously seeded as: " + twister.torrentIds[torrentId] + ")" ); + jQuery.ajax({ + crossDomain: true, + dataType: 'script', + url: 'js/webtorrent.min.js', + success: function() { + WebTorrentClient = new WebTorrent(); + console.log("WebTorrent started") + WebTorrentClient.on('error', function (err) { + console.error('ERROR: ' + err.message); + }); + WebTorrentClient.on('warning', function (err) { + console.error('WARNING: ' + err.message); + }); + + jQuery.ajax({ + crossDomain: true, + dataType: 'script', + url: 'js/localforage.min.js', + success: function() { + localforage.setDriver([localforage.INDEXEDDB,localforage.WEBSQL]).then(function() { + for (var torrentId in twister.torrentIds) { + if( twister.torrentIds[torrentId] ) { + if (typeof(twister.torrentIds[torrentId]) === "string") { + // get blob file to restart seeding this file + var onGetItem = function(torrentId, err, data) { + console.log("onget:", torrentId, err, data) + if (err || data === null) { + // error reading blob, just add torrentId + console.log("WebTorrent auto-download: " + torrentId + + " (previously seeded as: " + twister.torrentIds[torrentId] + ")" ); + WebTorrentClient.add(torrentId); + } else { + var fileBlob = new File([data], twister.torrentIds[torrentId]); + console.log('WebTorrent seeding: "' + twister.torrentIds[torrentId] + + '" size: ' + data.size); + WebTorrentClient.seed(fileBlob); + } + } + localforage.getItem(torrentId, onGetItem.bind(null, torrentId)); + } else if ($.Options.WebTorrentAutoDownload.val === 'enable') { + console.log("WebTorrent auto-download: " + torrentId); WebTorrentClient.add(torrentId); - } else { - var fileBlob = new File([data], twister.torrentIds[torrentId]); - console.log('WebTorrent seeding: "' + twister.torrentIds[torrentId] + - '" size: ' + data.size); - WebTorrentClient.seed(fileBlob); } } - localforage.getItem(torrentId, onGetItem.bind(null, torrentId)); - } else if ($.Options.WebTorrentAutoDownload.val === 'enable') { - console.log("WebTorrent auto-download: " + torrentId); - WebTorrentClient.add(torrentId); } - } - } - // setup attach button - $(".post-area-attach").show(); - var fileInput = $("#fileInputAttach"); - fileInput.on('change', function(event) { - var file = fileInput[0].files[0]; - var seedingTorrent = undefined; - for (var i = 0; i < WebTorrentClient.torrents.length; i++) { - var torrent = WebTorrentClient.torrents[i]; - if (torrent.length === file.size && - torrent.files[0].name === file.name) { - seedingTorrent = torrent; - } - } - var saveBlobFile = function(infoHash,file) { - var magnetLink = "magnet:?xt=urn:btih:" + infoHash - var blobFile = new Blob([file]); - localforage.setItem(magnetLink, blobFile); - twister.torrentIds[magnetLink] = file.name; - $.localStorage.set('torrentIds', twister.torrentIds); - return magnetLink; - } - if (seedingTorrent) { - var magnetLink = saveBlobFile(seedingTorrent.infoHash,file); - console.log('Already seeding ' + magnetLink); - shortenMagnetLink(event,magnetLink); - } else { - WebTorrentClient.seed(file, function (torrent) { - var magnetLink = saveBlobFile(torrent.infoHash,file); - console.log('Client is seeding ' + magnetLink); - shortenMagnetLink(event,magnetLink); + // setup attach button + $(".post-area-attach").show(); + var fileInput = $("#fileInputAttach"); + fileInput.on('change', function(event) { + var file = fileInput[0].files[0]; + var seedingTorrent = undefined; + for (var i = 0; i < WebTorrentClient.torrents.length; i++) { + var torrent = WebTorrentClient.torrents[i]; + if (torrent.length === file.size && + torrent.files[0].name === file.name) { + seedingTorrent = torrent; + } + } + var saveBlobFile = function(infoHash,file) { + var magnetLink = "magnet:?xt=urn:btih:" + infoHash + var blobFile = new Blob([file]); + localforage.setItem(magnetLink, blobFile); + twister.torrentIds[magnetLink] = file.name; + $.localStorage.set('torrentIds', twister.torrentIds); + return magnetLink; + } + if (seedingTorrent) { + var magnetLink = saveBlobFile(seedingTorrent.infoHash,file); + console.log('Already seeding ' + magnetLink); + shortenMagnetLink(event,magnetLink); + } else { + WebTorrentClient.seed(file, function (torrent) { + var magnetLink = saveBlobFile(torrent.infoHash,file); + console.log('Client is seeding ' + magnetLink); + shortenMagnetLink(event,magnetLink); + }); + } }); - } - }); + }); + } }); - }); + } }); }