2

I have a use case where I have to programmatically attach attachments to a Gmail compose window using a chrome extension. inboxSDK has APIs to do this but I do not want to include a huge library just for this one function.

I tried doing this by simulating a drag and drop event on the compose window. I create a drop event this way:

            var fakeDropEvent = new DragEvent('drop');
            Object.defineProperty(fakeDropEvent, 'dataTransfer', {
              value: {files: [blob]}
            });

I am giving it a dataTransfer property that has files property with an array of Blob objects as value. But dispatching this event on the compose window does not do anything. When I tried to catch a normal drop event (one that was manually triggered by a user), I noticed its event.dataTransfer.files has a FileList object whereas, for the fake event that I am making, I return an array of blob objects. I am guessing this is the issue that I am facing. Gmail is expecting a FileList but does not get it. I tried to create a FileList object myself but was unsuccessful. Is there any way something like this can be achieved. I tried to read what inboxSDK is doing but their code is closed source and it is nearly impossible to understand their uglified code.

revant
  • 234
  • 3
  • 13
  • 1
    Content scripts run in isolated world so you need to [Insert code into the page context using a content script](//stackoverflow.com/a/9517879) – wOxxOm Dec 11 '18 at 14:21

1 Answers1

0

This is because your newly created Event has its isTrusted property set to false. The browser reads this property and does nothing when you try to dispatch the event to your target node.