-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidImporter.ts
More file actions
78 lines (71 loc) · 4.26 KB
/
idImporter.ts
File metadata and controls
78 lines (71 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 將IDNumber改成自己的身分證
const IDNumber: string = "A1234567890";
/**
* 處理 Google Drive 中的 PDF 密碼對話框。
* 嘗試自動填入密碼並觸發 'change' 事件。
* @param dialogElement 檢測到的密碼對話框元素。
*/
function handlePasswordDialog(dialogElement: Element): void {
// 暫時斷開觀察器,以避免在處理程式碼中修改 DOM 時產生無限迴圈。
// 這是一個最佳實踐,確保我們的 DOM 操作不會再次觸發觀察器。
observer.disconnect();
// 設定一個延遲,給予對話框足夠的時間來渲染其內容。
// 1500 毫秒(1.5 秒)的延遲應足夠。
setTimeout(() => {
try {
// 尋找包含檔案資訊的元素。
const driveInfoElement = document.querySelector<HTMLDivElement>('#drive-active-item-info');
if (!driveInfoElement) {
console.warn("警告:未找到元素 #drive-active-item-info。");
return; // 如果找不到該元素,則退出函式。
}
// 解析 JSON 字串以獲取檔案資訊。
// 使用類型斷言 <{ title: string }> 以告知 TypeScript 期望的結構。
const fileInfo: { title: string } = JSON.parse(driveInfoElement.innerText);
// 檢查檔案標題是否存在且為字串,並判斷是否為 PDF 檔案。
if (fileInfo && typeof fileInfo.title === 'string' && /\.pdf$/i.test(fileInfo.title)) {
// 在對話框元素中尋找密碼輸入框。
const passwordInput = dialogElement.querySelector<HTMLInputElement>('[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: MutationRecord[]) => {
// 遍歷所有發生的 DOM 變化記錄。
for (const mutation of mutationsList) {
// 我們只關心 'childList' (節點被添加/移除) 或 'attributes' (元素屬性被改變) 類型的變化。
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 });