Search code examples
javascriptgreasemonkey

Getting a text value in class with Greasemonkey


sorry I am newbie about javascript and greasemonkey so that question is too easy maybe,

I want to get a text value which is change by time, here is related part of code, I need to get text value of "THE TEXT I WANT TO GET";

    <div class="mautoclass>
     <a href="url" data-toggle="tooltip" data-placement="top" title="" class="text-decoration-none text-reset balance" data-original-title="Title">
       <h4 class="mclass">
         <i class="sync">
         </i> 
         "THE TEXT I WANT TO GET"
       </h4>
    </a>
   </div>

Solution

  • Based on your example code, if there is only ONE element with that class="mautoclass" and it only has ONE text node, then for example the following would get that text.

    const text = document.querySelector('.mautoclass').textContent;
    

    You may need to trim() the spaces.

    If there are more elements with that class or there are more text nodes, then the code has to be adjusted accordingly.