gpt4 book ai didi

javascript - 如何允许在 Google Chrome App 的 webview 中下载?

转载 作者:行者123 更新时间:2023-11-29 19:44:49 26 4
gpt4 key购买 nike

我有一个在本地网络服务器上运行的网络应用程序,我试图通过 WebView 与之交互。我有一个链接将返回一个 PDF,其中内容配置是附件(要求系统默认的 pdf 阅读器下载并打开该文件)。

我查看了我能找到的关于处理 Chrome 应用中的权限请求的内容。在这一点上,我什至无法确认该应用程序甚至正在接收权限请求。难道我做错了什么?大概。这是什么?

这是来 self 的网络应用程序的链接。

<a href="/mypage/publication_file/8827">Download</a>

这是我的 list 。

{
"app": {
"background": {
"scripts": [ "main.js" ]
}
},
"icons": {
"128": "icon_128.png",
"16": "icon_16.png"
},
"manifest_version": 2,
"minimum_chrome_version": "30",
"name": "PED",
"permissions": [ "webview" ],
"version": "0.0.0"
}

这是我的 JS。

chrome.app.runtime.onLaunched.addListener(function() {
runApp();
});

function runApp() {
chrome.app.window.create('browser.html', {
bounds: {
'width': 1280,
'height': 768
}
});
}

var webview = document.getElementById("webview");
webview.addEventListener('permissionrequest', function(e) {
console.log("permission requested")
});

当我点击我的链接时,我没有在控制台上收到我的消息。

最佳答案

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);

关于javascript - 如何允许在 Google Chrome App 的 webview 中下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20479956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com