My Greasemonkey script starts a sequence of images loading. How do I stop it when desired?

Kat Cox

I use ImgLikeOpera and Squid Caching Proxy to manage my bandwidth while on dialup. But, I can't set it to load one image at a time, so I had the bright idea to write a script that will open each image on a page one at a time in a new tab and then close them so that they'll be saved in my cache.

Script works great, added a start button so that I could control when it started... but can't figure out how to make a stop button that will interrupt the process. I tried a bunch of stuff and nothing works...

It seems like when it's in the loop it can't hear what's going on outside the loop...

I have a feeling that there is a very simple way to do this, but I'm getting frustrated. Isn't this what break or return is supposed to be for?

Here's the relevant parts of my script:

var box = document.createElement ('div');
box.id = 'mySelectBox';
document.body.appendChild (box);

box.innerHTML = 'click>';

var searchButton = document.createElement ('div');
searchButton.className = 'mySearchButton';
searchButton.textContent = 'Search and open';

box.insertBefore (searchButton, box.nextSibling);

var stopButton = document.createElement ('div');
stopButton.className = 'myStopButton';
stopButton.textContent = 'Stop';

box.insertBefore (stopButton, box.nextSibling);

var mytable = document.getElementById ('lair-sort-pets').getElementsByTagName ('img');
var linksToOpen = [];
var mywin2 = null;

function openpics () {

    for (var J = 0, L = mytable.length; J < L; J++) {

        linksToOpen.push (mytable[J].src); //-- Add URL to list
    }

    openLinksInSequence ();
};

function openLinksInSequence () {

    if (mywin2) {
        mywin2.close ();
        mywin2 = null;
    }

    if (linksToOpen.length) {

        var link = linksToOpen.shift ();
        mywin2 = window.open (link, "my_win2");

        mywin2.addEventListener ('load', openLinksInSequence, false);

    }
}

searchButton.addEventListener ('click', openpics, true);
//stopButton.addEventListener ('click', , true);


How do I make the stop button actually stop any more links from loading?

Brock Adams

Use a global state variable. Like so:

var okayToOpenLinks = true;

searchButton.addEventListener ('click', openpics);
stopButton.addEventListener ('click', stopLinkSequence);

function openpics () {
    okayToOpenLinks = true;

    if (linksToOpen.length === 0) {
        for (var J = 0, L = mytable.length; J < L; J++) {

            linksToOpen.push (mytable[J].src); //-- Add URL to list
        }
    }

    openLinksInSequence ();
};

function stopLinkSequence () {
    okayToOpenLinks = false;
}

function openLinksInSequence () {
    if (mywin2) {
        mywin2.close ();
        mywin2 = null;
    }

    if (okayToOpenLinks  &&  linksToOpen.length) {
        var link = linksToOpen.shift ();
        mywin2 = window.open (link, "my_win2");

        mywin2.addEventListener ('load', openLinksInSequence, false);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How will I stop repeating the loading of images in my table view?

From Dev

How will I stop repeating the loading of images in my table view?

From Dev

How do I stop a signal from killing my Bash script?

From Dev

How do I stop my script from looping

From Dev

Trying to display Instagram images in carousel - how do I load the carousel only when the images are done loading?

From Dev

How do I stop slideshow of Images?

From Dev

How do I get my script to run BEFORE any shutdown sequence like unmounting my external drive

From Dev

How can I stop my python script when another python script is running?

From Dev

How can I stop my Collatz Sequence code at 1?

From Dev

My Edittext starts invisible, how do I make it editable when visible?

From Dev

How do I stop tail command in script

From Dev

How do I stop execution of bash script?

From Dev

How to stop my WPF usercontrol from loading, if i have basecontrol

From Dev

How can I run a script that starts before my login screen?

From Dev

How do i stop my css animation

From Dev

How do i stop my server in java

From Dev

How can I stop an inlined background process when my main script stops?

From Dev

How can I stop an inlined background process when my script stops?

From Dev

How can I provide a link inside my greasemonkey script to backup the GM_---value database?

From Dev

How do i stop my webpage from refreshing when i change the style:display of a div in JS?

From Dev

How do I stop Firefox from going to a previous page when I swipe my trackpad right?

From Dev

How do I seperate my desired data from the HTML tags it is enclosed in when web scraping a table with Python 3

From Dev

How to stop ROS script that starts after reboot

From Dev

How do I write a script to automatically optimize all the PNGs in my MediaWiki images directory?

From Dev

How do I build in images to my app

From Dev

How can I stop image's onloading when it's loading

From Dev

How can I play sound with a GreaseMonkey script in FireFox when there's a "Content Security Policy" error?

From Dev

How can I do a Continue option that starts the script again?

From Java

How do I make my images disappear when clicked using the code I have?

Related Related

  1. 1

    How will I stop repeating the loading of images in my table view?

  2. 2

    How will I stop repeating the loading of images in my table view?

  3. 3

    How do I stop a signal from killing my Bash script?

  4. 4

    How do I stop my script from looping

  5. 5

    Trying to display Instagram images in carousel - how do I load the carousel only when the images are done loading?

  6. 6

    How do I stop slideshow of Images?

  7. 7

    How do I get my script to run BEFORE any shutdown sequence like unmounting my external drive

  8. 8

    How can I stop my python script when another python script is running?

  9. 9

    How can I stop my Collatz Sequence code at 1?

  10. 10

    My Edittext starts invisible, how do I make it editable when visible?

  11. 11

    How do I stop tail command in script

  12. 12

    How do I stop execution of bash script?

  13. 13

    How to stop my WPF usercontrol from loading, if i have basecontrol

  14. 14

    How can I run a script that starts before my login screen?

  15. 15

    How do i stop my css animation

  16. 16

    How do i stop my server in java

  17. 17

    How can I stop an inlined background process when my main script stops?

  18. 18

    How can I stop an inlined background process when my script stops?

  19. 19

    How can I provide a link inside my greasemonkey script to backup the GM_---value database?

  20. 20

    How do i stop my webpage from refreshing when i change the style:display of a div in JS?

  21. 21

    How do I stop Firefox from going to a previous page when I swipe my trackpad right?

  22. 22

    How do I seperate my desired data from the HTML tags it is enclosed in when web scraping a table with Python 3

  23. 23

    How to stop ROS script that starts after reboot

  24. 24

    How do I write a script to automatically optimize all the PNGs in my MediaWiki images directory?

  25. 25

    How do I build in images to my app

  26. 26

    How can I stop image's onloading when it's loading

  27. 27

    How can I play sound with a GreaseMonkey script in FireFox when there's a "Content Security Policy" error?

  28. 28

    How can I do a Continue option that starts the script again?

  29. 29

    How do I make my images disappear when clicked using the code I have?

HotTag

Archive