-
Notifications
You must be signed in to change notification settings - Fork 3
/
palscript.js
202 lines (150 loc) · 6.25 KB
/
palscript.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
const puppeteer = require('puppeteer');
const request = require('request-promise-native');
const poll = require('promise-poller').default;
const apiKey = '' //2captcha api key
const { get_items } = require('./helper.js');
const user = require('./user.js');
console.log(user);
const chromeOptions = {
headless:false,
slowMo:10,
defaultViewport: null};
(async () => {
const browser = await puppeteer.launch(chromeOptions)
const page = await browser.newPage()
const navigationPromise = page.waitForNavigation()
const items = get_items();
for (const item of items) {
await page.goto(item.url)
await page.setViewport({ width: 1440, height: 747 })
await scrollToBottom(page)
await page.waitFor(500);
await scrollToBottom(page)
await page.waitFor(500);
await scrollToBottom(page)
await page.waitFor(500);
const [h3] = await page.$x("//h3[contains(.,\'" + item.keyPhrase + "\')]");
if (h3) {
await h3.click();
}
if(item.size !== "") {
// dropdown with each size
await page.waitFor(700);
await page.type("select#product-select", item.size)
}
await page.click('.add')
await page.waitFor(700);
await page.waitForSelector('.cart-heading')
await page.click('.cart-heading')
}
//cart to checkout
await page.goto('https://shop-usa.palaceskateboards.com/cart')
await page.waitForSelector('#cartform > #basket-right > .checkbox-wrapper > label > .checkbox-control')
await page.click('#cartform > #basket-right > .checkbox-wrapper > label > .checkbox-control')
await page.waitFor(500);
await page.waitForSelector('#content > #shopping-cart > #cartform #checkout')
await page.click('#content > #shopping-cart > #cartform #checkout')
console.log('clicked checkout')
await navigationPromise
console.log('passed nav promise')
//checkout page
//business for captcha below
await page.waitFor(1000);
console.log('1000 ms')
console.log('before const')
const currentURL = page.url();
console.log('page url caught')
const requestId = await initiateCaptchaRequest(apiKey,currentURL);
console.log('after const')
//await page.waitForNavigation({ waitUntil: 'load' })
console.log('load')
await page.waitForSelector('#checkout_email')
console.log('waited for #checkout_email')
element = await page.$x(`//*[@id="checkout_email"]`);
await element[0].click();
console.log('clicked email')
element = await page.$x(`//*[@id="checkout_email"]`);
await element[0].type(user.email);
console.log('typed email')
element = await page.$x(`(.//*[normalize-space(text()) and normalize-space(.)='Email'])[1]/following::label[1]`);
await element[0].click();
element = await page.$x(`//*[@id="checkout_shipping_address_first_name"]`);
await element[0].click();
element = await page.$x(`//*[@id="checkout_shipping_address_first_name"]`);
await element[0].type(user.firstName);
element = await page.$x(`//*[@id="checkout_shipping_address_last_name"]`);
await element[0].click();
element = await page.$x(`//*[@id="checkout_shipping_address_last_name"]`);
await element[0].type(user.lastName);
element = await page.$x(`//*[@id="checkout_shipping_address_address1"]`);
await element[0].click();
element = await page.$x(`//*[@id="checkout_shipping_address_address1"]`);
await element[0].type(user.address);
element = await page.$x(`//*[@id="checkout_shipping_address_city"]`);
await element[0].type(user.city);
await page.type("select#checkout_shipping_address_province", "Tennessee")
element = await page.$x(`//*[@id="checkout_shipping_address_zip"]`);
await element[0].type(user.zip);
element = await page.$x(`//*[@id="checkout_shipping_address_phone"]`);
await element[0].type(user.phone);
const response = await pollForRequestResults(apiKey, requestId);
await page.evaluate(`document.getElementById("g-recaptcha-response").innerHTML="${response}";`);
//finish up shipping info
element = await page.$x(`//*[@class="btn__content"]`);
await element[0].click();
//confirm shipping page?
await page.waitForSelector('.step__footer__continue-btn')
await page.click('.step__footer__continue-btn')
//pay page
await page.waitFor(500);
await page.reload({ waitUntil: ["networkidle2", "domcontentloaded"] });
await page.waitFor(500);
//paypal obvi
element = await page.$x(`//img[@alt='PayPal']`);
await element[0].click();
//seeya
await page.waitForSelector('.edit_checkout > .step__footer > .shown-if-js > .step__footer__continue-btn > .btn__content')
await page.click('.edit_checkout > .step__footer > .shown-if-js > .step__footer__continue-btn > .btn__content')
await navigationPromise
})()
//-----bottom scroller function-----
async function scrollToBottom(page) {
const distance = 746; // should be less than or equal to window.innerHeight
const delay = 120;
while (await page.evaluate(() => document.scrollingElement.scrollTop + window.innerHeight < document.scrollingElement.scrollHeight)) {
await page.evaluate((y) => { document.scrollingElement.scrollBy(0, y); }, distance);
await page.waitFor(delay);
}
}
//-----captcha business-----
async function initiateCaptchaRequest(apiKey,currentURL) {
const formData = {
method: 'userrecaptcha',
key: apiKey, // your 2Captcha API Key
googlekey: '6LeoeSkTAAAAAA9rkZs5oS82l69OEYjKRZAiKdaF', //palaces captcha api key, i did the work for you
pageurl: currentURL,
json: 1
};
const response = await request.post('http://2captcha.com/in.php', {form: formData});
return JSON.parse(response).request;
}
async function pollForRequestResults(key, id, retries = 30, interval = 1500, delay = 10000) {
await timeout(delay);
return poll({
taskFn: requestCaptchaResults(key, id),
interval,
retries
});
}
function requestCaptchaResults(apiKey, requestId) {
const url = `http://2captcha.com/res.php?key=${apiKey}&action=get&id=${requestId}&json=1`;
return async function() {
return new Promise(async function(resolve, reject){
const rawResponse = await request.get(url);
const resp = JSON.parse(rawResponse);
if (resp.status === 0) return reject(resp.request);
resolve(resp.request);
});
}
}
const timeout = millis => new Promise(resolve => setTimeout(resolve, millis))