1

I am using chrome.webRequest.onBeforeSendHeaders and I want to get an output from a notification box to decide what to do with that http request (allow/deny). I tried triggering chrome rich notifications inside the onBeforeSendHeaders listener however the rich notification is asynchronous and I do not know how to get the result from it once the user enters the info, I wrote an example code below for further clarification:

chrome.webRequest.onBeforeSendHeaders.addListener(
    function(details) {
    //create a notification using chrome.notifications.create
    //get the result of that notification when it is ready and store it in var called notification_result
      if(notification_result)
          return {cancel:true};
      else {cancel:false};
    },
    {urls: ["<all_urls>"]},
    ["blocking"]);

The problem is that the notification result is triggered by an onclick listener and I do not know how to force chrome.webRequest.onBeforeSendHeaders to wait for that result. Is there a way I can make this work or achieve the same effect?

Joseph
  • 25
  • 4

1 Answers1

0

The only synchronous function that may suit your needs is confirm. It'll be ugly and hardly will satisfy anyone. The really working solutions with synchronous/blocking user confirmations are firewall applications integrated deep into OS.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • I thought this is something intuitive and should be easy to implement in a programming language like javascript. But in this case, then it seems I have no choice. I spent so much time on trying to achieve this effect with no success. I wanted to upvote but it seems there I cannot upvote yet due to reputation. Thanks for your response. – Joseph Nov 23 '15 at 21:50
  • Javascript has just a few legacy functions like `confirm`, `alert`, `prompt` that are synchronous/blocking. Nobody uses them in production (usually) except for debugging purposes. – wOxxOm Nov 23 '15 at 21:55
  • Yeah but it is weird that I cannot wait inside an event listener, for example I am facing difficulties also in loading and storing from local data, I want a function inside an event listener to continue execution only when the asynchronous load has been finished, but also I am not figuring out how to do that. – Joseph Nov 23 '15 at 21:58
  • It's not weird. Just remember that all Chrome API callbacks are asynchronous: [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321) – wOxxOm Nov 23 '15 at 22:02
  • As for accessing the data synchronously just keep a copy of data in an object/array inside the background script. – wOxxOm Nov 23 '15 at 22:05