swift - WKWebView:如何处理 BLOB URL
问题描述
WKWebView
当我单击文件下载它时,我有我的。我有一个弹出式写作:
没有应用程序配置为打开 URL
blob:https// ...
我尝试将自定义 URL 方案注册blob
到WKWebView
,应用程序崩溃说默认情况下已经支持此方案。
但是,当我单击文件时,不会调用委托:
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)`
所以我什至不知道何时单击 blob url 以尝试通过注入 JavaScript 来下载文件。
我的应用程序适用于 macOS。
解决方案
请注意,这是一个非常迂回的黑客攻击。扫描所有 href 并用 datauri 替换检测到的任何 blob url。编辑:更新以显示它正在运行
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function(e) {callback(e.target.result);}
a.readAsDataURL(blob);
}
// not sure what elements you are going to intercept:
document.querySelectorAll('a').forEach(async (el)=>{
const url = el.getAttribute('href');
if( url.indexOf('blob:')===0 ) {
let blob = await fetch(url).then(r => r.blob());
blobToDataURL(blob, datauri => el.setAttribute('href',datauri));
}
});
b=new Blob([new Int8Array([1,2,3,4,5,6,7,8,9,10]).buffer]);
test.href=URL.createObjectURL(b);
b=new Blob([new Int8Array([31,32,33,34,35]).buffer]);
test1.href=URL.createObjectURL(b);
b=new Blob([new Int8Array([51,52,53,54]).buffer]);
test2.href=URL.createObjectURL(b);
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function(e) {callback(e.target.result);}
a.readAsDataURL(blob);
}
document.addEventListener('click', function(event) {
event.preventDefault();
if ( event.target.matches('a[href^="blob:"]') )
(async el=>{
const url = el.href;
const blob = await fetch(url).then(r => r.blob());
blobToDataURL(blob, datauri => el.href=datauri);
})(event.target);
});
// not sure what elements you are going to intercept:
/*document.querySelectorAll('a').forEach(async (el)=>{
const url = el.href;
if( url.indexOf('blob:')===0 ) {
let blob = await fetch(url).then(r => r.blob());
blobToDataURL(blob, datauri => el.href=datauri);
}
});*/
<a id="test">test</a>
<a id="test1">test</a>
<a id="test2">test</a>
点击时数据 uri 转换示例:
b=new Blob([new Int8Array([1,2,3,4,5,6,7,8,9,10]).buffer]);
test.href=URL.createObjectURL(b);
b=new Blob([new Int8Array([31,32,33,34,35]).buffer]);
test1.href=URL.createObjectURL(b);
b=new Blob([new Int8Array([51,52,53,54]).buffer]);
test2.href=URL.createObjectURL(b);
function blobToDataURL(blob, callback) {
var a = new FileReader();
a.onload = function(e) {callback(e.target.result);}
a.readAsDataURL(blob);
}
document.addEventListener('click', function(event) {
if ( event.target.matches('a[href^="blob:"]') ) {
event.preventDefault();
(async el=>{
const url = el.href;
const blob = await fetch(url).then(r => r.blob());
blobToDataURL(blob, datauri => window.open(el.href=datauri,el.target||'_self'));
})(event.target);
}
});
// not sure what elements you are going to intercept:
/*document.querySelectorAll('a').forEach(async (el)=>{
const url = el.href;
if( url.indexOf('blob:')===0 ) {
let blob = await fetch(url).then(r => r.blob());
blobToDataURL(blob, datauri => el.href=datauri);
}
});*/
<a id="test">test</a>
<a id="test1">test</a>
<a id="test2">test</a>
推荐阅读
- windows - poolmon负数是什么意思?
- java - 无法从 JSF Java Web 应用程序插入 Oracle 数据库
- angular - Keycloak:Angular 6 将 XMLHttpRequest 发送到 SpringBoot 后端的 CORS 问题
- css - 以角度显示播放/暂停按钮
- java - Spring在拦截器中向重定向的URL添加额外的查询参数
- amazon-web-services - 是否可以仅验证启用 MFA 的 AWS Cognito 用户的密码?
- javascript - 如何使 JavaScript regexbb 查找缺少 html 属性
- python - 如何检查值是 nan 还是列表?
- c# - 项目引用错误版本的 DLL
- android - 安卓。如何跳转到路径中带有“$”符号的系统活动?