WKWebView:如何处理 BLOB URL
WKWebView: How to handle BLOB URL
当我点击一个文件下载它时,我有我的 WKWebView
。我有一个弹出窗口写作:
No application configured to open the URL blob:https// ...
我尝试将自定义 URL 方案 blob
注册到 WKWebView
,但应用程序崩溃并提示该方案已被默认支持。
但是,当我点击文件时,并没有调用委托:
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)`
所以我什至不知道什么时候单击 blob url 以尝试通过注入 JavaScript.
下载文件
我的应用程序适用于 macOS。
请注意,这是一个非常迂回的 hack。扫描所有 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>
当我点击一个文件下载它时,我有我的 WKWebView
。我有一个弹出窗口写作:
No application configured to open the URL
blob:https// ...
我尝试将自定义 URL 方案 blob
注册到 WKWebView
,但应用程序崩溃并提示该方案已被默认支持。
但是,当我点击文件时,并没有调用委托:
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)`
所以我什至不知道什么时候单击 blob url 以尝试通过注入 JavaScript.
下载文件我的应用程序适用于 macOS。
请注意,这是一个非常迂回的 hack。扫描所有 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>