-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-clipper.user.js
85 lines (73 loc) · 2.74 KB
/
auto-clipper.user.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
// ==UserScript==
// @name Safeway Coupon Auto-clipper
// @icon https://www.safeway.com/favicon.ico
// @namespace https://github.com/NeurekaSoftware/Safeway-Coupon-Auto-Clipper
// @version 1.0.2
// @description A Violentmonkey userscript that will automatically clip your Just For U coupons from Safeway.
// @author Douglas Parker
// @downloadURL https://raw.githubusercontent.com/NeurekaSoftware/Safeway-Coupon-Auto-Clipper/main/auto-clipper.user.js
// @supportURL https://github.com/NeurekaSoftware/Safeway-Coupon-Auto-Clipper/issues
// @match https://www.safeway.com/foru/coupons-deals.html
// @run-at document-idle
// @grant none
// ==/UserScript==
(function() {
'use strict';
const STARTUP_DELAY = 5000;
const ACTION_DELAY = 1000;
const DISPLAY_STATISTICS = true;
let totalCouponsClipped = 0;
let totalCouponsExpired = 0;
let couponsLoaded = false;
let couponsClipped = false;
let statisticsSent = false;
setTimeout(function() {
setInterval(function() {
if(!couponsLoaded) {
loadCoupons();
} else if(!couponsClipped) {
clipCoupons();
}
else if(DISPLAY_STATISTICS && !statisticsSent) {
alert(`Safeway Coupon Auto-clipper\n\nCoupons Clipped: ${totalCouponsClipped + totalCouponsExpired}`);
statisticsSent = true;
}
}, ACTION_DELAY);
}, STARTUP_DELAY);
function loadCoupons() {
let loadMoreButton = document.querySelector('.btn.load-more');
if(loadMoreButton) {
loadMoreButton.click();
// Scroll to bottom of page
let footer = document.querySelector('footer');
footer.scrollIntoView({ behavior: 'smooth'});
}
else {
couponsLoaded = true;
}
}
function clipCoupons() {
let coupon = document.querySelector('[id^="couponAddBtn"]');
if(coupon) {
coupon.click();
checkExpiredCoupon(coupon);
// Scroll to next element
coupon = document.querySelector('[id^="couponAddBtn"]');
if(coupon) coupon.scrollIntoView({ behavior: 'smooth'});
}
else {
couponsClipped = true;
}
}
function checkExpiredCoupon(coupon) {
const expiredCoupon = document.querySelector('[id="errorModal"] [class="btn btn-default btn-modal"]');
if(expiredCoupon) {
expiredCoupon.click();
coupon.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.remove();
totalCouponsExpired++;
}
else {
totalCouponsClipped++;
}
}
})();