Closed Bug 1439165 Opened 7 years ago Closed 7 years ago

WebExtensions: browser.downloads.open - not open from content script

Categories

(WebExtensions :: General, defect)

58 Branch
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 1398833

People

(Reporter: pag77, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0 Build ID: 20180206200532 Steps to reproduce: manifest.json ... "content_scripts": [{ "js": [ "test.js" ], "matches": [ "<all_urls>" ], "all_frames" : true }], ... test.js ------------ 1. ddata_box.addEventListener("click", function(event) { browser.downloads.open(event.target.id); }, true); -------- 2. ddata_box.addEventListener("click", function(event) { chrome.runtime.sendMessage({ 'action' : 'file_open', 'id' : event.target.id }, function(response) {}); }, true); background.js chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { ... chrome.downloads.open(request.id); ... }); Actual results: 1. it's normal TypeError: browser.downloads is undefined 2. GoogleChrome works correctly and file is opened, but Firefox WebExtensions gives this error: Error: downloads.open may only be called from a user input handler Expected results: To use this function in your extension... ... you can only call this function from inside the handler for a user action, such as * clicking the extension's browser action or page action * selecting its context menu item * activating a keyboard shortcut defined by the extension (note: this is not currently supported in Firefox) * clicking a button in a page bundled with the extension. https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/downloads/open I'm porting my extension to WebExtensions: show downloads in statusbar The feature of this extension is that it draws a statusbar with downloads at the bottom of each web page. Tell me please - how to open a file by passing a message from content_script to background.js? For the Google Chrome version there are no such restrictions for "downloads.open", but suddenly I ran into limitations in Firefox. why did you add restrictions to "downloads.open" in WebExtensions? :(
Status: UNCONFIRMED → RESOLVED
Closed: 7 years ago
Component: Untriaged → WebExtensions: General
Product: Firefox → Toolkit
Resolution: --- → DUPLICATE
Even while the issue is marked as "RESOLVED", it persists in Firefox 59.0.3 (64-bit). The root cause appears to be a loss of context information when a UI-triggered callback (such as "addEventListener") in a content script forwards to a background script via a "chrome.runtime.sendMessage" call, exactly as shown in the example highlighted in the bug report. The background script then sees the action as if it had not originated from a user action and throws the exception "Error: downloads.open may only be called from a user input handler". The current setup (as far as I can see) disallows "chrome.downloads.open" to be invoked in background script, which can only communicate with other scripts (e.g. content scripts) with the message passing infrastructure. Meanwhile, the same script runs without problems in Google Chrome, where the fact that the action had originally come from a user action is propagated via "chrome.runtime.sendMessage".
Product: Toolkit → WebExtensions
You need to log in before you can comment on or make changes to this bug.