【问题标题】:How to convert a jQuery filter for use with waitForKeyElements?如何转换 jQuery 过滤器以与 waitForKeyElements 一起使用?
【发布时间】:2015-11-10 19:07:34
【问题描述】:

此代码删除了转发次数少于 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();

【问题讨论】:

    标签: javascript jquery greasemonkey tampermonkey


    【解决方案1】:

    要将静态的 jQuery 过滤器转换为支持 AJAX 的 waitForKeyElements(),使用起来并不难:

    1. 您的基本选择器只是成为选择器参数。例如:
      waitForKeyElements (".js-stream-item:has(span.ProfileTweet-action--retweet)"...

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

    请注意,使用parseInt()you should always specify the base 时要避免意外(“定时炸弹”)行为。

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

    【讨论】:

    • 它工作完美,比我想象的要容易,您非常好心,帮助我如此迅速和详细,非常感谢!!!
    猜你喜欢
    • 1970-01-01
    • 2012-01-28
    • 2021-04-02
    • 2015-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2012-02-04
    • 2019-09-26
    相关资源
    最近更新 更多