Browse Source

Register protocol handler in WebUI for magnet links

adaptive-webui-19844
Cory 6 years ago
parent
commit
d49379789a
  1. 41
      src/webui/www/private/scripts/client.js

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

@ -677,6 +677,7 @@ window.addEvent('load', function() {
loadMethod: 'xhr', loadMethod: 'xhr',
contentURL: 'transferlist.html', contentURL: 'transferlist.html',
onContentLoaded: function() { onContentLoaded: function() {
handleDownloadParam();
updateMainData(); updateMainData();
}, },
column: 'mainColumn', column: 'mainColumn',
@ -781,8 +782,48 @@ window.addEvent('load', function() {
addMainWindowTabsEventListener(); addMainWindowTabsEventListener();
addSearchPanel(); 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() { function closeWindows() {
MochaUI.closeAll(); MochaUI.closeAll();
} }

Loading…
Cancel
Save