-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
64 lines (58 loc) · 1.92 KB
/
content.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
function getQuantity() {
return document.querySelector('input[data-test=pab-item-quantity]');
}
function getBuyBtn() {
return document.querySelector('button[data-test=pab-item-btn-pick]');
}
function getPlusBtn() {
return document.querySelector('button[data-test=quantity-increase-cta]');
}
function getLoggedInLink() {
return document.querySelector('a[data-test=header-account-cta]');
}
function clickPlus(iteration, times, sendResponse) {
const plusBtn = getPlusBtn();
if (plusBtn) {
for (let i = 0; i < times; i++) {
plusBtn.click();
}
sendResponse('bought ' + getQuantity().value);
} else if (iteration < 20) {
setTimeout(() => clickPlus(iteration + 1, times, sendResponse), 300);
} else {
sendResponse('no plus button');
}
}
function clickBuy(iteration, quantity, sendResponse) {
console.log('buying');
const buyBtn = getBuyBtn();
const plusBtn = getPlusBtn();
console.log('buying', buyBtn);
if (buyBtn) {
buyBtn.click();
if (quantity > 1) {
setTimeout(() => {
clickPlus(1, quantity - 1, sendResponse);
}, 300);
} else if (getQuantity()) {
sendResponse('bought ' + getQuantity().value);
} else {
sendResponse('bought 1');
}
} else if (plusBtn) {
clickPlus(1, quantity, sendResponse);
} else if (iteration < 10) {
setTimeout(() => clickBuy(iteration + 1, quantity, sendResponse), 300);
} else {
sendResponse('not found');
}
}
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
console.log('got msg', msg);
if (msg.text === 'buy') {
clickBuy(1, msg.quantity, function(content) { sendResponse({content, index: msg.index, buyAll: msg.buyAll}) });
} else if (msg.text === 'loggedin') {
sendResponse({loggedin: !!getLoggedInLink()});
}
return true;
});