2

How do I extract the value 2083236893 from the following object using JavaScript?

<div class="gwt-Label">2083236893</div>

I installed Greasemonkey 3.17 for Firefox 52.2.1 (32-bit) and tested every code example provided and failed.

var html = document.getElementsByClassName("gwt-Label")[0];
alert(html.innerHTML);

The above example comes from: Accessing Div, by class name, with javascript.

Should the code be run on full download of the web page?

Appended:

var elements = document.getElementsByClassName("gwt-Label")[0];
alert(elements.innerHTML);

the above may fail if

elements.length = 0

It may be the case the document has not been loaded in full or query result = 0 - no DIV object contains class name from query string

Firebug can generate XPath to the selected object (innerHTML or innerTEXT) in the document (HTML version in your browser) to let you verify if the class name from the query is correct and exists.

Setting greater value for timeout may let HTML document to be loaded fully to make your script run via Greasemonkey or alike add-on to give correct results via

console.log(HTMLCollection.length) 

see above.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
darius
  • 29
  • 1
  • 8

2 Answers2

2

class="gwt-Label" in the HTML, strongly implies that the page is driven by Google Web Toolkit -- which means the page is AJAX driven and static techniques, like in some other answers, will not work.

Use AJAX aware techniques, like waitForKeyElements(). Here's a complete script that will print gwt-Label values to the browser console. (Ctrl Shift I opens said console.) :

// ==UserScript==
// @name     _Print gwt-Label text
// @match    *://YOUR_SERVER.COM/YOUR_PATH/*
// @match    https://stacksnippets.net/js*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.

waitForKeyElements (".gwt-Label", printNodeText);

function printNodeText (jNode) {
    console.log ("gwt-Label value: ", jNode.text ().trim () );
}

This will print each label, if there is more than one.

Note that waitForKeyElements takes jQuery selectors.



You can test the above script by installing it using Greasemonkey, Tampermonkey, Violentmonkey, or similar, then running this snippet:

var lblbNum = 0;
function addLableLine () {
    lblbNum++;
    $("body").append(`<div class="gwt-Label">New label: ${lblbNum}</div>`);
}
setInterval (addLableLine, 1333);  // New line every 1.3 seconds.
.gwt-Label {display: inline-block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gwt-Label">2083236893</div>

You will then see:

gwt-Label value:  2083236893
gwt-Label value:  New label: 1
gwt-Label value:  New label: 2
etc...

in the browser console.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thank you for your excellent answer. It would take me another 2 months to find the correct answer. BTW, I run your script either in Greasemonkey or in console. I get: ReferenceError: waitForKeyElements is not defined [Learn More] , GM calculated no value for gwt-Label – darius Dec 26 '17 at 03:53
  • It won't run from the console as is. But it ***does*** run from Greasemonkey/Tampermonkey. Install the script above **exactly as is**, but change the `@match` line to your website. It will work. If not, provide a link to your target page. – Brock Adams Dec 26 '17 at 03:56
  • Updated the script to run, exactly as is, for a simulated ajax page in the answer. If it doesn't run in your target page (after changing the match line), then something you haven't told us is the problem. Link to your target page. – Brock Adams Dec 26 '17 at 04:35
  • No value generated in Greasemonkey. Script created in Tampermonkey, enabled, "no script is running" from menu option – darius Dec 26 '17 at 04:41
  • "no script is running" on what page? If it's your target page, then you've not set the `@match` right. Give us a link to the target page. If it's *this* page, make sure you installed the script exactly as above. Then you won't see it shown as running until you click the "Run code snippet" button. – Brock Adams Dec 26 '17 at 04:45
  • You were exactly right. Alert failed to handle large number of gwt-Label classes. Console.log worked fine for GM. Now I need to learn how to limit the output, selecting specific array index only. What comes next is looping the script over updated @ match . Not sure GM or TM support dynamic @ match in a loop. Great job done. Thank you. You are the Best!!! – darius Dec 26 '17 at 04:58
  • Tried really hard to find "Run code snipped" button in TM. Opening dashboard I have your script selected as enabled and other possible actions are to edit it or to delete it. v.4.5.5660 – darius Dec 26 '17 at 05:20
  • That button is not a Tampermonkey thing. That was only to activate the special test code, on this page. See https://i.stack.imgur.com/isunq.png – Brock Adams Dec 26 '17 at 05:31
  • Oh, I see. I just reloaded this page to stop the code looping without limit. Since your code generated 100+ lines in console - jNode.text ().trim () I need to open a new question how to select only few instances of gwt-Label and assigned values. – darius Dec 26 '17 at 05:55
  • Yes, you would need a new question for that. – Brock Adams Dec 26 '17 at 06:02
1

If you wish to use .text() in jQuery that may be easier!

Check out this link: http://api.jquery.com/text/

This should select your text:

$(window).load(function(){
    $(".gwt-Label").text();
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Edward
  • 309
  • 1
  • 8
  • Thank you. The above code works fine in Fiddle https://fiddle.jshell.net/tfyssyqb/ fails in Greasemonkey. BTW, why the code is executed before I click the button ? – darius Dec 26 '17 at 03:15
  • Since I don't see jQuery, you can use `.textContent`, vanilla JS version of it. Yes it is not supported by IE8 and below, but should not matter much – Rajesh Dec 26 '17 at 04:37
  • are you ready for a challenge ? I would like to extract b lock header data from this place http://srv1.yogh.io/#mine:height:0 and save to file, incrementing height from 0 to up and up. – darius Dec 27 '17 at 00:33
  • Can I ask a question ? – darius Jan 12 '18 at 00:55