Skip to content

Instantly share code, notes, and snippets.

@emlyn
Last active April 7, 2022 10:32
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.js
// @updateURL https://gist.github.com/emlyn/979b2820eaa74fea01b8ddc1351a0f60/raw/show_password.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