google-chromegoogle-chrome-extensionbrowsergoogle-chrome-devtoolschrome-devtools-protocol

Input.dispatchDragEvent doesn't drop a file in a Chrome extensoin


I have been testing the API on this webpage.

And the background script code:

let tab = {};
tab.id = 1857800479; // https://elmah.io/tools/base64-image-encoder/

chrome.debugger.attach({ tabId: tab.id }, "1.2", function () {
  chrome.debugger.sendCommand({ tabId: tab.id }, "Input.enable", {}, function () {
    let x = 500;
    let y = 500;

    var params = {
      type: 'dragEnter',
      x: x,
      y: y,
      modifiers: 0,
      data: {
        items: [],
        files: ['N:/_secure_downloads/_6f9cae77-b075-4b9a-b69f-a36fa2683b0b.jpg'],
      },
    };

    chrome.debugger.sendCommand({ tabId: tab.id }, 'Input.dispatchDragEvent', params, function () {

      params.type = 'dragOver';
      chrome.debugger.sendCommand({ tabId: tab.id }, 'Input.dispatchDragEvent', params, function () {

        params.type = 'drop';
        chrome.debugger.sendCommand({ tabId: tab.id }, 'Input.dispatchDragEvent', params, function () {

        });

      });

    }); // dragEnter

  }); // Input.enable
}); // attach

But this doesn't work. From not working I mean it doesn't log an error but the page is not appeared to be reacting to the drag and drop action.

(Note: I said "no error" but sometimes (randomly) it logs Unchecked runtime.lastError: {"code":-32602,"data":"Failed to deserialize params.data - BINDINGS: mandatory field missing at position 193","message":"Invalid parameters"} kind of error.)

(As the above note, I also tried:

 data: {
    items: [{
      mimeType: 'text/plain',
      data: btoa('Hello, world!'),
    }],
    files: ['file.txt'],
 },

instead. But it also randomly gets the error, and generally no error.)

Am I missing something? Thanks.


Solution

  • Check puppeteer - Drag and Drop Behavior on Chrome DevTools Protocol - Stack Overflow

    but only if you enable it, using Input.setInterceptDrags with enable set as true

    You need to set enabled in Input.setInterceptDrags true.