记录账号和密码

自动填写网站密码

目前為 2024-07-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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