Last active
December 2, 2025 06:37
-
-
Save PrintNow/c506c07931b93a164825def44c8018aa to your computer and use it in GitHub Desktop.
[Tampermonkey 插件] 自动登录 ArgoCD 系统
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name [Work] ArgoCD 自动登录 - 请你修改源代码,修改 @match 后面的 URL 为你的 ArgoCD 地址 | |
| // @namespace https://nowtime.cc | |
| // @version 2025-12-02 | |
| // @description 自动化登录 ArgoCD | |
| // @author Shine | |
| // @match https://argocd.example.com/login?* | |
| // @icon https://www.google.com/s2/favicons?sz=64&domain=example.com | |
| // @grant GM.setValue | |
| // @grant GM.getValue | |
| // @grant GM.deleteValue | |
| // @run-at document-idle | |
| // ==/UserScript== | |
| (function () { | |
| 'use strict'; | |
| const context = { | |
| username: undefined, | |
| password: undefined, | |
| }; | |
| // 延迟 100ms 后运行登录 | |
| setTimeout( | |
| () => initData(handleLogin), | |
| 100 | |
| ) | |
| async function initData(then) { | |
| let username = await GM.getValue('username'), | |
| password = await GM.getValue('password'); | |
| if (!username) { | |
| username = prompt("请输入用户名") | |
| if (username === '') { | |
| throw new Error(`请输入用户名`) | |
| } | |
| GM.setValue('username', username); | |
| console.log('用户名设置成功') | |
| } | |
| if (!password) { | |
| password = prompt("请输入密码") | |
| if (password === '') { | |
| throw new Error(`请输入密码`) | |
| } | |
| GM.setValue('password', password); | |
| console.log('密码设置成功') | |
| } | |
| context.username = username; | |
| context.password = password; | |
| then && then() | |
| } | |
| async function handleLogin(attempt = 0) { | |
| const $inputs = document.querySelectorAll(`form input`) | |
| if ($inputs.length !== 2) { | |
| if (attempt > 10) { | |
| throw new Error(`无法获取输入框`) | |
| } | |
| console.log(`尝试第 ${attempt + 1} 登录`) | |
| setTimeout( | |
| () => handleLogin(attempt + 1), | |
| 200 | |
| ) | |
| return | |
| } | |
| setInputValue($inputs[0], context.username, ["input", "change"]) | |
| setInputValue($inputs[1], context.password, ["input", "change"]) | |
| // 模拟提交 | |
| document.querySelector(`button[type="submit"]`).click() | |
| console.log('模拟提交登录') | |
| } | |
| function setInputValue(input, value, trigger = ["input"]) { | |
| const setter = Object.getOwnPropertyDescriptor( | |
| Object.getPrototypeOf(input), | |
| "value" | |
| )?.set; | |
| if (setter) { | |
| setter.call(input, value); // 最通用 | |
| } else { | |
| input.value = value; | |
| }, @match | |
| trigger.forEach(eventName => { | |
| input.dispatchEvent(new Event(eventName, {bubbles: true})); | |
| }); | |
| } | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用方法
注意安装完成后,修改脚本:
