-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmh-gift-buttons.user.js
297 lines (246 loc) · 8.95 KB
/
mh-gift-buttons.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// ==UserScript==
// @name 🐭️ MouseHunt - Gift Buttons
// @version 1.7.0
// @description Add buttons to easily accept and return all daily gifts.
// @license MIT
// @author bradp
// @namespace bradp
// @match https://www.mousehuntgame.com/*
// @icon https://i.mouse.rip/mouse.png
// @grant none
// @run-at document-end
// @require https://cdn.jsdelivr.net/npm/[email protected]/mousehunt-utils.js
// @require https://cdn.jsdelivr.net/npm/[email protected]
// ==/UserScript==
(function () {
'use strict';
/**
* Send the gifts.
*
* @param {string} buttonClass The class of the button to click.
* @param {number} limit The number of gifts to send.
* @param {boolean} reverse Whether to reverse the order of the clicks.
*/
const sendGifts = (buttonClass, limit = 15, reverse = false) => {
if (hg && hg.views?.GiftSelectorView?.show) { // eslint-disable-line no-undef
hg.views.GiftSelectorView.show(); // eslint-disable-line no-undef
}
const expand = document.querySelectorAll('.giftSelectorView-claimableGift');
if (expand) {
expand.forEach((el) => {
el.classList.add('expanded');
});
}
let innerButtons = document.querySelectorAll(`.giftSelectorView-friendRow-action.${buttonClass}:not(.disbled):not(.selected)`);
if (! innerButtons.length) {
return;
}
// If we're doing it in reverse order, reverse the array.
if (getSetting('gift-buttons-reverse', false) || reverse) {
innerButtons = Array.prototype.slice.call(innerButtons);
innerButtons.reverse();
}
let sent = 0;
innerButtons.forEach((button) => {
if (sent >= limit) {
return;
}
sent++;
button.click();
});
const confirm = document.querySelector('.mousehuntActionButton.giftSelectorView-action-confirm.small');
if (confirm) {
confirm.click();
}
};
const makePaidGiftsButton = (buttonContainer) => {
const hasPaidGifts = document.querySelectorAll('.giftSelectorView-friendRow-returnCost');
if (! hasPaidGifts.length) {
return;
}
const paidGiftsButton = makeElement('button', ['mh-gift-button', 'mh-gift-buttons-paid-gifts'], 'Accept & Return Paid Gifts');
paidGiftsButton.addEventListener('click', () => {
hg.views.GiftSelectorView.show(); // eslint-disable-line no-undef
hg.views?.GiftSelectorView.showTab('claim_paid_gifts', 'selectClaimableGift');
let acceptedGifts = JSON.parse(localStorage.getItem('mh-gift-buttons-accepted-paid-gifts'));
if (! acceptedGifts) {
acceptedGifts = {};
}
const newAcceptedGifts = {};
const gifts = document.querySelectorAll('.giftSelectorView-friendRow.paidgift');
gifts.forEach((gift) => {
const friendId = gift.getAttribute('data-snuid');
const giftId = gift.parentNode.parentNode.parentNode.getAttribute('data-item-type');
const acceptButton = gift.querySelector('.giftSelectorView-friendRow-action.claim');
const returnButton = gift.querySelector('.giftSelectorView-friendRow-action.return');
if (! giftId || ! friendId || ! acceptButton || ! returnButton) {
return;
}
if (! acceptedGifts[ giftId ] || ! acceptedGifts[ giftId ].includes(friendId)) {
returnButton.click();
// save the gift as accepted.
if (! newAcceptedGifts[ giftId ]) {
newAcceptedGifts[ giftId ] = [];
}
newAcceptedGifts[ giftId ].push(friendId);
} else {
acceptButton.click();
}
});
if (newAcceptedGifts !== acceptedGifts) {
localStorage.setItem('mh-gift-buttons-accepted-paid-gifts', JSON.stringify(newAcceptedGifts));
}
});
buttonContainer.appendChild(paidGiftsButton);
};
const makeAcceptButton = (buttonContainer) => {
const acceptButton = makeElement('button', ['mh-gift-button', 'mh-gift-buttons-accept'], 'Accept All');
const acceptLimit = document.querySelector('.giftSelectorView-numClaimActionsRemaining');
if (acceptLimit && acceptLimit.innerText === '0') {
acceptButton.classList.add('disabled');
} else {
acceptButton.addEventListener('click', () => {
sendGifts('claim', acceptLimit ? parseInt(acceptLimit.innerText, 10) : 15);
});
}
buttonContainer.appendChild(acceptButton);
};
const makeReturnButton = (buttonContainer) => {
// Return button.
const returnWrapper = makeElement('div', 'mh-gift-buttons-return-wrapper');
const returnButton = makeElement('button', ['mh-gift-button', 'mh-gift-buttons-return'], 'Accept & Return All');
const returnLimit = document.querySelector('.giftSelectorView-numSendActionsRemaining');
if (returnLimit && returnLimit.innerText === '0') {
returnButton.classList.add('disabled');
} else {
returnButton.addEventListener('click', () => {
sendGifts('return', returnLimit ? parseInt(returnLimit.innerText, 10) : 25);
});
}
returnWrapper.appendChild(returnButton);
buttonContainer.appendChild(returnWrapper);
};
/**
* Make the buttons and add them to the page.
*/
const makeButtons = () => {
if (document.getElementById('bulk-gifting-gift-buttons')) {
return;
}
const buttonContainer = document.createElement('div');
buttonContainer.id = 'bulk-gifting-gift-buttons';
makePaidGiftsButton(buttonContainer);
makeAcceptButton(buttonContainer);
makeReturnButton(buttonContainer);
// Add the buttons to the page.
const giftFooter = document.querySelector('.giftSelectorView-inbox-footer');
if (giftFooter && giftFooter.firstChild) {
giftFooter.insertBefore(buttonContainer, giftFooter.firstChild);
}
};
/**
* On a sucessful send, close the modal.
*
* @param {Object} request The request.
*/
const checkForSuccessfulGiftSend = (request) => {
if (! request || 'undefined' === request.friends_sent_gifts || ! request.friends_sent_gifts.length > 1) {
return;
}
const okayBtn = document.querySelector('.giftSelectorView-confirmPopup-submitConfirmButton');
if (! okayBtn) {
return;
}
setTimeout(() => {
okayBtn.click();
if ('undefined' === typeof activejsDialog || ! activejsDialog || ! activejsDialog.hide) { // eslint-disable-line no-undef
return;
}
activejsDialog.hide(); // eslint-disable-line no-undef
}, 2000);
};
addStyles(`#bulk-gifting-gift-buttons {
margin-bottom: 10px;
position: relative;
display: flex;
justify-content: flex-end;
}
#bulk-gifting-gift-buttons button {
display: block;
padding: 10px;
font-size: 12px;
color: #000;
text-align: center;
text-decoration: none;
background-color: #eee;
border: 1px solid #000;
border-radius: 5px;
box-shadow: 1px 1px 1px #eee;
}
#bulk-gifting-gift-buttons .mh-gift-buttons-accept {
margin-right: 10px;
}
#bulk-gifting-gift-buttons .mh-gift-buttons-paid-gifts {
margin-right: 10px;
}
#bulk-gifting-gift-buttons button:hover,
#bulk-gifting-gift-buttons button:focus,
#bulk-gifting-gift-buttons .mh-gift-buttons-return:hover,
#bulk-gifting-gift-buttons .mh-gift-buttons-return:focus,
#bulk-gifting-gift-buttons .mh-gift-buttons-accept-reverse:hover,
#bulk-gifting-gift-buttons .mh-gift-buttons-accept-reverse:focus {
background-color: #ffae00;
box-shadow: 0 0 5px #fff inset, 1px 1px 1px #fff;
}
#bulk-gifting-gift-buttons button.disabled:hover,
#bulk-gifting-gift-buttons button.disabled:focus {
background-color: #eee;
cursor: default;
box-shadow: 0 0 3px #f00;
}
#bulk-gifting-gift-buttons .mh-gift-buttons-return {
background-color: #fff600;
}
.giftSelectorView-inbox-giftContainer {
height: auto;
min-height: 300px;
max-height: 75vh;
}
.giftSelectorView-inbox-giftRow.complete {
height: 25px;
padding-top: 5px;
padding-left: 15px;
border: none;
box-shadow: none;
}
.giftSelectorView-inbox-giftRow.complete .giftSelectorView-inbox-gift-thumb {
display: inline;
}
.giftSelectorView-inbox-giftRow.complete .itemImage {
display: inline-block;
width: 25px;
height: 25px;
}
.giftSelectorView-inbox-giftRow.complete .giftSelectorView-inbox-gift-details {
width: 90%;
}
`);
onAjaxRequest(makeButtons, '/managers/ajax/users/socialGift.php');
onAjaxRequest(checkForSuccessfulGiftSend, '/managers/ajax/users/socialGift.php');
const buttonLink = document.querySelector('#hgbar_freegifts');
if (buttonLink) {
buttonLink.addEventListener('click', function () {
makeButtons();
});
}
const tab = addSettingsTab();
addSetting(
'Gift Buttons - Reverse Order',
'gift-buttons-reverse',
false,
'Accept and send gifts from oldest to newest instead of newest to oldest.',
{},
tab
);
migrateUserscript('🐭️ MouseHunt - Gift Buttons', 'https://greasyfork.org/en/scripts/449489-mousehunt-gift-buttons');
}());