记录账号和密码

记录网站密码信息

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         记录账号和密码
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.3.1
// @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";
        };
        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 = "暂无存储的账号和密码";
            }
        }
    }

    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);
})();