Note that there are some explanatory texts on larger screens.

plurals
  1. POGreasemonkey popup loop not waiting for load-event listener
    primarykey
    data
    text
    <p>I'm writing a Greasemonkey script to automatically delete my notifications from a site, based on words I enter into a search box.</p> <p>The delete "button" is basically a link, so I'm trying to open the first link in a new tab. Then, after it loads enough, open the rest of the links, one by one, in that same tab.</p> <p>I figured out how to get the links I needed and how to loop and manipulate them. I was able to grab the first delete-link and open it in a new tab. I added an event listener to make sure the page was loaded before going to the next link.<br> I finally made that work so added my search box and button. Then I had to figure out how to wrap the whole thing in the event listener again. </p> <p>So, I now have the whole thing working, <strong>except only the last link loads.</strong><br> All links are going to my <code>waitFor</code> function so they should open, so it seems the event listener isn't working so it goes through the loop too fast and only the last link loads. </p> <p><strong>How do I make this script not continue the loop until the previous loaded page is fully loaded?</strong></p> <p>Complete code except for box and button creation:</p> <pre><code>var mytable = document.getElementById ('content').getElementsByTagName ('table')[0] var myrows = mytable.rows //function openLinkInTab () { //mywin2.close (); //} var mywin2; mywin2 = window.open ("http://www.aywas.com/message/notices/test/", "my_win2"); var links; var waitFor = function (i) { links = myrows[i].cells[1].getElementsByTagName ("a"); mywin2 = window.open (links[0].href, "my_win2"); } var delnotifs = function () { var matching; var toRemove; toRemove = document.getElementById ('find').value; alert (toRemove) for (i = 0; i &lt; 10; i++) { matching = myrows[i].cells[0].innerHTML; if (matching.indexOf (toRemove) &gt; 0) { mywin2.addEventListener ('load', waitFor (i), false); } } } searchButton.addEventListener ('click', delnotifs, true); </code></pre> <p><br> So, why isn't it waiting for <code>mywin2.addEventListener('load', waitFor(i), false);</code>? I have a feeling it's something extremely simple that I'm missing here, but I just can't see it.</p> <p>I also tried <code>mywin2.addEventListener('load', function(){waitFor(i)}, false);</code> and it still does the same thing, so it's not a problem of being a call instead of a pointer.</p> <p>Swapping <code>mywin2.addEventListener('load', waitFor(i), false);</code> for <code>if (mywin2.document.readyState === "complete") { waitFor(i)}</code> doesn't work either.</p> <p>And while I'm at it... every time I see code looping through a list like this it uses </p> <pre><code>for(i=1;i &lt; myrows.length;i++) </code></pre> <p>Which was skipping the first link in the list since arrays start at zero. So my question is, if I switch 'i' to zero, and the loop only goes while 'i' is &lt; length, doesn't that mean it won't go through the whole list? Shouldn't it be </p> <pre><code>for(i=0;i != myrows.length;i++) </code></pre> <p>Thanks!</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.
    1. This table or related slice is empty.
    1. COThe `i=1` for-loop is because for many tables, the first row (row 0), is for column headers, and usually can't contain what you are looking for. While `for(i=0;i != myrows.length;i++)` will work in many cases, it is what is called "Time bomb code". If you use it, there **will** come a time when your index overruns the array and then "Eyes melt. Skin explodes. Everybody dead".
      singulars
    2. CO@BrockAdams Yeah - array out of bounds is an pain in the butt... unfortunately, in the table I'm working with the first row isn't table headers (I don't think... because when I was just alerting the rows and was using i=1 it wasn't showing the first row), so considering I haven't played with arrays much in any language, I can't figure out whether that means I have to -1 from the length (because 0+43 isn't the same as 1+43) or whether I need to != it.
      singulars
    3. CO@BrockAdams Thanks for the edit... guess I don't need the sob story in there since I got an answer lol... and you thought that code was messy - I cleaned it up before I posted it (you should see my code when I'm working on it lol) I actually TRY to make my posts on here clear and concise, but with ADHD that's really difficult for me, so thanks for fixing it for me!
      singulars
 

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