Question:
I want to write and post a comment using javascript on instagram. But the problem is that he does not want to go.
Wrote code like this:
//В форму добавляем комментарий
document.getElementsByClassName('Ypffh')[0].textContent = "Это комментарий!";
//Ждем пару секунд
setTimeout(function() {
//убираем disabled из формы отправки
document.querySelector("#react-root > section > main > section > div:nth-child(1) > div > article:nth-child(1) > div.eo2As > section.sH9wk._JgwE > div > form > button").disabled = true;
//нажимаем отправить
document.querySelector("#react-root > section > main > section > div:nth-child(1) > div > article:nth-child(1) > div.eo2As > section.sH9wk._JgwE > div > form > button").click();
}, 2000);
But instagram is not responding. Why is instagram comment not being sent with javascript? (might be missing something)
Answer:
I puzzled for a long time, but still found a solution. Works in mobile version.
Each element that React "owns" has a __reactInternalInstance$XXXXXX
method. You can find a lot of interesting things in it, including props and element handlers. We will refer to onChange
. First, click on the comment button. Then we extract this method from the textarea
( __reactInternalInstance$XXXXXX
), then find onChange
in its props and pass it an "artificial" event object {target:{value:"Это коммент"}}
. React will think that we are writing something and will do everything for us. The button will be activated and we will click on it. Go to the page with the post and execute the code. If something is not clear – ask.
window.FindReact = function(dom) {
for (var key in dom) {
if (key.startsWith("__reactInternalInstance$")) {
return dom[key];
}
}
return null;
};
document.querySelector("#react-root > section > main > div > div:nth-child(1) > article:nth-child(1) > div > section > span:nth-child(2) > button").click();
setTimeout(() => {
FindReact(document.querySelector("#react-root > section > main > div > div:nth-child(1) > article:nth-child(1) > div > section > div > form > textarea")).memoizedProps.onChange({
target: {
value: "Это коммент"
}
});
document.querySelector("#react-root > section > main > div > div:nth-child(1) > article:nth-child(1) > div > section > div > form > button").click();
}, 1000);