0

I am unable to retrieve a username from a dynamic webpage using the wait function. However when I execute $("#label301354000").text(); in Chrome Developer tool, I get my username. Any idea how to tweak the function to capture the id value?

// ==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() );
}
Ura
  • 2,173
  • 3
  • 24
  • 41
  • the console log show some error? – nguaman Feb 02 '16 at 19:11
  • No error is displayed. I am able to print values from other ids but not the username. – Ura Feb 02 '16 at 19:13
  • This suggests you are waiting for the wrong node or condition. Are iframes involved? What is the exact structure of the `label301354000` node? Link to the target page. – Brock Adams Feb 02 '16 at 20:49
  • Does the username change once set? Does the function work correctly once and then stop? You need to provide more of an MCVE. – Brock Adams Feb 02 '16 at 20:50

1 Answers1

0

The question omits critical details, see the comments above.

But in general, if $("#label301354000").text(); always works and

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

Comes up empty in the Tampermonkey script, it suggests that the target node is not the best choice or that the node is created at some point and filled in by the page later.

waitForKeyElements checks the callback return for cases like that. Change getUser() to:

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

If that doesn't work, provide a proper MCVE and/or link to the target page. (Per SO guidelines, you should always provide one or both.)

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295