自动登录和切换账号,带有手动切换按钮
当前为
// ==UserScript==
// @name RawChat 自动账号登录
// @namespace https://www.runningcheese.com
// @version 1.2
// @description 自动登录和切换账号,带有手动切换按钮
// @author Kai, RunningCheese
// @match https://chat.rawchat.cc/login
// @icon https://t1.gstatic.cn/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&size=32&url=https://chat.rawchat.cc
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 获取存储的账号信息
let accounts = JSON.parse(localStorage.getItem('accounts')) || [];
let currentAccountIndex = parseInt(localStorage.getItem('currentAccountIndex') || '0', 10);
// 检查是否有存储的账号信息,如果没有,提示用户输入
if (accounts.length === 0) {
showAddAccountForm();
} else {
checkAndAddButtons();
}
function fillCredentials(account) {
const [username, password] = account.split(':');
// 输入用户名和密码
document.querySelector('input[name="username"]').value = username;
document.querySelector('input[name="password"]').value = password;
document.querySelector('input[name="password"]').dispatchEvent(new Event('input', { bubbles: true })); // 触发事件确保密码框填入
// 模拟按下Enter键以实现自动登录
document.querySelector('input[name="password"]').dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter', bubbles: true}));
}
function switchAccount() {
currentAccountIndex++;
if (currentAccountIndex >= accounts.length) {
currentAccountIndex = 0; // 循环使用账号
}
localStorage.setItem('currentAccountIndex', currentAccountIndex);
fillCredentials(accounts[currentAccountIndex]);
}
function addSwitchButton() {
const button = document.createElement('button');
button.id = 'switchAccountButton';
button.innerText = '切换账号';
button.style.cssText = 'max-width: 320px;margin-bottom: 10px;padding: 8px 15px;border: none;border-radius: 6px;color: white;cursor: pointer;font-size: 16px;width: 100%;text-align: center;background-color: #28a745;position: fixed;left: 50%;top: 50%;transform: translate(-50%, -50%);';
button.style.position = 'fixed';
button.style.top = '25px';
button.style.right = '40%';
button.style.zIndex = 1000;
button.addEventListener('click', handleButtonClick);
document.body.appendChild(button);
}
function addAccountButton() {
const button = document.createElement('button');
button.id = 'addAccountButton';
button.innerText = '添加账号';
button.style.cssText = 'max-width: 320px;margin-bottom: 10px;padding: 8px 15px;border: none;border-radius: 6px;color: white;cursor: pointer;font-size: 16px;width: 100%;text-align: center;background-color: #007bff;position: fixed;left: 50%;top: 50%;transform: translate(-50%, -50%);';
button.style.position = 'fixed';
button.style.top = '65px';
button.style.right = '40%';
button.style.zIndex = 1000;
button.addEventListener('click', showAddAccountForm);
document.body.appendChild(button);
}
function showAddAccountForm() {
const form = document.createElement('div');
form.style.position = 'fixed';
form.style.top = '170px';
form.style.right = '44%';
form.style.padding = '20px';
form.style.zIndex = 1000;
form.style.display = 'flex';
form.style.flexDirection = 'column';
form.style.alignItems = 'center'; // 水平居中
const accountLabel = document.createElement('label');
accountLabel.style.marginBottom = '10px'; // 增加下方间距
const accountInput = document.createElement('input');
accountInput.type = 'text';
accountInput.style.cssText = 'max-width: 320px;margin-bottom: 10px;padding: 8px 15px;border-radius: 6px;cursor: pointer;font-size: 16px;width: 100%;text-align: center;position: fixed;left: 50%;top: 5%;transform: translate(-50%, -50%);';
accountInput.style.top = '110px';
accountInput.style.width = '320px';
accountInput.style.height = '40px';
accountInput.style.borderRadius = '7px';
accountInput.style.marginBottom = '10px'; // 增加下方间距
accountInput.placeholder = '格式 = 帐号:密码,多个账号用空格分隔'; // 设置占位符
const saveButton = document.createElement('button');
saveButton.innerText = '保存';
saveButton.style.cssText = 'max-width: 320px;margin-bottom: 10px;padding: 8px 15px;border: none;border-radius: 6px;color: white;cursor: pointer;font-size: 16px;width: 100%;text-align: center;background-color: #F23822;position: fixed;left: 50%;top: 5%;transform: translate(-50%, -50%);';
saveButton.style.top = '160px';
saveButton.style.width = '320px';
saveButton.addEventListener('click', () => {
const newAccounts = accountInput.value;
addMultipleAccounts(newAccounts);
localStorage.setItem('accounts', JSON.stringify(accounts));
document.body.removeChild(form);
showAlert('添加成功');
checkAndAddButtons();
});
form.appendChild(accountLabel);
form.appendChild(accountInput);
form.appendChild(saveButton);
document.body.appendChild(form);
}
function addMultipleAccounts(newAccounts) {
const accountArray = newAccounts.split(' ').filter(account => account.includes(':'));
accountArray.forEach(account => {
if (!accounts.includes(account)) {
accounts.push(account);
}
});
}
function handleButtonClick() {
const currentURL = window.location.href;
if (currentURL.includes('https://chat.rawchat.cc/login')) {
if (accounts.length === 0) {
showAlert('请先添加账号');
} else {
switchAccount();
}
} else if (currentURL === 'https://chat.rawchat.cc/') {
currentAccountIndex++;
if (currentAccountIndex >= accounts.length) {
currentAccountIndex = 0; // 循环使用账号
}
localStorage.setItem('currentAccountIndex', currentAccountIndex);
window.open('https://chat.rawchat.cc/login', '_blank');
}
}
function showAlert(message) {
const alert = document.createElement('div');
alert.innerText = message;
alert.style.cssText = 'position: fixed; top: 20px; left: 50%; transform: translateX(-50%); background-color: #F23822; color: white; padding: 10px 20px; border-radius: 5px; z-index: 9000; font-size: 16px;';
document.body.appendChild(alert);
setTimeout(() => {
document.body.removeChild(alert);
}, 1500);
}
function checkAndAddButtons() {
if (!document.getElementById('switchAccountButton')) {
addSwitchButton();
}
if (!document.getElementById('addAccountButton')) {
addAccountButton();
}
}
window.addEventListener('load', () => {
const currentURL = window.location.href;
if (currentURL.includes('https://chat.rawchat.cc/login')) {
if (accounts.length > 0) {
fillCredentials(accounts[currentAccountIndex]);
}
}
if (accounts.length > 0) {
checkAndAddButtons();
}
});
})();