-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeijerAutoCouponClipper.js
110 lines (101 loc) · 4.02 KB
/
MeijerAutoCouponClipper.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
// Function to display a modal with the specified content
function showModal(content) {
// Clear any existing modal HTML
const existingModal = document.getElementById('coupon-modal');
if (existingModal) {
existingModal.remove();
}
// Create the modal container
const modal = document.createElement('div');
modal.setAttribute('id', 'coupon-modal');
modal.innerHTML = `
<div class="coupon-modal__content">
${content}
<div><button class="coupon-modal__close-btn">Close</button></div>
</div>
`;
document.body.appendChild(modal);
// Style the modal
modal.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 10px #333333;
`;
// Add event listener to remove the modal when the "Close" button is clicked
const closeBtn = document.querySelector('.coupon-modal__close-btn');
closeBtn.addEventListener('click', () => {
modal.remove();
});
}
// Expand the coupon categories box
const buttons = document.querySelectorAll('.coupon-filter__accordion-heading');
buttons.forEach(button => {
if (button.textContent.includes("Categories") && button.getAttribute('aria-expanded') === 'false') {
button.click();
}
});
// Get all the filter elements and loop through them to check if any of them contains a category
const filterElements = document.querySelectorAll('.coupon-filter__checkbox-wrapper .mperks-checkbox__container');
let checkedCategories = false;
for (const element of filterElements) {
if (categories.some((category) => element.textContent.includes(category))) {
element.click();
checkedCategories = true;
}
}
// If there are coupons available in selected categories, clip them and show a modal with the number of clipped coupons
if (checkedCategories) {
// Create a modal indicating that coupons are being clipped
showModal('<p>Clipping coupons...</p>');
setTimeout(() => {
// Get all the clip elements, the number of coupons already clipped, and calculate how many more coupons need to be clipped
const clipElements = document.getElementsByClassName('coupon-tile__button--clip');
const preClipped = Number(document.getElementById('js-clipped-coupon-count-display').innerHTML);
let numClipped = preClipped;
const numToClip = maxCoupons - numClipped;
// Define a recursive function to click elements with delay
function clickElementWithDelay(i) {
// If there are elements left to click
if (i < numToClip) {
// Check if the element exists
if (clipElements[i]) {
// Schedule a click after a random delay between 500-1000 milliseconds
setTimeout(() => {
clipElements[i].click();
numClipped++;
// Call the function recursively with the next index
clickElementWithDelay(i + 1);
}, Math.floor(Math.random() * (1000 - 500 + 1) + 500));
}
} else {
// If there are elements left to click
// Calculate the number of newly clipped coupons and the total number of clipped coupons
const clipped = numClipped - preClipped;
const totalClipped = numClipped;
// Create the content for the modal
const content = `
<p>Coupons Clipped: ${clipped}</p>
<p>Total Coupons Clipped: ${totalClipped}</p>
<p>Clipping Done!</p>
<p>Special Offers Page: <a href="https://www.meijer.com/shopping/coupons/hand-picked-offers.html" target="_blank">Link ↗</a></p>
`;
// Display the modal with the content
showModal(content);
}
}
// Start the recursive function with index 0
clickElementWithDelay(0);
}, 6000); // Wait 6 seconds for the coupons to load before clipping them
} else {
// If there are no coupons available in selected categories, show a modal with a message
const content = `
<p>No coupons available in selected categories</p>
`;
// Display the modal with the content
showModal(content);
}