-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel.js
More file actions
117 lines (90 loc) · 2.95 KB
/
Copy pathpixel.js
File metadata and controls
117 lines (90 loc) · 2.95 KB
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
const process = require('process')
const puppeteer = require('puppeteer')
const iPhone = puppeteer.devices['iPhone SE']
const args = process.argv.slice(2)
const DEBUG = process.env.DEBUG
const escapeXpathString = str => {
const splitQuotes = str.replace(/'/g, `', "'", '`)
return `concat('${splitQuotes}', '')`
}
const clickByText = async (page, text, tag = '*') => {
const escapedText = escapeXpathString(text)
const linkHandlers = await page.$x(
`//${tag}[contains(text(), ${escapedText})]`,
)
if (linkHandlers.length > 0) {
await linkHandlers[0].click()
} else {
throw new Error(`Link not found: ${text}`)
}
}
let browser
async function main() {
browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setBypassCSP(true)
await page.emulate(iPhone)
console.log('Navigate to login')
await page.goto('https://instagram.com/accounts/login/')
await page.waitFor('input[name=username]')
console.log('Type username')
await page.type('input[name=username]', args[0])
console.log('Type password')
await page.type('input[name=password]', args[1])
console.log('Click log in')
await clickByText(page, 'Log In')
console.log('Wait for navigation')
try {
await page.waitForNavigation()
} catch (error) {
console.error(error)
throw new Error('Log in error/timeout: Wrong credentials maybe?')
}
console.log('Go to root')
await page.goto('https://instagram.com', { waitUntil: 'networkidle2' })
console.log('Try to suppress any popup dialogs')
try {
await page.click('[role=dialog] button:nth-child(2)')
await page.waitFor(1000)
} catch (_) {}
try {
await page.click('[role=dialog] button:nth-child(2)')
await page.waitFor(1000)
} catch (_) {}
try {
await page.click('[role=dialog] button:nth-child(2)')
await page.waitFor(1000)
} catch (_) {}
console.log('Click new post')
page.waitFor('[data-testid="new-post-button"]')
const [fileChooser] = await Promise.all([
page.waitForFileChooser(),
page.click('[data-testid="new-post-button"]'),
])
console.log('Place photo')
await fileChooser.accept([args[2]])
console.log('Wait for photo preview')
await page.waitFor('[style*="blob:https://www.instagram.com"]')
console.log('Click next')
await clickByText(page, 'Next', 'button')
console.log('Wait for post preview')
await page.waitFor('[alt="Preview of photo to be uploaded"]')
if (args[3]) {
console.log('Type caption')
await page.type('textarea[aria-label="Write a caption…"]', args[3])
}
console.log('Click share')
await clickByText(page, 'Share', 'button')
console.log('Wait for navigation')
await page.waitForNavigation()
console.log('Wait 10s for post to finish')
await page.waitFor(10000)
console.log('Done!')
await browser.close()
}
main().catch(async error => {
console.error(error)
if (DEBUG) console.log(await page.screenshot({encoding: 'base64'}))
await browser.close()
process.exit(1)
})