I want to let the user choose and upload a file from the chrome extension popup. But, as soon as the file-chooser dialog opens the popup loses focus and closes immediately. From this answer, the workaround seems to be that I can move the dialog opening logic to the background-page, which is not affected by loss of focus.
I have tried the answer, but the file-chooser does not appear at all. It is weird that fileChooser.click()
event does actually occur (I was able to verify it by creating a click listener for fileChooser
). Below is a simplified version just to focus on the problem.
popup.html
<button id="uploadCSV">Upload CSV</button>
popup.js
$('#uploadCSV').click(function() {
chrome.extension.sendMessage({ action: 'browseAndUpload' });
});
background.js
var fileChooser = document.createElement('input');
fileChooser.type = 'file';
chrome.extension.onMessage.addListener(function (msg) {
if (msg.action === 'browseAndUpload') {
fileChooser.click();
}
});