forked from boboys501/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
99 lines (83 loc) · 2.39 KB
/
Copy pathbackground.js
File metadata and controls
99 lines (83 loc) · 2.39 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
let shouldStopLoop = false;
document.addEventListener('DOMContentLoaded', function () {
let excuteBtn = document.getElementById('excute');
// onClick's logic below:
excuteBtn.addEventListener('click', function () {
shouldStopLoop = false;
executeDelete();
});
});
document.addEventListener('DOMContentLoaded', function () {
let stopBtn = document.getElementById('stop');
// onClick's logic below:
stopBtn.addEventListener('click', function () {
shouldStopLoop = true;
return shouldStopLoop;
});
});
function executeDelete() {
const photoCount = document.getElementById('photoCount');
function logMessage(message) {
console.log(message);
}
function deleteLoop(i, count) {
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function deletePhoto() {
await findAndClickMenuBtn();
await sleep(1000);
await findAndClickDeleteBtn();
await sleep(1000);
await findAndClickDeleteBtn();
}
setTimeout(function () {
logMessage('開始刪除照片');
deletePhoto();
logMessage(`已刪除第 ${i + 1} 張照片`);
logMessage(shouldStopLoop);
deleteLoop(i + 1, count);
}, i * 1000);
if (shouldStopLoop) {
logMessage('偵測按下stop,終止執行');
return;
}
if (i >= count) {
logMessage('已執行指定次數');
return;
}
}
deleteLoop(0, parseInt(photoCount.value) || 0);
}
async function findAndClickMenuBtn() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: clickMenuBtn
});
}
async function findAndClickDeleteBtn() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: clickDeleteBtn
});
}
function clickMenuBtn() {
let menuBtn = document.querySelectorAll('[aria-label="更多動作"]');
let nodes = Array.from(menuBtn);
if (nodes.length > 0) {
nodes[0].click();
}
}
function clickDeleteBtn() {
const deleteBtn = document.querySelectorAll('span');
for (let i = 0; i < deleteBtn.length; i++) {
if (deleteBtn[i].textContent.includes('永久刪除')) {
// 找到匹配的元素
deleteBtn[i].click();
}
}
}