I am working on a Chrome Extension. I want to add a file to a webpage which has a dropzone on it.
This can be done for example in the developers console with
document.body.dropzone.addfile(file);
I have learnt that extensions do not have access to variables such as in this case the dropzone object. So I thought I could solve this problem by following the many examples like this one where I use a custom event. My code is still very similar;
In my content.js
//!!!!we are in a frame so doc is needed. Not using main document.
var s = doc.createElement('script');
s.src = chrome.extension.getURL('CustomEvent.js');
(doc.head || doc.documentElement).appendChild(s);
s.onload = function () {
s.remove();
};
// Event listener
doc.addEventListener('RW759_connectExtension', function (e) {
// e.detail contains the transferred data (can be anything, ranging
if (!e.detail) {
console.log('addFile - failed to get the e.detail object.');
return // this is where I end up no detail object.
}
e.detail.addFile(upFile);
CustomEvent.js
setTimeout(function () {
console.log('in the time out');
let dz = document.body.dropzone;
if (!dz) {
console.log('addFile - failed to get the dropzone object.');
}
/* Example: Send data from the page to your Chrome extension */
document.dispatchEvent(new CustomEvent('RW759_connectExtension', {
detail: dz
}));
}, 10);
In the timeoutfunction I can get the dropzone object. The event is dispatched. Content.js receives the event, but detail is null. To check everything is wired up right I changed detail to a string and my content.js gets the string ok.
So the problem is that I cannot pass an object. Does anyone know why?
Alternatively can I pass my file to the webpage some other way, to the script or in my customevent.js which is injected in the page?