2

我有一个运行在本地 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")
});

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

4

1 回答 1

3
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);
于 2015-07-03T14:12:19.943 回答