警報火災不止一次因爲該網頁包含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);
}
}