-
Notifications
You must be signed in to change notification settings - Fork 0
/
search with the unread proceed.js
119 lines (106 loc) · 4.07 KB
/
search with the unread proceed.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
let count = 0;
let unreadCount = 0;
let emails = [];
let currentIndex = 0;
// Function to perform the search
function performSearch() {
const searchBox = document.querySelector('input[name="q"]');
if (searchBox) {
searchBox.dispatchEvent(new Event('input', { bubbles: true })); // Trigger input event
setTimeout(() => {
searchBox.form.submit(); // Submit the form after the input event
}, 500); // Adjust the delay as needed
} else {
console.error('Search box not found');
}
}
// Function to process unread emails
function processUnreadEmails() {
emails = Array.from(document.querySelectorAll('tr.zA.zE')); // Select unread emails
unreadCount = emails.length; // Count total unread emails
if (unreadCount === 0) {
console.log("No unread emails found.");
return;
}
emails = shuffleArray(emails);
processNextEmail();
}
// Function to process the next email
function processNextEmail() {
if (currentIndex >= emails.length) {
console.log("All unread emails processed.");
return;
}
const email = emails[currentIndex];
email.click();
setTimeout(() => {
markAsImportant(() => {
count++;
updateCount(count, unreadCount);
currentIndex++;
processNextEmail();
});
}, 3000); // Increase delay to ensure email content is fully loaded
}
// Function to mark an email as important
function markAsImportant(callback) {
setTimeout(() => {
const moreButton = document.querySelector('div[aria-label="More options"]')
|| document.querySelector('div[role="button"]');
if (moreButton) {
moreButton.click();
setTimeout(() => {
const starIcon = document.querySelector('div[aria-label="Not starred"]')
|| document.querySelector('div[aria-label="Star"]');
if (starIcon) {
starIcon.click();
setTimeout(callback, 1000); // Wait for marking to complete
} else {
console.error("Star icon not found.");
callback();
}
}, 1000); // Delay to allow menu to open
} else {
console.error("More options button not found.");
callback();
}
}, 2000); // Delay to ensure elements are visible
}
// Function to update the count in storage
function updateCount(count, unreadCount) {
if (chrome && chrome.storage && chrome.storage.local) {
chrome.storage.local.set({ emailCount: count, totalUnread: unreadCount }, () => {
console.log(`Email count updated: Opened ${count}, Total Unread: ${unreadCount}`);
});
} else {
console.error("chrome.storage.local is not available.");
}
}
// Function to shuffle the array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
// Function to observe DOM changes
function observeForSearchBox() {
const observer = new MutationObserver(() => {
const searchBox = document.querySelector('input[name="q"]');
if (searchBox) {
observer.disconnect(); // Stop observing once the search box is found
// Wait for the user to enter search query and press Enter
const interval = setInterval(() => {
if (searchBox.value) {
clearInterval(interval);
performSearch(); // Perform the search with the entered query
setTimeout(processUnreadEmails, 10000); // Delay to ensure search results are loaded
}
}, 1000); // Check every second
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
// Start observing for the search box
observeForSearchBox();