I am trying to develop an import function for my chrome extension, but this task seems more difficult than expected.
Actually, my idea is to use
<input type="file" id="myInput">
Download the file, and then add the change listener to this element:
element.addEventListener('change', function(evt){ read_single_file(evt,tab); }
Now there are a few issues that I am facing:
- The first problem is that the pop-up window closes when you open the open dialog box and causes the destruction of all related objects and code using the tooltip. And looking at another question, this is the normal behavior of the chrome extension, when it has lost focus, the popup will be destroyed.
I found another solution offering to add file logic to the background page, which will not be destroyed if the pop-up window loses focus. Then I added this function in the background:
file_import = function(element, tab) { element.addEventListener('change', function(evt){ read_single_file(evt,tab); }, false); element.click(); console.log("uffa"); }
And then I updated my popup.js to call the background method:
get_current_tab(function(tab){ var BGPage = chrome.extension.getBackgroundPage(); BGPage.file_import(document.getElementById('myInput'), tab); });
And so the import_file is called by the popup, it adds a change listener to the myInput element and opens the file open dialog, but ... the popup window opens the dialog with the file, and everything connected with it, then ... the same problem again.
So, I decided to try to create a new input file element on the background page and trigger a click event from it, but obviously it does not work.
So, I'm stuck here, not having good ideas on how to solve this problem. Or at least I don’t have much, but I don’t know if they work, or they just get by.
- One of them is to move the import function to the devtools area (since this extension is useful for the developer, it makes sense), where I hope that the open file dialog will not destroy my extension.
- Or maybe, maybe, move the logic of the import file to an external page that opens a new tab and imports the values from the files. In this case, since I only need access except for the contents of the file, this is the current bookmark URL, maybe I don’t need to use the Google Chrome API.
- The third idea is to insert the html element on the current page, and then access it from the extension, adding a listener directly to this element, in which case I do not think that my listener is destroyed if the pop-up window (the page will reload immediately after import, therefore, my hidden input will remain only for the time required for the operation).
To summarize, the following questions:
- There is a clean way to read the contents of a file inside the chrome extension, without having to use external files, open new tabs, etc. (if possible, I prefer to store everything inside the extension)?
- If it is not possible to pre-populate a new tab using a JavaScript file?
- Can I add a listener to DOM elements inside the current page?
javascript google-chrome google-chrome-extension
Ivan
source share