I'm trying to write a userscript what is logging me in automatically on a site I'm using daily.
I want the script to check first if I am logged in or not, and execute the rest only if I'm logged out.
var content = document.body.textContent;
var hasText = content.indexOf("Login")!==-1;
if(hasText){
document.getElementById("useremail").value = "mymail@gmail.com";
document.getElementById("userpassword").value = "mypassword123";
}
My code is almost working, it has only the problem it is looking for the "login" text in the whole source code. Which usually is there, even if I am logged in. I want the searching part to look only in the nav (menu). How could I do that?
If I'm logged out: one of the menu (nav) listing (li) is "Login". If I'm logged in, that menu is showing my username (so no Login in the nav part of the site).
And the source of the site looks like:
<body>
<section id="main-header" class="headersclass">
<div class="container">
<header>
<div class="menusdiv">
<nav class="mainmenu">
<ul>
<li class="menulisting">
<li class="menulisting">
<li class="menulisting">
</ul>
</nav>
</div>
</header>
</div>
</section>
...random text of the site...
...the word "Login" can appear anywhere...
</body>
I've tried changing my script's first line to the next ones, but always error:
var content = document.body.nav.textContent;
var content = document.body > nav.textContent;
var content = document.body.section.div.header.div.nav.textContent;
var content = document.querySelectorAll('nav').textContent;
var content = document.getElementsByTagName("nav").textContent;
var lookhere = document.getElementsByTagName("nav");
var content = lookhere.textContent;
You need to use this way:
var content = document.getElementsByClassName("mainmenu")[0].textContent;
Or, like how you have tried accessing:
var content = document.getElementsByTagName("nav")[0].textContent;
These functions return a HTMLNodeList
, which is like an array, you need to get the first element.