Search code examples
javascriptif-statementuserscripts

Run user script only if page has a password field


I have a short piece of JavaScript code that I'm using as a user script. The action works fine, but I want to add if-statement to it and don't know how to structure it.

In plain language, I want it to do this:

  • If there is a password input field on the page, then do not run my action.

or alternately

  • If there is no password input field on this page, then run my action.

How is this accomplished? I'm fiddling with document.querySelectorAll but so far no luck.


Solution

  • You can use document.querySelector to check for the existence.

    if (!document.querySelector('input[type=password]')) {
        // your code here
    }