Auto import ID number

when opening PDF auto import ID number.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Auto import ID number
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  when opening PDF auto import ID number.
// @author       Adokun
// @match        https://mail.google.com/*
// @grant        none
// ==/UserScript==

// 將IDNumber改成自己的身分證
const IDNumber = "A1234567890";

function handlePasswordDialog(dialogElement) {
    observer.disconnect();
    setTimeout(() => {
        try {
            const driveInfoElement = document.querySelector('#drive-active-item-info');
            if (!driveInfoElement) {
                console.warn("警告:未找到元素 #drive-active-item-info。");
                return;
            }
            const fileInfo = JSON.parse(driveInfoElement.innerText);
            if (fileInfo && typeof fileInfo.title === 'string' && /\.pdf$/i.test(fileInfo.title)) {
                const passwordInput = dialogElement.querySelector('[type="password"]');
                if (passwordInput) {
                    // console.log(`偵測到 PDF 檔案:${fileInfo.title}。正在嘗試自動填入密碼。`);
                    passwordInput.value = IDNumber; // 填入密碼。
                    passwordInput.dispatchEvent(new Event('change')); // 觸發 'change' 事件,模擬使用者輸入。
                    // console.log("密碼欄位已成功填入並觸發 change 事件。");
                }
                else {
                    console.warn("警告:在 PDF 密碼對話框中未找到密碼輸入元素。");
                }
            }
        }
        catch (error) {
            console.error("錯誤:處理 PDF 密碼對話框時發生問題。", error);
        }
        finally {
            // 無論是否發生錯誤,都重新連接觀察器,繼續監聽 DOM 變化。
            observer.observe(document.body, { childList: true, subtree: true, attributes: true });
        }
    }, 1500);
}
// 創建一個 MutationObserver 實例來監聽 DOM 樹的變化。
// MutationObserver 比 DOMSubtreeModified 更加高效和推薦。
const observer = new MutationObserver((mutationsList) => {
    for (const mutation of mutationsList) {
        if (mutation.type === 'childList' || mutation.type === 'attributes') {
            // 檢查被修改的目標元素本身是否為對話框。
            const targetElement = mutation.target instanceof Element ? mutation.target : null;
            if (targetElement && targetElement.getAttribute('role') === 'dialog' && targetElement.classList.contains('aLF-aPX-axU')) {
                handlePasswordDialog(targetElement);
                return;
            }
            // 檢查所有新加入的節點,看是否有符合條件的對話框。
            mutation.addedNodes.forEach(node => {
                if (node instanceof Element && node.getAttribute('role') === 'dialog' && node.classList.contains('aLF-aPX-axU')) {
                    handlePasswordDialog(node);
                    return;
                }
            });
        }
    }
});
// 開始觀察整個 document body,監聽其子節點、所有後代子樹以及屬性的變化。
observer.observe(document.body, { childList: true, subtree: true, attributes: true });