使用jquery和greasemonkey获取多个页面
抱歉,标题不太好,这是一个负载问题。 我想做的是从页面上的链接加载多个页面(它们位于同一主机上)。就我而言,它是线程视图中的论坛,我想加载帖子的最后一页,以便有某种预览。
为了简单起见,我的代码示例假设 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码的问题是,它请求一个非异步请求。这导致 $.ajax 函数等待服务器应答。
对于异步请求,您需要定义一个回调函数,如下所示:
要逐个处理链接,而不是一堆同时请求,您可以捕获数组中的节点 (
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:
To process the links one by one, instead of a bunch of simulaneous requests, you could capture the nodes in an array (
makeArray
) andpop
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