双击显示密码

双击显示密码,失去焦点隐藏.不覆盖 onload,支持密码框后生成的网站

当前为 2020-10-24 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         双击显示密码
// @namespace    https://github.com/Tyrone2333/display-password
// @version      1.2
// @description  双击显示密码,失去焦点隐藏.不覆盖 onload,支持密码框后生成的网站
// @author       en20
// @include      http*://*
// @exclude      *baidu.com*
// @grant        none
// @run-at		 document-end
// ==/UserScript==
(function () {
    function displayPassword() {
        let list = document.querySelectorAll("input[type=password]")
        for (let i = 0; i < list.length; i++) {
            let item = list[i]
            item.ondblclick = function () {
                item.setAttribute("type", "text")
            }
            item.onblur = function () {
                item.setAttribute("type", "password")
            }
        }
    }

    // 防止覆盖 onload 事件
    function addLoadEvent(func) {
        const oldOnload = window.onload
        if (typeof window.onload != 'function') {
            window.onload = func
        } else {
            window.onload = function () {
                oldOnload()
                func()
            }
        }
    }

    // 防抖,mutationObserver 触发非常频繁
    function debounce(fn, delay) {
        // console.log(args)
        let timer = null

        return function (...args) {

            clearTimeout(timer)

            timer = setTimeout(() => {

                fn.call(this, ...args)

            }, delay)
        }
    }

    // 监听每次 dom 变化,重新寻找密码框添加事件
    const mutationObserver = new MutationObserver(debounce((mutations) => {
        displayPassword()
        // mutations.forEach(function (mutation) {
        //     console.log(mutation.addedNodes)
        // })
    }, 300))

    // 开始监听页面根元素 HTML 变化。
    mutationObserver.observe(document.documentElement, {
        attributes: true,
        characterData: true,
        childList: true,
        subtree: true,
        attributeOldValue: true,
        characterDataOldValue: true,
    })

    addLoadEvent(displayPassword)

})()