mardi 9 avril 2019

Trigger file upload in web.whatsapp.com

Im setting up a local test environment with tampermonkey to send files (images) to any whatsapp contact in google chrome webbrowser programmatically (web.whatsapp.com). I'm trying to do that with javascript and wrote that piece of code so far:

(function() {
    'use strict';

    var file;
    var reader = new FileReader();
    reader.onload = function() {
        file = reader.result;
    }
    reader.readAsDataURL(new File(["abc"], "file.txt", {type:"text/plain"}));
    function getSomeFileObjFromSomewhere() {
        return file
    }

    setTimeout(function() {
        var x = document.getElementsByClassName("_2JThb")[0]; // dropable area
        console.log(x);
        x.dispatchEvent(new Event('drop', {
            dataTransfer: { files: [ file ] },
            preventDefault: function () {}
        }));
    }, 20000); // invoke after 20sec to choose contact
})();

In the example above the file "file.txt" should be prepared for sending in web.whatsapp.com - but this doesn't work so far.

What's the proper way to this?

Thanks a lot!




Aucun commentaire:

Enregistrer un commentaire