Password Toggler - view input passwords

Adds buttons to each input to show/hide passwords

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Password Toggler - view input passwords
// @namespace    https://github.com/pckltr/password-toggler
// @version      1.4
// @description  Adds buttons to each input to show/hide passwords
// @author       pckltr
// @match        *://*/*
// ==/UserScript==

(function () {
  "use strict";

  const showIcon =
    "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0ZWQgYnkgSWNvTW9vbi5pbyAtLT4KPCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj4KPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIxNCIgaGVpZ2h0PSIxNCIgdmlld0JveD0iMCAwIDE0IDE0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jIxMSAxLjE5OCAwIDAuNzUgMC4yMzYgMS40NDYgMC42MzggMi4wMTVsLTAuODMzIDAuODMzYy0wLjcxNy0wLjUwNC0xLjMyOS0xLjE0Ny0xLjgtMS44OTF6Ij48L3BhdGg+CjxwYXRoIGZpbGw9IiM3NTc1NzUiIGQ9Ik0xMC41IDYuMDQzYzAtMC4zNzEtMC4wNTgtMC43MjktMC4xNjUtMS4wNjVsLTQuNCA0LjRjMC4zMzYgMC4xMDcgMC42OTQgMC4xNjUgMS4wNjUgMC4xNjUgMS45MzMgMCAzLjUtMS41NjcgMy41LTMuNXoiPjwvcGF0aD4KPHBhdGggZmlsbD0iIzc1NzU3NSIgZD0iTTExLjM0OCAzLjk2NGwtMC45NDggMC45NDhjMC4wMTcgMC4wMTEgMC4wMzUgMC4wMjEgMC4wNTIgMC4wMzIgMC44MjMgMC41MjUgMS41MiAxLjIyNyAyLjA0MyAyLjA1NS0wLjUyNCAwLjgyNy0xLjIyMSAxLjUzLTIuMDQzIDIuMDU1LTEuMDM0IDAuNjU5LTIuMjI3IDEuMDA4LTMuNDUxIDEuMDA4LTAuNTI5IDAtMS4wNTEtMC4wNjUtMS41NTgtMC4xOTJsLTEuMDUxIDEuMDUxYzAuODE2IDAuMjkzIDEuNjk1IDAuNDUzIDIuNjA5IDAuNDUzIDMuMDUzIDAgNS42OTktMS43NzggNy00LjM3NS0wLjYxNi0xLjIyOS0xLjUzMy0yLjI3NS0yLjY1Mi0zLjAzNnoiPjwvcGF0aD4KPC9zdmc+Cg==";

  const buttonStyle = `
            .password-toggler-button-parent {
                position: relative;
                overflow: visible;
            }
            .password-toggler-button {
                position: absolute;
                top: 50%;
                right: 8px;
                transform: translateY(-50%);
                cursor: pointer;
                background-repeat: no-repeat;
                z-index: 9999;
                background-color: #dcdcdc;
                border-radius: 50%;
                background-position: center;
                width: 20px;
                height: 20px;
                display: none;
            }
            .password-toggler-show { background-image: url(${showIcon}); }
            .password-toggler-hide { background-image: url(${hideIcon}); }
        `;

  // append styles to the document
  const styleElement = document.createElement("style");
  styleElement.appendChild(document.createTextNode(buttonStyle));
  document.head.appendChild(styleElement);

  // create toggle button
  const createToggleButton = (input) => {
    const button = document.createElement("div");
    button.className = "password-toggler-button password-toggler-show";
    button.title = "Show password - Password Toggler";
    button.addEventListener("click", () => togglePassword(input, button));
    input.parentElement.classList.add("password-toggler-button-parent");
    input.parentElement.appendChild(button);
  };

  // toggle visibility
  const togglePassword = (input, button) => {
    const isPassword = input.type === "password";
    input.type = isPassword ? "text" : "password";
    button.className =
      "password-toggler-button " +
      (isPassword ? "password-toggler-hide" : "password-toggler-show");
    button.title =
      (isPassword ? "Hide password" : "Show password") + " - Password Toggler";
  };

  // add buttons
  const addToggleButtons = () => {
    document.querySelectorAll("input[type='password']").forEach((input) => {
      const button = input.parentElement.querySelector(
        ".password-toggler-button"
      );
      if (!button) {
        createToggleButton(input);
      } else {
        input.addEventListener("focus", () => {
          button.style.display = "inline";
        });
        input.addEventListener("blur", () => {
          if (!input.value) {
            button.style.display = "none";
          }
        });
      }
    });
  };

  // handle dynamically added password fields
  const observer = new MutationObserver(addToggleButtons);
  observer.observe(document.body, { childList: true, subtree: true });

  // initial run
  addToggleButtons();
})();