2011-05-04 46 views
2

我写了一个Greasemonkey脚本,它影响Facebook上首先加载的帖子。 但在向下滚动Feed之后,该脚本不适用于新加载的帖子。Greasemonkey脚本在Facebook上动态加载帖子上工作

有没有办法重新运行这些帖子的脚本,或类似的东西?谁能帮我?

+0

发布您的greasemonkey脚本,也许我可以帮你。这真的取决于你如何'影响'的职位。 – 2011-05-04 22:54:50

回答

7

更新:
这个问题的答案很旧,DOMSubtreeModified is deprecated
我不再推荐这种方法。相反,看到:



老答案:

是的,因为你使用的是Firefox,你可以触发关闭DOMSubtreeModified事件。

为此,首先将当前脚本的代码部分包装在一个函数中;例如:

// ==UserScript== 
// @name   Facebook Fixer 
// ==/UserScript== 

function LocalMain() 
{ 
    //--- Do all of your actions here. 
} 

LocalMain(); //-- Fire GM script once, normally. 

下,找到包含新加载的职位的节点。说你发现它是一个div与编号“All_posts_go_here”(我不使用Facebook,一定要找到正确的节点,并且不要使用body,浏览器将缓慢爬行)。

一旦确定了正确的节点,就可以设置事件侦听器。但是,您还需要很短的时间延迟,因为节点更改一次会有数百个,并且需要等到当前批处理完成。

所以,把他们放在一起,代码如下所示:

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

要小心,我假设节点不是一个iframe。如果是,则可能需要采用不同的方法。

+0

这些帖子不在IFRAME中,幸运的是。它们是LI元素,id为“stream_story_xxxxxx”,其中xxxxxx是23个字符的唯一代码,位于ID为“home_stream”的UL元素中。 – 2011-05-05 09:05:07