常常寫JS下載一堆東西,但user還要自己手動點選:「允許下載多個檔案」
能下載時候熱血沸騰,但是,要點這傢伙就讓人乾掉了。
因此今天的筆記就是能夠使用JS跳過此步驟。
var webview = null;
function isSafeUrl(url) {
// You may want to perform a more rigorous check.
// There's a technique that creates an <a> to parse the URI, but that seems
// like a security risk.
return !!url.match(/^(?:ftp|https?):\/\//i);
}
function onPermissionRequest(event) {
switch (event.permission) {
case 'download':
if (isSafeUrl(event.request.url)) {
event.request.allow();
} else {
event.request.deny();
}
break;
// You can add code for other permissions here.
}
}
function onDomReady() {
webview = document.getElementById('webview');
webview.addEventListener('permissionrequest', onPermissionRequest);
}
document.addEventListener('DOMContentLoaded', onDomReady);