问题
I'm trying to send a chat message in Facebook using Javascript, but keep getting an error message. Either it being TypeError: Object #<NodeList> has no method 'WHATEVERIPUTHERE'
Basically o
= the chat text area.
The o.WHATEVERIPUTHERE("Hello!")
is what I'm trying to do (setting a value for the text area, and sending it)
This is what I have tried:
var o = document.getElementsByClassName("uiTextareaAutogrow _552m");
o.WHATEVERIPUTHERE("Hello!");
回答1:
document.getElementsByClassName
returns an array of elements. In this case they will have same number with the chat boxes opened.
I wrote it in pure Javascript:
var o = document.getElementsByClassName("uiTextareaAutogrow _552m");
// set the chat textbox
var chatTextBox = o[0];
// set the message value
chatTextBox.value = "Hello";
// create a keydown event
var e = new Event("keydown");
// it has to simulate the Enter press (key code is 13)
e.keyCode = 13;
// trigger it
chatTextBox.dispatchEvent(e);
回答2:
As of 31-12-2016 the structure has changed... I haven't achieved it completely because I'm stuck with the sending part. Because for some reason when I'm entering the dynamic value, FB is blocking me from pressing Enter or back..The behaviour is weird.
Here's my piece of code [Not complete]
//Open up all the chat window visible on the chat panel
STEP 1:
javascript:var inputs = document.getElementsByClassName('_55ln');
for(var i=0; i<inputs.length ; i++) {
inputs[i].click();
break; //testing purpose
}
STEP 2:
javascript:var item = document.getElementsByClassName('fbNubFlyout fbDockChatTabFlyout uiContextualLayerParent');
for(var k=0;k<item.length;k++){
var child = item[k].getElementsByClassName('_1p1t');
item[k].removeChild(child);
var send = item[k].getElementsByClassName('_1mf _1mj');
console.log(send);
for(var j=0;j<send.length; j++){
var attr = send[j].getAttribute('data-offset-key');
var new_content = '<span data-offset-key="'+attr+'"><span data-text="true">HI!! Testing</span></span>';
send[j].innerHTML=new_content;
}
}
回答3:
Awesome...it's working for me. Below is my code
var o = document.getElementsByClassName("uiTextareaNoResize uiTextareaAutogrow _1rv");
// set the chat textbox
var chatTextBox = o[0];
// set the message value
chatTextBox.value = "Hello";
// create a keydown event
var e = new Event("keydown");
// it has to simulate the Enter press (key code is 13)
e.keyCode = 13;
// trigger it
chatTextBox.dispatchEvent(e);
来源:https://stackoverflow.com/questions/18371703/sending-a-chat-message-in-facebook-using-javascript-using-the-browser-console