-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
112 lines (94 loc) · 3.02 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
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
// jshint esversion: 8
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function findButtons(str) {
let btns = [];
for (let btn of document.querySelectorAll('button')) {
if (btn.textContent.includes(str)) {
btns.push(btn);
}
}
return btns;
}
async function runSelect(event) {
event.preventDefault();
// Click on "Show More" button until it no longer shows up.
// Limit the number of times we click on that button.
for (let i = 0; i < 10; ++i) {
let btns = findButtons('Show More');
if (!btns.length) break;
let btn = btns[0];
btn.scrollIntoView();
btn.click();
await sleep(1000);
}
// Click on "Clip Coupon" buttons.
let btns = findButtons('Clip Coupon');
console.log(btns.length + ' coupons found');
let clicked = 0;
for (let btn of Array.from(btns).reverse()) {
btn.scrollIntoView();
btn.click();
clicked++;
await sleep(1000);
}
console.log(clicked + ' coupons clicked');
}
function insertButton(btn) {
function waitForSite() {
let targetelem = document.getElementById('app');
if (targetelem !== null) {
clearInterval(waitForSiteTimer);
targetelem.insertBefore(btn, targetelem.childNodes[0]);
}
}
// Wait for site to finish loading before inserting button.
let waitForSiteTimer = setInterval(waitForSite, 100);
}
function run() {
// Check if we are on the coupons page.
let loc = window.location.href;
if (!loc.includes('/savings/coupons/browse')) {
// Remove button if it is on a non-coupon page.
let btn = document.getElementById('load_all_to_card');
if (btn) btn.remove();
return;
}
// Make a new button for our action.
let newbutton = document.createElement('button');
newbutton.name = 'load_all_to_card';
newbutton.id = 'load_all_to_card';
newbutton.style.cssText = 'background-color: #fff; color: #E82A24; font-weight: 700; border: solid #E82A24; padding: 6px 10px; cursor: pointer; margin: 5px';
newbutton.addEventListener('mouseenter',
() => {
newbutton.style.color = '#fff';
newbutton.style.backgroundColor = '#E82A24';
}
);
newbutton.addEventListener('mouseleave',
() => {
newbutton.style.color = '#E82A24';
newbutton.style.backgroundColor = '#fff';
}
);
newbutton.appendChild(document.createTextNode('Load All To Card'));
newbutton.addEventListener('click', runSelect);
// Don't add button if already there.
if (!document.getElementById('load_all_to_card')) insertButton(newbutton);
}
// Run the button inserter the first time and also whenever the URL changes.
// Some links in the new Food Lion web interface do not reload the page.
const observeUrlChange = () => {
let oldHref = null;
const body = document.querySelector('body');
const observer = new MutationObserver(mutations => {
if (oldHref !== document.location.href) {
oldHref = document.location.href;
run();
}
});
observer.observe(body, { childList: true, subtree: true });
};
window.onload = observeUrlChange;
// -- The End --