我试图在没有运气的情况下隐藏Twitter中所有受保护的帐户。代码正在处理其他具有不同类别的页面,例如Stack Overflow中的这些。
它与Ajax有关,但是如何在Ajax完成后触发它?
// ==UserScript==
// @name Hide twitter locked accounts
// @namespace http://www.multitopia.com
// @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();
});
答案 0 :(得分:1)
首先,我会让循环更具可读性:
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);
答案 1 :(得分:0)
Twitter使用AJAX引入所有内容 - 文本和图像......所以你需要在AJAX加载运行后运行代码 - 这个答案How to load a greasemonkey script after AJAX-Request提供了这样做的功能....
根据@rlemon关于另一个例子的评论:
How can I intercept XMLHttpRequests from a Greasemonkey script?
答案 2 :(得分:0)
最强大的方法是使用有效的间隔计时器轮询页面。在实践中,这比拦截和解密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;
}
注意:
这是主Twitter网站,对吧?如果是包含iFramed twitter内容的页面,请调整传递给waitForKeyElements
的参数以匹配。
键可调节是动作功能(在本例中为HideProtectedAccounts
)和传递给waitForKeyElements
的CSS选择器。