【问题标题】:How to allow downloads inside webview in Google Chrome App?如何允许在 Google Chrome App 中的 webview 内下载?
【发布时间】:2013-12-09 20:21:29
【问题描述】:

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

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

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

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

这是我的清单。

{
  "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")
});

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

【问题讨论】:

  • 我假设您的 JS 清单中的最后 4 行代码在 browser.html 窗口上下文中,而不是在 main.js 中?
  • 不,这一切都在 main.js 中。我没有想到浏览器上下文无法访问 main.js。我会尝试移动一些东西。感谢您的提示:-)
  • 你有没有设法解决这个问题?

标签: javascript google-chrome-app google-chrome-webview


【解决方案1】:
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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 2016-03-18
  • 1970-01-01
  • 2013-08-21
相关资源
最近更新 更多