--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/87003bda-de2c-417b-89cb-adf0365ee801n%40chromium.org.
sendResponse
in an asynchronous callback (like the reader.onload
), you need to indicate to the browser that the listener will respond asynchronously by returning true
from the onMessage
listener.chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "openFileChooser") {
var fileChooser = document.createElement("input");
fileChooser.type = 'file';
fileChooser.addEventListener('change', function (evt) {
var f = evt.target.files[0];
if (f) {
var reader = new FileReader();
reader.onload = function (e) {
var contents = e.target.result;
console.log(contents);
sendResponse({ success: true, data: contents });
document.body.removeChild(fileChooser); // Clean up
};
reader.onerror = function (e) {
console.error("Error reading file", e);
sendResponse({ success: false, error: "Failed to read file" });
document.body.removeChild(fileChooser); // Clean up
};
reader.readAsText(f);
}
});
document.body.appendChild(fileChooser);
fileChooser.click();
}
return true;
});
--