Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot trigger a click from XMLHttpRequest
    primarykey
    data
    text
    <p>I wrote a similar post about this but it was closed for being too localized. After some research and testing, I think I found the source of the problem, and this question should be better explained and hopefully more helpful.</p> <p>Using XMLHttpRequest I need to go to a page, simulate a mouse click, then get some contents. Here is the code I wrote:</p> <pre><code>var sendToLog; var xhr = new XMLHttpRequest(); xhr.open('GET', '/newsfeed-simulator', true); xhr.responseType = 'document'; xhr.onload = function(e) { if (this.status == 200) { var event = xhr.response.createEvent("MouseEvents"); event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); xhr.response.getElementById("simulate-button").dispatchEvent(event); $(xhr.response).ready(function() { console.log(xhr.response.body); sendToLog = xhr.response.getElementsByClassName("story-score"); }); chrome.extension.sendMessage({writeToLog: sendToLog}); } } xhr.send(); </code></pre> <p>Problem is click event does not work. Tried it also <code>xhr.response.getElementById("simulate-button").click();</code> to no avail.</p> <p>Not sure what is the problem? It should be a straight forward afair: <strong>go to a page, wait for it to load, click a button, wait for it to load again, and then get contents from DOM.</strong></p> <p>Any help would be appreciated it. </p> <p>Thank you in advance </p> <p><strong>EDIT</strong> removed second onload, and added xhr.response. Now it prints the DOM without the mouse click to the console. Still not what I needed but maybe it is a step in the right direction</p> <p><strong>EDIT2</strong>: According to <a href="https://stackoverflow.com/questions/8801570/simulate-a-click-on-a-tag-in-chrome">this</a> post and others, it's better not to use click since it only works in IE. So I edited my code but till doesn't work...bummer</p> <p><strong>EDIT3</strong> As Fabien Quatravaux, the problem here is that the ajax request response is not loaded into the current DOM, so I cannot call out the event (well I can but it won't do anything). So the question is how can I using xmlhttprequest, go to a page using, fire an event and then get the resulting DOM?? Thank you again Fabien for pointing this out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload