-
Notifications
You must be signed in to change notification settings - Fork 0
/
ylabot.js
190 lines (151 loc) · 7.23 KB
/
ylabot.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
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const localChrome = require('chrome-location');
const prompt = require('prompt-sync')()
puppeteer.use(StealthPlugin());
async function payment(page) {
// Enter your card info here
await page.waitForSelector("iframe[title='Field container for: Card number']")
await page.waitForTimeout(2000)
let iframe = await page.$("iframe[title='Field container for: Card number']")
let iframeElement = await iframe.contentFrame()
await iframeElement.type("input[id='number']", '5529960000869293')
iframe = await page.$("iframe[title='Field container for: Name on card']")
iframeElement = await iframe.contentFrame()
await iframeElement.type("input[id='name']", 'Aiden Robinson')
iframe = await page.$("iframe[title='Field container for: Expiration date (MM / YY)']")
iframeElement = await iframe.contentFrame()
await iframeElement.type("input[id='expiry']", '07/25')
iframe = await page.$("iframe[title='Field container for: Security code']")
iframeElement = await iframe.contentFrame()
await iframeElement.type("input[id='verification_value']", '909')
await page.waitForTimeout(2000)
await page.evaluate(() => document.getElementById('continue_button').click()); // last step
}
async function checkout(page) {
await page.waitForTimeout(1500);
await page.evaluate(() => {
// document.querySelectorAll('button[data-testid="sheet-open-button"]')[0].click()
document.querySelector('button[name="checkout"]').click()
//document.querySelectorAll('button[class="Cart__Checkout Button Button--primary Button--full"]')[0].click()
})
await page.waitForSelector('#checkout_shipping_address_province');
await page.waitForTimeout(1500);
await page.select("#checkout_shipping_address_province", "Indiana");
// Enter your Info here and uncommment anything if needed
await page.evaluate(() => {
document.getElementById('checkout_email').value = "email@gmail.com"
document.getElementById('checkout_shipping_address_first_name').value = "Shivi"
document.getElementById('checkout_shipping_address_last_name').value = "Ranganathan"
document.getElementById('checkout_shipping_address_address1').value = "212 W fowler Ave"
//document.getElementById('checkout_shipping_address_address2').value = "AddressLine2"
document.getElementById('checkout_shipping_address_city').value = "West Lafayette"
document.getElementById('checkout_shipping_address_province').value = "IN"; // Select State (XX form) Ex. IN
document.getElementById('checkout_shipping_address_zip').value = "47906";
document.getElementById('checkout_shipping_address_phone').value = "9197609010";
})
// apply discount code
await page.waitForTimeout(2000);
await page.evaluate(() => {
document.getElementById('checkout_reduction_code').value = 'youngla15'
document.querySelectorAll('button[id="checkout_submit"]')[0].click()
})
await page.waitForTimeout(2000);
await page.evaluate(() => document.getElementById('continue_button').click());
await page.waitForTimeout(2000);
await page.evaluate(() => document.getElementById('btn-proceed-address').click());
// continue to estimate shipping date
await page.waitForTimeout(2000)
await page.waitForSelector("#continue_button")
await page.evaluate(() => document.getElementById('continue_button').click());
await payment(page);
}
async function monitor(page, productLink, colorChoice, sizeChoice) {
await page.goto(productLink);
await page.waitForTimeout(2000);
// Using page.evaluate to interact with the DOM
await page.evaluate((colorChoice) => {
let links = document.querySelectorAll("label[class]");
for (let i = 0; i < links.length; i++) {
if (links[i].innerText.toLowerCase() === colorChoice) {
links[i].click();
break;
}
}
}, colorChoice);
await page.waitForTimeout(2000);
let available = await page.evaluate((sizeChoice) => {
let links = document.querySelectorAll("label[class]");
let flag = false;
for (let i = 0; i < links.length; i++) {
if (links[i].innerText.toLowerCase() === sizeChoice) {
links[i].click();
flag = true;
break;
}
}
return true;
}, sizeChoice);
await page.waitForTimeout(2000);
console.log(available);
if (available) {
await page.evaluate(() => {
document.querySelector('button[data-action="add-to-cart"]').click();
});
await page.waitForTimeout(2000);
return true;
}
return false;
}
// async function run() {
// const browser = await puppeteer.launch({headless: false, executablePath: localChrome});
// const page = await browser.newPage();
// const colorChoice = prompt('Enter the color of your choice: ').trim().toLowerCase();
// const sizeChoice = prompt('Enter your desired size: ').trim().toLowerCase();
// while (true) {
// try {
// let isAvailable = await monitor(page, colorChoice, sizeChoice);
// console.log("Availability:", isAvailable);
// if (isAvailable) {
// await page.waitForTimeout(7000);
// await checkout(page);
// break;
// } else {
// console.log("Product not available. Retrying...");
// await page.waitForTimeout(5000);
// }
// } catch (error) {
// console.error("An error occurred:", error);
// await page.screenshot({ path: 'error.png' });
// // Handle error or retry logic here
// }
// }
// }
async function monitorAndCheckout(browser, productLink, colorChoice, sizeChoice) {
const page = await browser.newPage();
let isAvailable = await monitor(page, productLink, colorChoice, sizeChoice);
console.log(`Availability for ${productLink}:`, isAvailable);
if (!isAvailable) {
console.log(`Product at ${productLink} not available or options not found.`);
}
return page;
//await page.close(); // Close the current product tab
}
async function run() {
const browser = await puppeteer.launch({ headless: false, executablePath: localChrome });
let page = await browser.newPage();
let inputLinks = prompt('Enter product links separated by commas: ');
let productLinks = inputLinks.split(',').map(link => link.trim());
for (const productLink of productLinks) {
console.log(`Product link: ${productLink}`);
let colorChoice = prompt('Enter the color of your choice for this product: ').trim().toLowerCase();
let sizeChoice = prompt('Enter your desired size for this product: ').trim().toLowerCase();
page = await monitorAndCheckout(browser, productLink, colorChoice, sizeChoice);
// You might want to add a timeout here to space out requests
//await browser.waitForTimeout(5000);
}
await checkout(page); // Proceed to checkout after all items are added to the cart
//await browser.close();
}
// GhostCursor Item
run();