How to allow downloads inside webview in Google Chrome App?

I have web app running on a local web server that I am trying to interact with via a webview. I have link that will return a PDF where content-disposition is attachment (it is a requirement that the file be downloaded and opened by the system default pdf reader).

I have looked through what I can find on dealing with permission requests in Chrome apps. At this point I can't even get an acknowledgement that the app is even receiving a permission request. Am I doing something wrong? Probably. What is it?

Here is the link from my web app.

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

Here is my manifest.

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

Here is my 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")
});

When I click on my link I don't get my message on the console.

Answers


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

Need Your Help

How to retrieve data with offset and limit using webservice request?

axapta x++ dynamics-ax-2009

I am trying to build an AX service to retrieve data from InventTable in Microsoft Dynamics AX 2009.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.