-
Notifications
You must be signed in to change notification settings - Fork 1
/
TwitterPuppeteer.ts
173 lines (139 loc) · 6.56 KB
/
TwitterPuppeteer.ts
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
import { getMousePosition, useMouseHelper } from "@/composables/useMouseHelper"
import { useMoveToClick } from "@/composables/useMoveToClick"
import puppeteer, { Browser, Page } from "puppeteer"
import * as fs from "fs"
import * as path from "path"
import { app } from "electron"
import { BaseDestinationPlugin } from "@/lib/BaseDestinationPlugin"
import { useRandomDelay } from "@/composables/useRandomDelay"
export class TwitterPuppeteer extends BaseDestinationPlugin {
name = "TwitterPuppeteer"
twitterUsername: string
twitterPassword: string
browser?: Browser
page?: Page
constructor(options?: any) {
super(options)
this.twitterUsername = process.env.TWITTER_USERNAME!
this.twitterPassword = process.env.TWITTER_PASSWORD!
this.login = this.login.bind(this)
this.deleteSingletonFiles = this.deleteSingletonFiles.bind(this)
process.on("exit", async () => {
if (this.browser) {
await this.browser.close()
}
app.exit()
})
}
async initialize() {
await this.deleteSingletonFiles()
if (!this.twitterUsername || !this.twitterPassword) {
this.log("Twitter username or password not set. Cannot enable TwitterPuppeteer plugin. Exiting...")
await new Promise(resolve => setTimeout(resolve, 5000))
process.exit(1)
}
let puppeteerOpts: any = {
headless: false,
userDataDir: "./user_data",
args: [
"--disable-infobars", "--no-first-run", "--disable-notifications", "--disable-background-timer-throttling",
"--disable-backgrounding-occluded-windows", "--disable-breakpad", "--disable-component-extensions-with-background-pages",
"--disable-extensions", "--disable-features=TranslateUI,BlinkGenPropertyTrees", "--disable-ipc-flooding-protection",
"--disable-renderer-backgrounding", "--mute-audio", "--no-default-browser-check", "--no-first-run", "--hide-crashed-bubbles",
"--hide-crashed-restore-bubble", "--noerrdialogs", "--enable-automation", "--disable-blink-features=AutomationControlled",
]
}
if (process.env.PUPPETEER_EXEC_PATH !== undefined) {
puppeteerOpts = {
...puppeteerOpts,
executablePath: process.env.PUPPETEER_EXEC_PATH
}
}
await puppeteer.launch(puppeteerOpts).catch(async (err: any) => {
this.log(err)
this.log("Failed to launch puppeteer. Exiting...")
await new Promise(resolve => setTimeout(resolve, 1000))
process.exit(1)
}).then(async (browser: any) => {
if (!browser) {
this.log("Failed to launch puppeteer. Exiting...")
await new Promise(resolve => setTimeout(resolve, 1000))
process.exit(1)
}
this.browser = browser
this.page = await browser.newPage()
this.browser.on("disconnected", async () => {
this.log("Browser disconnected. Exiting...")
await new Promise(resolve => setTimeout(resolve, 1000))
process.exit()
})
this.page.on("dialog", async dialog => {
this.log(dialog.message())
await dialog.dismiss()
})
await useMouseHelper(this.page)
await this.login()
this.log("Logged in.")
})
}
async deleteSingletonFiles() {
// delete singleton files in case of crash
try {
const files = fs.readdirSync("@/user_data/")
for (const file of files) {
if (file.match("Singleton")) {
const filePath = path.join("./user_data/", file)
this.log(`Removing ${filePath}...`)
fs.unlinkSync(filePath)
}
}
} catch (e) {
}
}
async sendPost(postContent: string): Promise<void> {
await this.page.waitForSelector(".public-DraftStyleDefault-block")
await this.page.click("[data-testid='PostTextarea_0RichTextInputContainer']")
await this.page.type("[data-testid='PostTextarea_0RichTextInputContainer']", postContent)
await new Promise(resolve => setTimeout(resolve, 1000))
const elementHandle = await this.page.$("[aria-label=\"Add post\"]")
const nextSiblingHandle = await elementHandle?.evaluateHandle(element => element.nextElementSibling)
await nextSiblingHandle?.asElement()?.click()
await this.page.waitForSelector("[id*='typeaheadDropdownWrapped-']", {timeout: 1000})
.then(async () => {
await this.page.keyboard.press("Escape")
}).catch(() => {
})
const start = getMousePosition()
await useMoveToClick(this.page, nextSiblingHandle?.asElement()!, start.x, start.y).catch(() => {
this.log("Unable to send Post.")
}).then(async () => {
this.log("Post sent.")
})
}
async login() {
// set user agent and viewport size
await this.page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36")
await this.page.setViewport({
width: Math.floor(1366 + ( Math.random() * 100 )),
height: Math.floor(768 + ( Math.random() * 100 ))
})
// go to twitter login page
await this.page.goto("https://twitter.com/login")
// wait for username field to appear, then type username and password
await this.page.waitForSelector("input[autocomplete='username']", {timeout: 5000})
.then(async () => {
await this.page.click("input[autocomplete='username']")
await this.humanType("input[autocomplete='username']", this.twitterUsername + "\n")
await this.page.waitForSelector("input[autocomplete='current-password']")
await this.page.click("input[autocomplete='current-password']")
await this.humanType("input[autocomplete='current-password']", this.twitterPassword + "\n")
})
.catch(() => this.log("Didn't find username field, already logged in?"))
}
async humanType(selector: string, text: string, delay: number = 100) {
// type each character with a random delay
for (const character of text) {
await this.page.type(selector, character, {delay: useRandomDelay(delay, delay + 500)})
}
}
}