-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy-coupon-code-copier.user.js
133 lines (117 loc) · 4.13 KB
/
easy-coupon-code-copier.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// ==UserScript==
// @name Easy Coupon Code Copier
// @namespace easy-coupon-code-copier
// @version 1.3
// @description Quickly copy coupon codes in Zendesk!
// @updateURL https://github.com/druesome/andrew-scripts/raw/main/easy-coupon-code-copier.user.js
// @downloadURL https://github.com/druesome/andrew-scripts/raw/main/easy-coupon-code-copier.user.js
// @author druesome
// @match https://*.apps.zdusercontent.com/*
// @require http://code.jquery.com/jquery-latest.js
// @grant GM_addStyle
// ==/UserScript==
GM_addStyle(`
span.coupon-code {
transition: all 0.3s;
padding: 8px;
background: #f7f7f7;
border: 1px solid #ccc;
margin: 2px;
&:hover {
cursor: pointer;
background-color: #cfe88b;
}
}
span.no-coupon {
display: none;
}
div.actions {
margin-bottom: 20px;
}
`);
var $ = window.jQuery;
var monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
];
function fetchCouponCodes() {
const cacheBuster = Date.now();
const url = 'https://raw.githubusercontent.com/druesome/andrew-scripts/main/couponCodes.json?cache=' + cacheBuster;
return fetch(url)
.then(response => response.json())
.then(data => data.codes)
.catch(error => console.error('Failed to fetch coupon codes: ', error));
}
function isCouponExpired(expirationDate) {
const now = new Date();
const expiration = new Date(expirationDate);
return now > expiration;
}
function addCouponLinks() {
$('.user__info_container .actions').each(function (site) {
if (!$(this).hasClass('itsadded')) {
$(this).addClass('itsadded');
// Add regular monthly coupon code
var addRegularCoupon = function (couponCode, expirationDate) {
var span = $('<span>');
span.addClass('coupon-code');
span.css('cursor', 'pointer');
span.click(function () {
if (!isCouponExpired(expirationDate)) {
navigator.clipboard.writeText(couponCode)
.then(function () {
console.log('Coupon code copied to clipboard: ' + couponCode);
span.text('Copied');
setTimeout(function () {
span.text(couponCode);
}, 2000);
})
.catch(function (error) {
console.error('Failed to copy coupon code to clipboard: ', error);
});
}
});
if (isCouponExpired(expirationDate)) {
span.text('Expired');
span.addClass('no-coupon');
} else {
span.text(couponCode);
}
$('.user__info_container .actions').append(span);
};
var currentMonth = new Date().getMonth() + 1;
fetchCouponCodes().then(couponCodes => {
var regularCouponCode = couponCodes[currentMonth.toString()];
if (typeof regularCouponCode === 'string') {
addRegularCoupon(regularCouponCode);
} else {
console.error('No regular coupon code found for the current month');
}
var oneOffCouponData = couponCodes['13'];
if (oneOffCouponData && typeof oneOffCouponData === 'object' && !isCouponExpired(oneOffCouponData.expiration)) {
var span = $('<span>');
span.addClass('coupon-code');
span.css('cursor', 'pointer');
span.click(function () {
navigator.clipboard.writeText(oneOffCouponData.code)
.then(function () {
console.log('One-off coupon code copied to clipboard: ' + oneOffCouponData.code);
span.text('Copied');
setTimeout(function () {
span.text(oneOffCouponData.code);
}, 2000);
})
.catch(function (error) {
console.error('Failed to copy one-off coupon code to clipboard: ', error);
});
});
span.text(oneOffCouponData.code);
$('.user__info_container .actions').append(span);
}
});
}
});
}
// Loop until links are added
window.setInterval(function () {
addCouponLinks();
}, 1000);