Browse Source

Merge pull request #10552 from CoryG89/webui-magnet-handler

Register protocol handler in WebUI for magnet links
adaptive-webui-19844
Mike Tzou 6 years ago committed by GitHub
parent
commit
412b51fb3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 41
      src/webui/www/private/scripts/client.js

41
src/webui/www/private/scripts/client.js

@ -696,6 +696,7 @@ window.addEvent('load', function() { @@ -696,6 +696,7 @@ window.addEvent('load', function() {
loadMethod: 'xhr',
contentURL: 'transferlist.html',
onContentLoaded: function() {
handleDownloadParam();
updateMainData();
},
column: 'mainColumn',
@ -800,8 +801,48 @@ window.addEvent('load', function() { @@ -800,8 +801,48 @@ window.addEvent('load', function() {
addMainWindowTabsEventListener();
addSearchPanel();
}
registerMagnetHandler();
});
function registerMagnetHandler() {
if (typeof navigator.registerProtocolHandler !== 'function')
return;
const hashParams = getHashParamsFromUrl();
hashParams.download = '';
const templateHashString = Object.toQueryString(hashParams).replace('download=', 'download=%s');
const templateUrl = location.origin + location.pathname
+ location.search + '#' + templateHashString;
navigator.registerProtocolHandler('magnet', templateUrl,
'qBittorrent WebUI magnet handler');
}
function handleDownloadParam() {
// Extract torrent URL from download param in WebUI URL hash
const hashParams = getHashParamsFromUrl();
const url = hashParams.download;
if (!url)
return;
// Remove the download param from WebUI URL hash
delete hashParams.download;
let newHash = Object.toQueryString(hashParams);
newHash = newHash ? ('#' + newHash) : '';
history.replaceState('', document.title,
(location.pathname + location.search + newHash));
showDownloadPage([url]);
}
function getHashParamsFromUrl() {
const hashString = location.hash ? location.hash.replace(/^#/, '') : '';
return (hashString.length > 0) ? String.parseQueryString(hashString) : {};
}
function closeWindows() {
MochaUI.closeAll();
}

Loading…
Cancel
Save