-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
76 lines (68 loc) · 2.42 KB
/
background.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
const interval = 15 * 1000; // Refresh interval in milliseconds (15 seconds)
const serviceID = "serviceID"; // Your EmailJS service ID
const templateID = "templateID"; // Your EmailJS template ID
const to_email = "[email protected]"; // Recipient email address
const from_name = "Your Name"; // Sender's name
const userID = "userID"; // Your EmailJS user ID
// Initialize EmailJS with your user ID
emailjs.init(userID);
// Function to send an email with specified message content
const sendEmail = async (message) => {
const params = {
to_email,
from_name,
message,
};
// Send the email using EmailJS service
emailjs.send(serviceID, templateID, params).then(
(response) => {
console.log("SUCCESS!", response.status, response.text);
// Remove the URL from the tabList after successfully sending the email
removeFromTabList(message);
},
(error) => {
console.log("FAILED...", error);
}
);
};
// Function to remove a URL from the tabList in local storage
const removeFromTabList = (url) => {
browser.storage.local.get(["tabList"], function (result) {
const tabList = result.tabList || [];
const updatedTabList = tabList.filter((storedUrl) => storedUrl !== url);
// Update the tabList in local storage
browser.storage.local.set({ tabList: updatedTabList });
console.log(`URL removed from tabList: ${url}`);
});
};
// Function to reload tabs in the tabList and check for "Preorder" button presence
const reloadTabs = (tabList) => {
tabList.forEach(function (tabUrl) {
chrome.tabs.query({ url: tabUrl }, function (tabs) {
if (tabs.length > 0) {
chrome.tabs.reload(tabs[0].id, function () {
console.log("Reloaded:", tabs[0].url);
checkPrecommandeButton(tabs[0]);
});
}
});
});
};
// Function to check for the presence of the "Preorder" button on a tab
const checkPrecommandeButton = (tab) => {
browser.tabs
.sendMessage(tab.id, { action: "checkPrecommandeButton" })
.then(function (response) {
if (response && response.found) {
// If the "Preorder" button is found, send an email and remove the URL from tabList
sendEmail(tab.url);
}
});
};
// Set interval to periodically reload tabs and check for "Preorder" button
setInterval(function () {
chrome.storage.local.get(["tabList"], function (result) {
const tabList = result.tabList || [];
reloadTabs(tabList);
});
}, interval);