-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (127 loc) · 5.47 KB
/
index.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
const puppeteer = require('puppeteer');
require('dotenv').config();
const userAgent = require('user-agents');
const randomUseragent = require('random-useragent');
const helper = require('./util/helper');
// const StealthPlugin = require('puppeteer-extra-plugin-stealth');
// puppeteer.use(StealthPlugin());
//const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36';
const getPS5 = async () => {
const browser = await puppeteer.launch({ headless: false });
const userAgent = randomUseragent.getRandom();
const UA = userAgent;
console.log(UA);
// Does not work Opera/9.80 (Android; Opera Mini/9.0.1829/66.318; U; en) Presto/2.12.423 Version/12.16
// Mozilla/5.0 (Symbian/3; Series60/5.2 NokiaN8-00/014.002; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) Version/3.0 BrowserNG/7.2.6.4 3gpp-gba
// Mozilla/5.0 (Linux; U; Android 2.0.1; de-de; Milestone Build/SHOLS_U2_01.14.0) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17
const [page] = await browser.pages();
await page.setRequestInterception(true);
page.on('request', (request) => {
if (
['image', 'stylesheet', 'font', 'script'].indexOf(
request.resourceType()
) !== -1
) {
request.abort();
} else {
request.continue();
}
});
await page.setViewport({
width: 1500 + Math.floor(Math.random() * 100),
height: 1080 + Math.floor(Math.random() * 100),
deviceScaleFactor: 1,
hasTouch: false,
isLandscape: false,
isMobile: false,
});
await page.setUserAgent(UA);
await page.setDefaultNavigationTimeout(0);
const pageReload = async () => {
console.log('Looking my lord');
await page.reload({ waitUntil: ['networkidle2', 'domcontentloaded'] });
$addToCartButton = await page.$(
'#add-on-atc-container > div:nth-child(1) > section > div.valign-middle.display-inline-block.prod-product-primary-cta.primaryProductCTA-marker > div.prod-product-cta-add-to-cart.display-inline-block'
);
//console.log($addToCartButton);
if (!$addToCartButton || $addToCartButton === null) await pageReload();
};
// Sign in to WalMart
await signIn(page);
console.log('Headed to Walmart.com...');
// const findAddToCart = async (page) => {
await page.goto('https://www.walmart.com/ip/PlayStation-5-Console/363472942');
await helper.checkURL(
page,
'https://www.walmart.com/ip/PlayStation-5-Console/363472942',
async function () {
await addToCart(page);
}
);
let $addToCartButton = await page.$(
'#add-on-atc-container > div:nth-child(1) > section > div.valign-middle.display-inline-block.prod-product-primary-cta.primaryProductCTA-marker > div.prod-product-cta-add-to-cart.display-inline-block'
);
if (!$addToCartButton) await pageReload();
console.log('Add to cart button was found. Clicking now...');
await page.click(
'#add-on-atc-container > div:nth-child(1) > section > div.valign-middle.display-inline-block.prod-product-primary-cta.primaryProductCTA-marker > div.prod-product-cta-add-to-cart.display-inline-block'
);
console.log('Add to cart button is clicked');
await page.waitForTimeout(500);
// }
//add to cart
await addToCart(page);
console.log('add to cart finished');
// Checkout
// 1) Delivery Method
await page.waitForSelector('.cxo-continue-btn');
await page.click('.cxo-continue-btn');
// 2) Confirm Delivery Method
await page.waitForSelector(
'body > div.js-content > div > div.checkout-wrapper > div > div.accordion-inner-wrapper > div.checkout-accordion > div > div > div > div:nth-child(2) > div.CXO_module_container > div.CXO_module_body.ResponsiveContainer > div > div > div > div.text-left.Grid > div > div > div > div > div.arrange > div.arrange-fill.u-size-1-12-m > button > span'
);
await page.click(
'body > div.js-content > div > div.checkout-wrapper > div > div.accordion-inner-wrapper > div.checkout-accordion > div > div > div > div:nth-child(2) > div.CXO_module_container > div.CXO_module_body.ResponsiveContainer > div > div > div > div.text-left.Grid > div > div > div > div > div.arrange > div.arrange-fill.u-size-1-12-m > button > span'
);
// 3) Type CVV and finish order
await page.waitForSelector('#cvv-confirm');
await page.type('#cvv-confirm', process.env.CREDIT_CARD_CVV);
await page.click('.fulfillment-opts-continue');
await page.waitForTimeout(5000);
await browser.close();
};
getPS5();
async function signIn(page) {
await page.goto('https://www.walmart.com/account/login?tid=0&returnUrl=%2F', {
waitUntil: 'networkidle0',
});
await helper.checkURL(
page,
'https://www.walmart.com/account/login?tid=0&returnUrl=%2F',
async function () {
await signIn(page);
}
);
await page.type('#email', process.env.WALMART_EMAIL, { delay: 50 });
await page.type('#password', process.env.WALMART_PASSWORD, { delay: 50 });
await page.click('button[type="submit"]');
await page.waitForTimeout(2000);
}
const addToCart = async (page) => {
await page.goto('https://www.walmart.com/cart', {
waitUntil: 'networkidle2',
});
await helper.checkURL(
page,
'https://www.walmart.com/cart',
async function () {
await addToCart(page);
}
);
await page.waitForSelector('.checkoutBtn');
console.log('In the shopping cart now...');
await page.evaluate(() => {
const $checkOutBtn = document.querySelector('.checkoutBtn');
$checkOutBtn.click();
});
};