警报火灾不止一次因为该网页包含iFrame中(有,事实上,同样的网址为主体页)。 Greasemonkey将iFrame视为独立网页。使用@noframes
停止。
该脚本没有找到链接,因为它们是在页面加载和GM脚本激发后很长时间内通过JavaScript添加的。这是脚本和AJAX常见的问题。一个简单而强大的解决方案是使用waitForKeyElements()
(和jQuery)。
这里是一个完整的示例脚本避免I帧,并演示了如何获取动态链接:
// ==UserScript==
// @name _Find elements added by AJAX
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @match http://stackoverflow.com/questions/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @noframes
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
var totalUsrLinks = 0;
waitForKeyElements ("a[href*='/users/']", listLinks);
function listLinks (jNode) {
var usrMtch = jNode.attr ("href").match (/^.*\/users\/(\d+)\/.*$/);
if (usrMtch && usrMtch.length > 1) {
totalUsrLinks++;
var usrId = usrMtch[1];
console.log ("Found link for user: ", usrId, "Total links = ", totalUsrLinks);
}
}