检测何时使用jQuery加载新推文?
你知道当你滚动到个人资料页面的底部时(我说的是用户的个人页面,而不是时间轴),会自动加载新的推文。 我正在编写一个UserScript,我想在每次加载这些新推文时执行一个函数。
但我无法找到一种方法来检测这个新的tweets加载事件。 我假设它是一个AJAX请求,对吧? 所以我尝试了以下两个function但无济于事。
$(document).ajaxComplete(function() { alert("Complete! New tweets loaded!"); }); $(document).ajaxSuccess(function() { alert("Success! New tweets loaded!"); });
这是我到目前为止所拥有的。 这在首先加载页面时有效。 但它不会影响向下滚动后加载的推文。
// ==UserScript== // @name CoolScript // @include https://twitter.com/IJNanayakkara // @include https://twitter.com/IJNanayakkara/status/* // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @version 1.0 // @license GPL v3 or any later version (http://www.gnu.org/copyleft/gpl.html) // @grant GM_addStyle // ==/UserScript== $.each($('p.js-tweet-text'), function() { $(this).prepend("Brought to you by "); });
如何检测从jQuery加载新推文?
编辑:我注意到另一个问题。 导航到页面时,脚本不会运行。 我实际上必须刷新页面。 我搜索并找到了这个答案 。 我将.js文件导入脚本,但我不知道我应该等待哪个元素。
如何在不手动刷新的情况下运行脚本?
如果,你的代码是这样的:
$.each($('p.js-tweet-text'), function() { $(this).prepend("Brought to you by "); });
处理页面的静态位,或从命令控制台,然后它转换为此waitForKeyElements
代码:
waitForKeyElements ("p.js-tweet-text", prependToTweet); function prependToTweet (jNode) { jNode.prepend("Brought to you by "); }
简单:选择器保持不变,并且.each()
代码直接端口,除了$(this)
被jNode
替换。
waitForKeyElements
自动处理AJAXed元素(适用于向下滚动而无需刷新)。