-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
67 lines (54 loc) · 1.84 KB
/
main.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
// jshint esversion: 8
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function runSelect(event) {
event.preventDefault();
// Scroll to the bottom a few times to try to get all coupons to show up.
for (let i = 0; i < 7; ++i) {
window.scrollTo(0, document.body.scrollHeight);
await sleep(300);
}
let coupons = document.getElementsByTagName('cvs-coupon-container');
console.log(coupons.length + ' coupons found');
let clicked = 0;
for (let coupon of coupons) {
// let shadow = coupon.shadowRoot;
// if (shadow) {
// Click on "send to card" buttons.
let send2crd = coupon.querySelectorAll('button.coupon-action');
for (let btn of send2crd) {
// console.log(btn);
// btn.scrollIntoView();
btn.click();
clicked++;
}
// }
}
console.log(clicked + ' coupons clicked');
// Scroll back to the top.
window.scrollTo(0, 0);
}
function insertButton(btn) {
function waitForSite() {
let targetElem = document.querySelector('extracare-all-coupons');
if (targetElem != null) {
clearInterval(waitForSiteTimer);
targetElem.insertBefore(btn, targetElem.firstChild);
}
}
// Wait for site to finish loading the coupon list container before inserting button.
let waitForSiteTimer = setInterval(waitForSite, 100);
}
function init() {
// Make a new button for our action.
let newbutton = document.createElement('button');
newbutton.name = 'send_all_to_card';
newbutton.id = 'send_all_to_card';
newbutton.style.cssText = 'background-color: #E82A24; color: #fff; font-weight: 700; margin: 5px; border: none; padding: 6px 10px; cursor: pointer;';
newbutton.appendChild(document.createTextNode('Send All To Card'));
newbutton.addEventListener('click', runSelect);
insertButton(newbutton);
}
init();
// -- The End --