记录网站密码信息并自动填充
目前為
// ==UserScript==
// @name 记录账号和密码
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.3.4
// @description 记录网站密码信息并自动填充
// @author niweizhuan
// @match *://*/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM_addStyle
// @grant unsafeWindow
// @license MIT
// ==/UserScript==
(function() {
'use strict';
//是否自动填写密码
var autoFillkey = true
//如果不需要自动填写密码,可以改为false
//获取当前域
function getCurrentDomain() {
return window.location.hostname;
}
//显示界面
function createDisplayArea() {
var currentDomain = getCurrentDomain();
// 主显示div
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 = "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();
}));
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 storedData = GM_getValue(currentDomain);
if (storedData) {
var accountInfo = JSON.parse(storedData);
// 帐户信息
var accountContainer = document.createElement("div");
accountContainer.style.display = "flex";
accountContainer.style.alignItems = "center";
accountContainer.appendChild(createLabel("账号:"));
accountContainer.appendChild(createCopyButton("复制", accountInfo.username));
displayDiv.appendChild(accountContainer);
displayDiv.appendChild(createValue(accountInfo.username));
// 密码信息
var passwordContainer = document.createElement("div");
passwordContainer.style.display = "flex";
passwordContainer.style.alignItems = "center";
passwordContainer.appendChild(createLabel("密码:"));
passwordContainer.appendChild(createCopyButton("复制", accountInfo.password));
displayDiv.appendChild(passwordContainer);
displayDiv.appendChild(createValue(accountInfo.password));
// 调用 fillCredentials 函数填写信息
fillCredentials(accountInfo.username, accountInfo.password);
} else {
// 没有帐户或密码信息
displayDiv.appendChild(createLabel("账号:"));
displayDiv.appendChild(createNoInfoText("[无账号信息]"));
displayDiv.appendChild(createLabel("密码:"));
displayDiv.appendChild(createNoInfoText("[无密码信息]"));
}
// 将 displayDiv 附加到正文
document.body.appendChild(displayDiv);
}
// 创建具有单击处理程序的按钮
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);
});
}
// 初始化显示域
createDisplayArea();
//编辑界面
function showEditPage() {
//初始化
var currentDomain = getCurrentDomain();
var storedData = GM_getValue(currentDomain);
var existingEditDiv = document.querySelector(".jizhuEditArea");
if (existingEditDiv) {
existingEditDiv.remove();
}
//编辑框
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 = "2147483647";
editDiv.style.display = "block";
//显示域名
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();
location.reload();
if (!newUsername && !newPassword) {
GM_deleteValue(currentDomain);
} else {
GM_setValue(currentDomain, JSON.stringify({ username: newUsername, password: newPassword }));
}
editDiv.style.display = "none";
fillCredentials(newUsername, newPassword);
};
saveButton.ontouchend = saveButton.onclick;
editDiv.appendChild(saveButton);
//关闭
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() {
editDiv.style.display = "none";
};
closeButton.ontouchend = closeButton.onclick;
editDiv.appendChild(closeButton);
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) {
var parsedData = JSON.parse(storedData);
displayDiv.querySelector(".credentialsContainer").innerHTML = "<p>账号: " + parsedData.username + "</p><p>密码: " + parsedData.password + "</p>";
fillCredentials(parsedData.username, parsedData.password);
} else {
displayDiv.querySelector(".credentialsContainer").textContent = "暂无存储的账号和密码";
}
}
}
//自动填充密码
function fillCredentials(username, password) {
if (autoFillkey == true) {
var usernameInput = document.querySelector('input[type="text"], input[name*="user"], input[name*="email"], input[name*="mail"], input[name*="username"]');
var passwordInput = document.querySelector('input[type="password"], input[name*="password"]');
if (usernameInput && passwordInput) {
usernameInput.value = username;
passwordInput.value = password;
}
}
}
createDisplayArea();
// 创建按钮:显示账号密码
var displayButton = document.createElement("button");
displayButton.textContent = "显示账号密码";
displayButton.style.position = "fixed";
displayButton.style.bottom = "10px";
displayButton.style.right = "10px";
displayButton.style.zIndex = "2147483647";
displayButton.style.cursor = "pointer";
displayButton.style.fontSize = "10px";
// 添加点击事件处理函数
displayButton.addEventListener("click", function(event) {
toggleDisplay();
event.stopPropagation(); // 阻止事件传播
});
// 添加触摸事件处理函数
displayButton.addEventListener("touchend", function(event) {
toggleDisplay();
event.stopPropagation(); // 阻止事件传播
});
document.body.appendChild(displayButton);
//填充到页面
document.body.appendChild(displayButton);
//启动自动填充密码
window.addEventListener('load', function() {
var currentDomain = getCurrentDomain();
var storedData = GM_getValue(currentDomain);
if (storedData) {
var parsedData = JSON.parse(storedData);
fillCredentials(parsedData.username, parsedData.password);
}
});
})();