使用jquery和greasemonkey获取多个页面

发布于 2024-08-22 21:40:53 字数 783 浏览 2 评论 0原文

抱歉,标题不太好,这是一个负载问题。 我想做的是从页面上的链接加载多个页面(它们位于同一主机上)。就我而言,它是线程视图中的论坛,我想加载帖子的最后一页,以便有某种预览。

为了简单起见,我的代码示例假设 jquery 已经加载。

基本上代码正在工作,但是如果有很多多个请求,页面就会挂起,即 - 如果我将条件更改为 intIndex < 3个或更多。

    $('.lastposter').each(
        function (intIndex){
            if(intIndex < 2){
                var threadlink = $('a', this)[1].href;
                var thread = $.ajax({ type: "GET", url: threadlink, async: false }).responseText;

                $(this.parentNode.parentNode).attr('title', $('.post', thread).text());

            }
        }
    );

那么我的问题是,有没有更好的方法来做到这一点?我想我只需要在循环中进行某种暂停,或者更改它以便ajax请求发生在(例如)行的悬停事件上。 另外,结果会被缓存吗?如果没有,有没有办法让它们缓存,或者我应该把它留给服务器?我想将它们存储为 GM 变量(GM_setValue),但也许这会变得混乱和/或泄漏。

如果您有一些想法、建议、示例等,我很乐意听到。

Sorry the title isn't great, it's sort of a loaded question..
What I am trying to do is load multiple pages from links on a page (they are on the same host). In my case it is a forum in the thread view, and I'd like to load the last page of posts so there's some kind of preview.

To keep it simple, my code example assumes jquery is already loaded.

Basically the code is working, however if there's many multiple requests, the page just hangs, ie - if I change the condition to intIndex < 3 or more.

    $('.lastposter').each(
        function (intIndex){
            if(intIndex < 2){
                var threadlink = $('a', this)[1].href;
                var thread = $.ajax({ type: "GET", url: threadlink, async: false }).responseText;

                $(this.parentNode.parentNode).attr('title', $('.post', thread).text());

            }
        }
    );

So then my loaded question is, is there a better way to do this? I was thinking I just need some kind of pause in the loop, or maybe change it so the ajax request happens on (for example) the hover event of the row.
Also, will the results be cached? If not, is there a way to make them cached or should I just leave it up to the server? I was thinking to store them as a GM variable (GM_setValue), but maybe that would get messy and or leaky.

If you have some ideas, suggestions, examples etc, I'd be happy to hear about them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙与沫 2024-08-29 21:40:53

您的代码的问题是,它请求一个异步请求。这导致 $.ajax 函数等待服务器应答。

对于异步请求,您需要定义一个回调函数,如下所示:

$.get(threadlink, function(thread) {  
    $(this.parentNode.parentNode).attr('title', $('.post', thread).text());  
});

要逐个处理链接,而不是一堆同时请求,您可以捕获数组中的节点 (makeArray) 并以特定速率一次 pop 一个 (setTimeout/setInterval)。请注意,greasemonkey 这里有一些陷阱...

结果将像任何其他页面一样被缓存。 IE 不会在过期日期之前检索它。也许您必须更改服务器缓存/过期响应标头。

看看这个,听起来很相似(但不使用jquery)

http://userscripts.org/脚本/show/19613

The problem with your code is, that it requests a non-asynchrous request. This results in the $.ajax function waiting for the servers answer.

For an asynchrous request, you'll need to define a callback function, like this:

$.get(threadlink, function(thread) {  
    $(this.parentNode.parentNode).attr('title', $('.post', thread).text());  
});

To process the links one by one, instead of a bunch of simulaneous requests, you could capture the nodes in an array (makeArray) and pop one at a time at a specific rate (setTimeout/setInterval). Note that greasemonkey got some pitfalls here...

Results will be cached mostly like any other page. IE won't retrieve it before expiration date. Maybe you have to alter the servers cache/expire response headers.

Take a look at this, it sounds similar (but does not use jquery)

http://userscripts.org/scripts/show/19613

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文