Javascript Greasemonkey脚本隐藏Twitter上的Ajax内容
我正试图隐藏Twitter上所有受保护的帐户,但运气不好。当然,这些代码在其他具有不同类的页面上工作,就像这里的堆栈溢出一样 它与Ajax有关,但在Ajax完成后如何启动它Javascript Greasemonkey脚本隐藏Twitter上的Ajax内容,javascript,jquery,greasemonkey,Javascript,Jquery,Greasemonkey,我正试图隐藏Twitter上所有受保护的帐户,但运气不好。当然,这些代码在其他具有不同类的页面上工作,就像这里的堆栈溢出一样 它与Ajax有关,但在Ajax完成后如何启动它 // ==UserScript== // @name Hide twitter locked accounts // @namespace // @description I want to rule the world // @include * // @require
// ==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);
Twitter使用AJAX带来一切——文本和图像。。。因此,您需要在AJAX加载运行之后运行代码——这个答案提供了一个函数来实现这一点 根据@rlemon对问题的评论,另一个例子是:
最可靠的方法是使用有效的间隔计时器轮询页面。这在实践中也比拦截和破译AJAX调用容易得多 下面是一个使用我的实用程序函数“WaitForkEyements”实现此功能的脚本:
注:
waitForKeyElements
的参数以匹配WaitForkKeyments
的CSS选择器也许是因为Twitter的AJAX特性?ie上面的代码在图像实际加载之前运行。如果你是说界面元素图像,大多数是CSS中的
背景图像,而不是
标签,尽管配置文件图片是
sSee this->@ManseUK谢谢我现在就看谢谢你,但是问题是,我想在Ajax请求后启动脚本。对于css解决方案,我必须添加+1,但我有点像原始的循环构造函数。我已经用我正试图做的事情更新了我的问题。我试图让它与给出的例子一起工作,但什么都没有..只是等待DOM准备好,而不是ajax加载。。。更新了我的答案以包含另一个示例
// ==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;
}