记录网站密码信息并自动填充
目前為
// ==UserScript==
// @name 记录账号和密码
// @version 0.4.1
// @description 记录网站密码信息并自动填充
// @author niweizhuan
// @match *://*/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM_addStyle
// @grant unsafeWindow
// @license MIT
// @namespace https://bbs.tampermonkey.net.cn/
// ==/UserScript==
(function() {
'use strict';
// 是否开启自动填充功能
var autoFillkey = true;
// 当前页数
var currentPage = 0;
// 当前显示区域的div
var displayDiv;
// 获取当前域名
var currentDomain = getCurrentDomain();
// 获取当前页面的域名
function getCurrentDomain() {
return window.location.hostname;
}
var prevButton;
var nextButton;
// 获取存储的数据
function getStoredData(domain) {
var data = GM_getValue(domain);
return data ? JSON.parse(data) : [];
}
// 设置存储的数据
function setStoredData(domain, data) {
GM_setValue(domain, JSON.stringify(data));
}
// 创建显示区域
function createDisplayArea() {
if (displayDiv) {
document.body.removeChild(displayDiv);
}
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 = "5px";
displayDiv.style.border = "1px solid #ccc";
displayDiv.style.zIndex = "2147483647";
displayDiv.style.maxWidth = "250px";
displayDiv.style.fontSize = "14px";
// 顶部按钮区域
var topButtons = document.createElement("div");
topButtons.style.display = "flex";
topButtons.style.justifyContent = "space-between";
topButtons.style.marginBottom = "5px";
// 关闭按钮
topButtons.appendChild(createButton("关闭", function() {
displayDiv.style.display = "none";
}));
// 编辑按钮
topButtons.appendChild(createButton("编辑", function() {
displayDiv.style.display = "none";
showEditPage();
}));
// 新建按钮
topButtons.appendChild(createButton("新建", function() {
displayDiv.style.display = "none";
showEditPage({}, true);
}));
displayDiv.appendChild(topButtons);
// 当前网站显示区域
var domainContainer = document.createElement("div");
domainContainer.style.display = "flex";
domainContainer.style.alignItems = "center";
domainContainer.appendChild(createLabel("当前网站:"));
domainContainer.appendChild(createValue(currentDomain));
displayDiv.appendChild(domainContainer);
// 账号信息显示区域
var accountContainer = document.createElement("div");
accountContainer.setAttribute("class", "accountContainer");
displayDiv.appendChild(accountContainer);
// 页面控制按钮区域
var pageControls = document.createElement("div");
pageControls.style.display = "flex";
pageControls.style.justifyContent = "center";
pageControls.style.marginTop = "5px";
// 上一页按钮
var prevButton = createButton("<", function() {
if (currentPage > 0) {
currentPage--;
updateAccountDisplay();
}
});
pageControls.appendChild(prevButton);
// 页面标签
var pageLabel = document.createElement("span");
pageLabel.setAttribute("class", "pageLabel");
pageLabel.style.margin = "0 10px";
pageControls.appendChild(pageLabel);
// 下一页按钮
var nextButton = createButton(">", function() {
var storedData = getStoredData(currentDomain);
if ((currentPage + 1) * 1 < storedData.length) {
currentPage++;
updateAccountDisplay();
}
});
pageControls.appendChild(nextButton);
displayDiv.appendChild(pageControls);
document.body.appendChild(displayDiv);
updateAccountDisplay();
}
// 创建按钮
function createButton(text, clickHandler) {
var button = document.createElement("button");
button.textContent = text;
button.style.cursor = "pointer";
button.style.backgroundColor = "transparent";
button.style.border = "none";
button.style.fontSize = "inherit";
button.style.color = "#333";
button.style.margin = "0 5px";
button.style.padding = "3px 5px";
button.onclick = clickHandler;
button.ontouchend = clickHandler;
return button;
}
// 创建标签
function createLabel(text) {
var label = document.createElement("p");
label.textContent = text;
label.style.margin = "0";
return label;
}
// 创建数值显示
function createValue(text) {
var value = document.createElement("p");
value.textContent = text;
value.style.margin = "0 5px";
return value;
}
// 创建无信息文本
function createNoInfoText(text) {
var noInfoText = document.createElement("p");
noInfoText.textContent = text;
noInfoText.style.margin = "0";
return noInfoText;
}
// 创建复制按钮
function createCopyButton(text, copyText) {
var button = document.createElement("button");
button.textContent = text;
button.style.cursor = "pointer";
button.style.backgroundColor = "transparent";
button.style.border = "none";
button.style.fontSize = "inherit";
button.style.color = "#333";
button.style.marginLeft = "5px";
button.style.padding = "3px 5px";
button.onclick = function() {
copyTextToClipboard(copyText, button);
};
button.ontouchend = button.onclick;
return button;
}
// 复制文本到剪贴板
function copyTextToClipboard(text, button) {
navigator.clipboard.writeText(text).then(function() {
var originalText = button.textContent;
button.textContent = "已复制";
setTimeout(function() {
button.textContent = originalText;
}, 2000);
}).catch(function(err) {
console.error('复制失败:', err);
});
}
// 更新账号信息显示
function updateAccountDisplay() {
var storedData = getStoredData(currentDomain);
var accountContainer = displayDiv.querySelector(".accountContainer");
accountContainer.innerHTML = "";
if (storedData.length > 0) {
var accountInfo = storedData[currentPage];
var accountRow = document.createElement("div");
accountRow.style.display = "flex";
accountRow.style.justifyContent = "space-between";
accountRow.appendChild(createLabel("账号:"));
accountRow.appendChild(createCopyButton("复制", accountInfo.username));
accountContainer.appendChild(accountRow);
accountContainer.appendChild(createValue(accountInfo.username));
var passwordRow = document.createElement("div");
passwordRow.style.display = "flex";
passwordRow.style.justifyContent = "space-between";
passwordRow.appendChild(createLabel("密码:"));
passwordRow.appendChild(createCopyButton("复制", accountInfo.password));
accountContainer.appendChild(passwordRow);
accountContainer.appendChild(createValue(accountInfo.password));
fillCredentials(accountInfo.username, accountInfo.password); // 这里进行填充
} else {
accountContainer.appendChild(createLabel("账号:"));
accountContainer.appendChild(createNoInfoText("[无账号信息]"));
accountContainer.appendChild(createLabel("密码:"));
accountContainer.appendChild(createNoInfoText("[无密码信息]"));
}
displayDiv.querySelector(".pageLabel").textContent = (currentPage + 1) + "/" + Math.ceil(storedData.length / 1);
prevButton.style.display = currentPage > 0 ? "inline" : "none";
nextButton.style.display = (currentPage + 1) * 1 < storedData.length ? "inline" : "none";
}
// 显示编辑页面
function showEditPage(accountInfo = {}, isNew = false) {
var editDiv = document.createElement("div");
editDiv.setAttribute("class", "jizhuEditArea");
editDiv.style.position = "fixed";
editDiv.style.bottom = "70px";
editDiv.style.right = "20px";
editDiv.style.background = "white";
editDiv.style.padding = "10px";
editDiv.style.border = "1px solid #ccc";
editDiv.style.zIndex = "2147483647";
editDiv.style.maxWidth = "250px";
editDiv.style.fontSize = "14px";
editDiv.innerHTML = `
<p><label>账号:<input type="text" id="editUsername" value="${accountInfo.username || ''}" style="width: 100%;"></label></p>
<p><label>密码:<input type="password" id="editPassword" value="${accountInfo.password || ''}" style="width: 100%;"></label></p>
<div style="display: flex; justify-content: space-between; margin-top: 10px;">
<button id="cancelEdit">取消</button>
<button id="saveEdit">${isNew ? '创建' : '保存'}</button>
${!isNew ? '<button id="deleteEntry">删除</button>' : ''}
</div>
`;
document.body.appendChild(editDiv);
document.getElementById("cancelEdit").onclick = function() {
document.body.removeChild(editDiv);
};
document.getElementById("saveEdit").onclick = function() {
var username = document.getElementById("editUsername").value.trim();
var password = document.getElementById("editPassword").value.trim();
if (username && password) {
var storedData = getStoredData(currentDomain);
if (isNew) {
storedData.push({ username: username, password: password });
} else {
var index = storedData.findIndex(function(account) {
return account.username === accountInfo.username && account.password === accountInfo.password;
});
if (index !== -1) {
storedData[index] = { username: username, password: password };
}
}
setStoredData(currentDomain, storedData);
document.body.removeChild(editDiv);
location.reload(true);
//createDisplayArea(); // 刷新显示界面
} else {
alert("账号和密码不能为空!");
}
};
if (!isNew) {
document.getElementById("deleteEntry").onclick = function() {
if (confirm("确认删除该账号信息吗?")) {
var storedData = getStoredData(currentDomain);
var index = storedData.findIndex(function(account) {
return account.username === accountInfo.username && account.password === accountInfo.password;
});
if (index !== -1) {
storedData.splice(index, 1);
setStoredData(currentDomain, storedData);
}
document.body.removeChild(editDiv);
location.reload(true);
//createDisplayArea(); // 刷新显示界面
}
};
}
}
// 填充账号和密码
function fillCredentials(username, password) {
if (autoFillkey) {
var inputFields = document.querySelectorAll("input[type='text'], input[type='email']");
if (inputFields.length > 0) {
inputFields[0].value = username;
}
var passwordFields = document.querySelectorAll("input[type='password']");
if (passwordFields.length > 0) {
passwordFields[0].value = password;
}
}
}
// 添加控制按钮
function createControlButton() {
var controlButton = document.createElement("button");
controlButton.textContent = "显示账号和密码";
controlButton.style.position = "fixed";
controlButton.style.bottom = "10px";
controlButton.style.right = "10px";
//controlButton.style.border = "none";
controlButton.style.fontSize = "10px";
controlButton.style.cursor = "pointer";
controlButton.style.zIndex = "2147483647";
controlButton.onclick = function() {
var displayDiv = document.querySelector(".jizhuDisplayArea");
if (displayDiv) {
displayDiv.style.display = displayDiv.style.display === "none" ? "block" : "none";
} else {
createDisplayArea();
}
};
controlButton.ontouchend = controlButton.onclick;
document.body.appendChild(controlButton);
}
createControlButton();
})();