Greasemonkey脚本在Facebook上动态加载的帖子 [英] Greasemonkey script to work on dynamically loaded posts on Facebook
问题描述
,但在您向下滚动Feed之后,脚本在新加载的帖子上不起作用。
有没有办法重新运行脚本那些帖子,还是这样的?任何人都可以帮助我吗?
更新:
这个问题和答案很旧, DOMSubtreeModified
已被弃用。
我不再推荐这种方法。相反看看:
- AJAX请求中的Fire Greasemonkey脚本
- 在同一页上运行Greasemonkey脚本多次?
- 在AJAX驱动的站点上选择和激活正确的控件
- 或使用
MutationObserver
通过图书馆,如变异摘要,或类似的。
旧答案
是的,由于您使用的是Firefox,因此您可以触发 DOMSubtreeModified
事件。
为此,首先将当前脚本的代码部分包含在函数中;例如:
// == UserScript ==
// @name Facebook Fixer
// == / UserScript ==
function LocalMain()
{
// ---在这里完成所有的操作。
}
LocalMain(); // - 一般通用脚本一次。
接下来,找到包含新加载的节点帖子。假设你发现它是一个 div
,其中id All_posts_go_here(我不使用Facebook,一定要找到正确的节点,并且不要使用 body
,浏览器将缓慢爬行)。
一旦你确定了正确的节点,可以设置事件监听器。但是,您也需要一个很短的时间延迟,因为节点更改一次数百次,您需要等到当前批次完成。
所以,把它全部一起,代码如下所示:
if(window.top!= window.self)//不运行框架或iframe
return;
函数LocalMain()
{
// ---在此处执行所有操作。
}
LocalMain(); // - 一般通用脚本一次。
var PostsChangedByAJAX_Timer ='';
// ---更改下一行找到正确的元素;样品显示。
var PostContainerNode = document.getElementById('All_posts_go_here');
PostContainerNode.addEventListener(DOMSubtreeModified,PageBitHasLoaded,false);
函数PageBitHasLoaded(zEvent)
{
/ * ---设置和重置一个计时器,以便我们运行我们的代码(LocalMain())只有
添加了最后一个帖子 - 在一批中。调整时间,如果需要,但
半秒是一个很好的全面价值。
* /
if(typeof PostsChangedByAJAX_Timer ==number)
{
clearTimeout(PostsChangedByAJAX_Timer);
PostsChangedByAJAX_Timer ='';
}
PostsChangedByAJAX_Timer = setTimeout(function(){LocalMain();},555);
}
当心我假设节点不是iframe。如果是,则可能需要另外的方法。
I wrote a Greasemonkey script and it affect the firstly loaded posts on Facebook. but after you scroll down on the feed, the script doesn't work on the newly loaded posts.
Is there a way to re-run the script for those posts, or something like that? can anyone help me?
Update:
This question and answer are very old and DOMSubtreeModified
is deprecated.
I no longer recommend this approach. Instead see:
- Fire Greasemonkey script on AJAX request
- Run Greasemonkey script on the same page, multiple times?
- Choosing and activating the right controls on an AJAX-driven site
- Or use
MutationObserver
via a library like Mutation Summary, or similar.
Old answer:
Yes, since you are using Firefox, you can trigger off the DOMSubtreeModified
event.
To do this, first wrap the code part of your current script in a function; for example:
// ==UserScript==
// @name Facebook Fixer
// ==/UserScript==
function LocalMain ()
{
//--- Do all of your actions here.
}
LocalMain (); //-- Fire GM script once, normally.
Next, find the node that contains the newly loaded posts. Say that you find that it is a div
with the id "All_posts_go_here" (I don't use Facebook, be sure to find the correct node, and do not use body
, the browser will slow to a crawl).
Once you've identified the correct node, you can set the event listener. But, you also need a short time delay because the node changes come hundreds at a time and you need to wait until the current batch is done.
So, putting it all together, the code looks like this:
if (window.top != window.self) //don't run on frames or iframes
return;
function LocalMain ()
{
//--- Do all of your actions here.
}
LocalMain (); //-- Fire GM script once, normally.
var PostsChangedByAJAX_Timer = '';
//--- Change this next line to find the correct element; sample shown.
var PostContainerNode = document.getElementById ('All_posts_go_here');
PostContainerNode.addEventListener ("DOMSubtreeModified", PageBitHasLoaded, false);
function PageBitHasLoaded (zEvent)
{
/*--- Set and reset a timer so that we run our code (LocalMain() ) only
AFTER the last post -- in a batch -- is added. Adjust the time if needed, but
half a second is a good all-round value.
*/
if (typeof PostsChangedByAJAX_Timer == "number")
{
clearTimeout (PostsChangedByAJAX_Timer);
PostsChangedByAJAX_Timer = '';
}
PostsChangedByAJAX_Timer = setTimeout (function() {LocalMain (); }, 555);
}
Beware that I'm assuming the node is not an iframe. If it is, then a different approach may be required.
这篇关于Greasemonkey脚本在Facebook上动态加载的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!