Reputation: 67
This piece of code should send a message in WhatsApp Web, on entering it to the JavaScript console, but it always says undefined
.
How can I prevent this?
function whatsAppText(t) {
window.InputEvent = window.Event || window.InputEvent;
var d = new Date();
var event = new InputEvent('input', {bubbles: true});
var textbox = document.querySelector('#main > footer > div.block-compose > div.input-container > div > div.input');
textbox.textContent = t;
textbox.dispatchEvent(event);
document.querySelector('.icon.btn-icon.icon-send.send-container').click()
}
Upvotes: 2
Views: 616
Reputation: 65806
The console will report the returned value of an expression. If a function does something meaningful, but does not return anything, then executing the function returns nothing, hence undefined
.
Here is an example:
function foo(){
document.body.innerHTML = "<p>I did some work, but I didn't return a value.</p>";
}
console.log(foo()); // undefined
As opposed to:
function foo(){
return "I did some work, and I did return a value.";
}
console.log(foo()); // "I did some work, and I did return a value."
But, if all you are doing is entering the function into your console and not even calling it, that won't return anything and the console will report undefined
. Based on the code you've shown, even if you then invoke your function with, say: whatsAppText("test")
, your function will execute and send the message, but you don't have your function set up to return anything, so you'll still see undefined
.
Upvotes: 2
Reputation: 791
That's because you are only defining a function and not executing it. You have to call the function somewhere after it's been declared:
whatsAppText('This should work!');
Upvotes: 0