无法检索 AJAXed 内容

Unable to retrieve AJAXed content

我无法使用等待功能从动态网页检索用户名。但是,当我在 Chrome 开发人员工具中执行 $("#label301354000").text(); 时,我得到了我的用户名。知道如何调整函数以捕获 id 值吗?

// ==UserScript==
// @name         UI
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Test
// @author       Dejan
// @match        http://url/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.2.0
// @require      https://gist.githubusercontent.com/raw/2625891/waitForKeyElements.js
// @grant        GM_addStyle
// ==/UserScript==
/* jshint -W097 */
'use strict';

waitForKeyElements("#label301354000", getUser);
function getUser(jNode) {
   console.log("User is "+ jNode.text() );
}

问题省略了关键细节,请参阅上面的评论。

但在 general 中,如果 $("#label301354000").text(); always 有效并且

function getUser(jNode) {
   console.log("User is "+ jNode.text() );
}

在 Tampermonkey 脚本中显示为空,这表明目标节点不是最佳选择,或者该节点是在某个时候创建​​的,稍后由页面填充。

waitForKeyElements 检查回调 return 是否存在此类情况。将 getUser() 更改为:

function getUser(jNode) {
    var usrText = jNode.text ().trim ();
    if (usrText) {
        console.log ("User is " + usrText);
    }
    else
        return true;  //-- Keep waiting.
}

如果这不起作用,请向目标页面提供适当的 MCVE and/or link。 (根据 SO 指南,您应该始终提供其中之一或两者。)