Skip to content

Instantly share code, notes, and snippets.

@emlyn
Last active September 28, 2024 22:16
Show Gist options
  • Save emlyn/979b2820eaa74fea01b8ddc1351a0f60 to your computer and use it in GitHub Desktop.
Save emlyn/979b2820eaa74fea01b8ddc1351a0f60 to your computer and use it in GitHub Desktop.
Show Passwords with double click
// ==UserScript==
// @name Show Passwords
// @description Double click on password fields to show password
// @downloadURL https://gist.github.com/emlyn/979b2820eaa74fea01b8ddc1351a0f60/raw/show_password.user.js
// @updateURL https://gist.github.com/emlyn/979b2820eaa74fea01b8ddc1351a0f60/raw/show_password.user.js
// @namespace https://gist.github.com/emlyn
// @version 0.3
// @author Emlyn Corrin
// @match http*://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const KEY_ENTER = 13;
const updateEl = function(el) {
el.addEventListener('dblclick', () => {
if (el.type === 'password') {
el.type = 'text';
} else {
el.type = 'password';
}
}, false);
el.addEventListener('blur', () => {
el.type = 'password';
}, false);
el.addEventListener('keydown', e => {
if (e.keyCode === KEY_ENTER) {
el.type = 'password';
}
}, false);
};
const done = new WeakSet();
const updateAll = function() {
document.querySelectorAll('input[type=password]').forEach(el => {
if (!done.has(el)) {
updateEl(el);
done.add(el);
}
});
};
updateAll();
const observer = new MutationObserver(updateAll);
observer.observe(document.documentElement, {
childList: true,
subtree: true,
// In case element added as text then changed to password:
attributes: true,
attributeFilter: ['type']
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment