我的Greasemonkey脚本启动一系列图像加载。如何在需要时停止它? [英] My Greasemonkey script starts a sequence of images loading. How do I stop it when desired?

查看:91
本文介绍了我的Greasemonkey脚本启动一系列图像加载。如何在需要时停止它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ImgLikeOpera和Squid Caching Proxy在拨号时管理我的带宽。但是,我无法将其设置为一次加载一个图像,所以我有一个好主意,就是编写一个脚本,在一个新页面中一次打开一个页面上的每个图像,然后关闭它们以便它们'将保存在我的缓存中。

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?

推荐答案

使用全局状态变量。像这样:

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);
    }
}

这篇关于我的Greasemonkey脚本启动一系列图像加载。如何在需要时停止它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆