如何转换jQuery过滤器以使用waitForKeyElements

How to convert a jQuery filter for use with waitForKeyElements?

本文关键字:过滤器 waitForKeyElements jQuery 何转换 转换      更新时间:2023-09-26

这段代码删除了转发少于3次的tweet,但是现在我有刷新(AJAX)问题。我如何添加waitForKeyElements函数来修复它?

$('.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()时,您应该始终指定基,以避免意外的("定时炸弹")行为。

这是一个完整的脚本显示了该过滤器到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 ();
}