0

I am working on a Firefox Addon that needs to read a text file. However, I am getting the error " picker was blocked due to lack of user activation.", which I understand is because I am doing it programmatically.

This is how I set the FileReader in the contentscript.js

var fileChooser = document.createElement('input');
fileChooser.type = 'file';
fileChooser.addEventListener('change', function () {
    const file = event.target.files[0];
    var fr = new FileReader();
    fr.onload = function (file) {
      console.log(file.target.result)
    };
    fr.readAsText(file);
});

Then it gets called when the user clicks on a button on the popup.html and sends a message to the contentscript.js which triggers the click

browserInUser.runtime.onMessage.addListener((obj, sender, sendResponse) => {
    const { type, value } = obj;
    if (type === "upload") {
        console.log("Listening")
        fileChooser.click();
    }
});

This gets called and it gets printed "Listening" but also the error described above. This is one of the attempts I tried to programmatically open a FileReader dialog from an addon, but it seems like I am not going to be able to do this programmatically.

I have been trying to achieve this for almost two weeks now, but I am about to give up. This is really easy to do in Chrome, but it seems to be a nightmare on Firefox.

Is this approach the wrong one? Is this even possible to do on Firefox? Please, even if you cannot help me with a "successful" answer, I need to know if this is even possible from an addon.

ehtio
  • 179
  • 3
  • 11

0 Answers0