MoviePilot-V2自动登录

MoviePilot-V2自动登录脚本,添加了网址检查,每3秒检查一次

// ==UserScript==
// @name         MoviePilot-V2自动登录
// @version      1.2
// @description  MoviePilot-V2自动登录脚本,添加了网址检查,每3秒检查一次
// @author       spacey0409
// @namespace    spacey0409
// @match        http://192.168.3.10
// @icon         https://www.google.com/s2/favicons?sz=64&domain=5.2
// @grant        none
// @require      https://unpkg.com/axios/dist/axios.min.js
// @license      GPL-3.0
// ==/UserScript==

(function () {
    'use strict';

    // 直接在这里配置账号、密码和MoviePilot地址
    const uname = "test";   // 你的 MoviePilot 账号
    const upassword = "test"; // 你的 MoviePilot 密码
    const MoviePilotHost = "http://192.168.3.10"; // MoviePilot 地址

    function autoLogin() {
        let usernameInput = document.querySelector('input[name="username"]');
        let passwordInput = document.querySelector('input[name="current-password"]');
        let loginButton = document.querySelector('button[type="submit"], button.login-button');

        if (usernameInput && passwordInput && loginButton) {
            function setInputValue(element, value) {
                let nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
                nativeInputValueSetter.call(element, value);
                element.dispatchEvent(new Event('input', { bubbles: true }));
                element.dispatchEvent(new Event('change', { bubbles: true }));
            }
            
            setInputValue(usernameInput, uname);
            setInputValue(passwordInput, upassword);

            setTimeout(() => {
                loginButton.click();
            }, 1000);
        } else {
            console.error("无法找到登录输入框或按钮!");
        }
    }

    function checkLoginPage() {
        // 每3秒检查一次 URL 是否包含 "/#/login"
        setInterval(() => {
            if (window.location.href.includes('/#/login')) {
                let checkLoginForm = setInterval(() => {
                    if (document.querySelector('input[name="username"]') && document.querySelector('input[name="current-password"]')) {
                        clearInterval(checkLoginForm);
                        autoLogin();
                    }
                }, 500);
            }
        }, 3000);  // 每3秒检查一次 URL
    }

    // 在页面加载时,检查是否需要自动登录
    checkLoginPage();
})();