How to convert a jQuery filter for use with waitForKeyElements?

吃可爱长大的小学妹 提交于 2019-12-02 02:21:11
Brock Adams

To convert a static jQuery filter, like that, to an AJAX-aware waitForKeyElements() use is not too hard:

  1. Your base selector just becomes the selector parameter. EG:
    waitForKeyElements (".js-stream-item:has(span.ProfileTweet-action--retweet)"...

  2. The filter(function() internals transfer to the waitForKeyElements callback almost as-is. See the script, below.

Note that when using parseInt(), you should always specify the base to avoid unexpected ("time bomb") behavior.

Here's a complete script showing the port, of that filter, to 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 ();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!