如何转换 jQuery 过滤器以用于 waitForKeyElements?

How to convert a jQuery filter for use with waitForKeyElements?

此代码删除了转发次数少于 3 次的推文,但现在我遇到了刷新 (AJAX) 问题。如何添加 the waitForKeyElements function 来修复它?

$('.js-stream-item:has(span.ProfileTweet-action--retweet)').filter(function() {
    return parseInt($(this).find('span.ProfileTweet-actionCount').attr('data-tweet-stat-count')) < 3;
}).remove();

将静态 jQuery 过滤器转换为 AJAX 感知 waitForKeyElements() 使用并不难:

  1. 你的基础选择器就变成了选择器参数。例如:
    waitForKeyElements (".js-stream-item:has(span.ProfileTweet-action--retweet)"...

  2. filter(function() 内部几乎按原样转移到 waitForKeyElements 回调。请参阅下面的脚本。

请注意,在使用 parseInt() 时,you should always specify the base 可避免意外 ("time bomb") 行为。

这是一个完整的脚本,显示了那个过滤器的端口,waitForKeyElements:

// ==UserScript==
// @name     _Remove or hide nodes based on jQuery filter
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements (
    ".js-stream-item:has(span.ProfileTweet-action--retweet)", removeFilteredNode
);

function removeFilteredNode (jNode) {
    var twtCnt  = parseInt ( 
        jNode.find ('span.ProfileTweet-actionCount').attr ('data-tweet-stat-count')
        , 10
    ) 
    if (twtCnt < 3)
        jNode.remove ();
}