记录账号和密码

自动填写网站密码

当前为 2024-07-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         记录账号和密码
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.3.2
// @description  自动填写网站密码
// @match        *://*/*
// @author       niweizhuan
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function getCurrentDomain() {
        return window.location.hostname;
    }

    function createDisplayArea() {
        var displayDiv = document.createElement("div");
        displayDiv.setAttribute("class", "jizhuDisplayArea");
        displayDiv.style.display = "none";
        displayDiv.style.position = "fixed";
        displayDiv.style.bottom = "70px";
        displayDiv.style.right = "20px";
        displayDiv.style.background = "skyblue";
        displayDiv.style.padding = "10px";
        displayDiv.style.border = "1px solid #ccc";
        displayDiv.style.zIndex = "9999";
        displayDiv.style.maxWidth = "300px";

        var closeButton = document.createElement("button");
        closeButton.textContent = "关闭";
        closeButton.style.cursor = "pointer";
        closeButton.style.backgroundColor = "transparent";
        closeButton.style.border = "none";
        closeButton.style.fontSize = "14px";
        closeButton.style.color = "#333";
        closeButton.style.marginLeft = "10px";
        closeButton.onclick = function() {
            displayDiv.style.display = "none";
        };
        displayDiv.appendChild(closeButton);

        var domainLabel = document.createElement("p");
        domainLabel.setAttribute("class", "domainLabel");
        displayDiv.appendChild(domainLabel);

        var credentialsContainer = document.createElement("div");
        credentialsContainer.setAttribute("class", "credentialsContainer");
        displayDiv.appendChild(credentialsContainer);

        var editButton = document.createElement("button");
        editButton.textContent = "编辑";
        editButton.style.cursor = "pointer";
        editButton.style.backgroundColor = "transparent";
        editButton.style.border = "none";
        editButton.style.fontSize = "14px";
        editButton.style.color = "#333";
        editButton.style.marginLeft = "10px";
        editButton.onclick = function() {
            displayDiv.style.display = "none";
            showEditPage();
        };
        displayDiv.appendChild(editButton);

        document.body.appendChild(displayDiv);
    }

    function showEditPage() {
        var currentDomain = getCurrentDomain();
        var storedData = GM_getValue(currentDomain);

        var editDiv = document.createElement("div");
        editDiv.setAttribute("class", "jizhuEditArea");
        editDiv.style.position = "fixed";
        editDiv.style.top = "50%";
        editDiv.style.left = "50%";
        editDiv.style.transform = "translate(-50%, -50%)";
        editDiv.style.background = "#fff";
        editDiv.style.padding = "20px";
        editDiv.style.border = "1px solid #ccc";
        editDiv.style.zIndex = "9999";

        var domainLabel = document.createElement("p");
        domainLabel.textContent = "此网站: " + currentDomain;
        editDiv.appendChild(domainLabel);

        var usernameInput = document.createElement("input");
        usernameInput.setAttribute("type", "text");
        usernameInput.setAttribute("placeholder", "请输入账号");
        usernameInput.value = storedData ? JSON.parse(storedData).username : "";
        editDiv.appendChild(usernameInput);

        var passwordInput = document.createElement("input");
        passwordInput.setAttribute("type", "password");
        passwordInput.setAttribute("placeholder", "请输入密码");
        passwordInput.value = storedData ? JSON.parse(storedData).password : "";
        editDiv.appendChild(passwordInput);

        var saveButton = document.createElement("button");
        saveButton.textContent = "保存";
        saveButton.style.cursor = "pointer";
        saveButton.style.backgroundColor = "#007bff";
        saveButton.style.border = "none";
        saveButton.style.color = "#fff";
        saveButton.style.padding = "10px 15px";
        saveButton.style.fontSize = "14px";
        saveButton.onclick = function() {
            var newUsername = usernameInput.value.trim();
            var newPassword = passwordInput.value.trim();

            if (!newUsername && !newPassword) {
                GM_deleteValue(currentDomain);
            } else {
                GM_setValue(currentDomain, JSON.stringify({ username: newUsername, password: newPassword }));
            }

            editDiv.style.display = "none";
            autoFillCredentials(newUsername, newPassword);
        };
        editDiv.appendChild(saveButton);

        document.body.appendChild(editDiv);
    }

    function toggleDisplay() {
        var displayDiv = document.querySelector(".jizhuDisplayArea");
        var currentDomain = getCurrentDomain();
        var storedData = GM_getValue(currentDomain);

        if (displayDiv.style.display === "block") {
            displayDiv.style.display = "none";
        } else {
            displayDiv.style.display = "block";
            displayDiv.querySelector(".domainLabel").textContent = "当前网站: " + currentDomain;

            if (storedData) {
                displayDiv.querySelector(".credentialsContainer").innerHTML = "<p>账号: " + JSON.parse(storedData).username + "</p><p>密码: " + JSON.parse(storedData).password + "</p>";
            } else {
                displayDiv.querySelector(".credentialsContainer").textContent = "暂无存储的账号和密码";
            }
        }
    }

    function autoFillCredentials(username, password) {
        var usernameField = document.querySelector('input[type="text"], input[type="email"]');
        var passwordField = document.querySelector('input[type="password"]');

        if (usernameField && passwordField) {
            usernameField.value = username;
            passwordField.value = password;
        }
    }

    function autoFillStoredCredentials() {
        var currentDomain = getCurrentDomain();
        var storedData = GM_getValue(currentDomain);

        if (storedData) {
            var credentials = JSON.parse(storedData);
            autoFillCredentials(credentials.username, credentials.password);
        }
    }

    createDisplayArea();

    var displayButton = document.createElement("button");
    displayButton.textContent = "显示账号和密码";
    displayButton.style.position = "fixed";
    displayButton.style.bottom = "20px";
    displayButton.style.right = "20px";
    displayButton.style.cursor = "pointer";
    displayButton.onclick = toggleDisplay;
    document.body.appendChild(displayButton);

    autoFillStoredCredentials();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址