Greasemonkey脚本在Twitter上隐藏ajax内容

Greasemonkey script to hide AJAXed content on Twitter

本文关键字:隐藏 ajax 内容 Twitter 脚本 Greasemonkey      更新时间:2023-09-26

我正试图隐藏Twitter上所有受保护的账户,但运气不好。当然,代码在其他具有不同类的页面上工作,就像在Stack Overflow中一样。

它与Ajax有关,但是我如何在Ajax完成后触发它?

// ==UserScript==
// @name          Hide twitter locked accounts
// @namespace     
// @description   I want to rule the world
// @include       *
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

$(document).ready(function() {
$('.stream-item-content:has(.protected-icon)').hide();
});

首先我要让循环更容易读:

for (var n = images.length -1 ; n >=0; n--) {

img.style.display = "none";

隐藏图像

另一种方法是使用css选择器禁用所有图像元素。您可以使用jQuery和greasemonkey

来完成此操作。
$('img').css({ 'display': 'none'});

编辑:这将创建一个样式元素,它将匹配所有的img标签,即使它是随后用ajax加载的。

var styleTag = document.createElement('style');
styleTag.type = "text/css";
styleTag.appendChild(document.createTextNode("img {display:none;}");
document.body.appendChild(styleTag);

做到这一点的最健壮的方法是使用有效的间隔计时器轮询页面。这在实践中也比拦截和破译AJAX调用容易得多。

下面是一个脚本,使用我的实用函数"waitForKeyElements":
// ==UserScript==
// @name    Twitter: Hide protected accounts
// @include http://twitter.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==
function HideProtectedAccounts (jNode) {
    jNode.hide();
}
waitForKeyElements ( 
    ".stream-item-content:has(.protected-icon)", 
    HideProtectedAccounts
);
/*--- waitForKeyElements():  A handy, utility function that
    does what it says.
*/
function waitForKeyElements (
    selectorTxt,    /* Required: The jQuery selector string that
                        specifies the desired element(s).
                    */
    actionFunction, /* Required: The code to run when elements are
                        found. It is passed a jNode to the matched
                        element.
                    */
    bWaitOnce,      /* Optional: If false, will continue to scan for
                        new elements even after the first match is
                        found.
                    */
    iframeSelector  /* Optional: If set, identifies the iframe to
                        search.
                    */
)
{
    var targetNodes, btargetsFound;
    if (typeof iframeSelector == "undefined")
        targetNodes     = $(selectorTxt);
    else
        targetNodes     = $(iframeSelector).contents ()
                                           .find (selectorTxt);
    if (targetNodes  &&  targetNodes.length > 0) {
        /*--- Found target node(s).  Go through each and act if they
            are new.
        */
        targetNodes.each ( function () {
            var jThis        = $(this);
            var alreadyFound = jThis.data ('alreadyFound')  ||  false;
            if (!alreadyFound) {
                //--- Call the payload function.
                actionFunction (jThis);
                jThis.data ('alreadyFound', true);
            }
        } );
        btargetsFound   = true;
    }
    else {
        btargetsFound   = false;
    }
    //--- Get the timer-control variable for this selector.
    var controlObj      = waitForKeyElements.controlObj  ||  {};
    var controlKey      = selectorTxt.replace (/[^'w]/g, "_");
    var timeControl     = controlObj [controlKey];
    //--- Now set or clear the timer as appropriate.
    if (btargetsFound  &&  bWaitOnce  &&  timeControl) {
        //--- The only condition where we need to clear the timer.
        clearInterval (timeControl);
        delete controlObj [controlKey]
    }
    else {
        //--- Set a timer, if needed.
        if ( ! timeControl) {
            timeControl = setInterval ( function () {
                    waitForKeyElements (    selectorTxt,
                                            actionFunction,
                                            bWaitOnce,
                                            iframeSelector
                                        );
                },
                200
            );
            controlObj [controlKey] = timeControl;
        }
    }
    waitForKeyElements.controlObj   = controlObj;
}



指出:

  1. 这是为主推特网站,对吗?如果是一个带有iFramed twitter内容的页面,请调整传递给waitForKeyElements的参数以匹配。

  2. 关键的可调项是动作函数(HideProtectedAccounts,在这种情况下)和CSS选择器(s)传递给waitForKeyElements

Twitter使用AJAX带来一切-文本和图像…因此,您需要在AJAX加载运行后运行代码-这是如何在AJAX- request提供了一个函数之后加载一个油脂猴子脚本....

根据@rlemon对另一个问题的评论:

我如何拦截xmlhttprequest从一个Greasemonkey脚本?