有没有人好奇为什么用浏览器打开过qb的页面之后,再点击magnet:// 链接,会自动打开你刚刚访问的qb页面。
其实本质上利用的是 url scheme协议,一般这种协议是用来打开app的。
qb的源码里有个registerMagnetHandler方法,就是通过这个实现的,用了浏览器registerProtocolHandler的api。
# 注册magnet协议处理方法
function registerMagnetHandler() {
if (typeof navigator.registerProtocolHandler !== 'function') {
if (window.location.protocol !== 'https:')
alert("要使用此功能,WebUI 需要通过 HTTPS 访问");
else
alert("您的浏览器不支持此功能");
return;
}
const hashString = location.hash ? location.hash.replace(/^#/, '') : '';
const hashParams = new URLSearchParams(hashString);
hashParams.set('download', '');
const templateHashString = hashParams.toString().replace('download=', 'download=%s');
const templateUrl = location.origin + location.pathname
+ location.search + '#' + templateHashString;
navigator.registerProtocolHandler('magnet', templateUrl,
'qBittorrent WebUI magnet handler');
}
其实本质上利用的是 url scheme协议,一般这种协议是用来打开app的。
qb的源码里有个registerMagnetHandler方法,就是通过这个实现的,用了浏览器registerProtocolHandler的api。
# 注册magnet协议处理方法
function registerMagnetHandler() {
if (typeof navigator.registerProtocolHandler !== 'function') {
if (window.location.protocol !== 'https:')
alert("要使用此功能,WebUI 需要通过 HTTPS 访问");
else
alert("您的浏览器不支持此功能");
return;
}
const hashString = location.hash ? location.hash.replace(/^#/, '') : '';
const hashParams = new URLSearchParams(hashString);
hashParams.set('download', '');
const templateHashString = hashParams.toString().replace('download=', 'download=%s');
const templateUrl = location.origin + location.pathname
+ location.search + '#' + templateHashString;
navigator.registerProtocolHandler('magnet', templateUrl,
'qBittorrent WebUI magnet handler');
}