-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.ts
More file actions
296 lines (259 loc) · 8.92 KB
/
browser.ts
File metadata and controls
296 lines (259 loc) · 8.92 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import puppeteer from 'puppeteer-core'
import * as path from 'path'
import * as os from 'os'
// Helper function to delay execution
async function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
function getEdgePath(): string {
if (process.platform === 'win32') {
return 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'
} else if (process.platform === 'darwin') {
return '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge'
} else {
return '/usr/bin/microsoft-edge'
}
}
function getEdgeUserDataDir(): string {
// Use a dedicated profile directory to avoid WebView2 conflicts
// Extensions need to be installed once in this profile
if (process.platform === 'win32') {
return path.join(
os.homedir(),
'AppData',
'Local',
'GH-SocialPreviewGenerator',
'EdgeProfile'
)
} else if (process.platform === 'darwin') {
return path.join(
os.homedir(),
'Library',
'Application Support',
'GH-SocialPreviewGenerator'
)
} else {
return path.join(os.homedir(), '.config', 'gh-social-preview-generator')
}
}
export async function uploadSocialPreviewViaBrowser(
owner: string,
repo: string,
imagePath: string
): Promise<void> {
const browser = await puppeteer.launch({
executablePath: getEdgePath(),
userDataDir: getEdgeUserDataDir(),
headless: false,
defaultViewport: null,
args: ['--no-first-run', '--no-default-browser-check'],
ignoreDefaultArgs: ['--disable-extensions']
})
try {
const page = await browser.newPage()
// Navigate to repo settings
const settingsUrl = `https://github.com/${owner}/${repo}/settings`
await page.goto(settingsUrl, { waitUntil: 'networkidle2' })
// Check if we're logged in by looking for the settings form
const settingsForm = await page.$('form[data-turbo="false"], main h2')
if (!settingsForm) {
console.log('\n Please log into GitHub in the browser window...')
console.log(' (Waiting up to 2 minutes for login)\n')
await page.waitForSelector('form[data-turbo="false"], main h2', {
timeout: 120000
})
}
await delay(1000)
// Scroll down to find Social preview section
await page.evaluate(() => {
const labels = document.querySelectorAll('label, h3, summary')
for (const label of labels) {
if (label.textContent?.includes('Social preview')) {
label.scrollIntoView({ behavior: 'smooth', block: 'center' })
break
}
}
})
await delay(1000)
// Click the Edit button or summary to expand the social preview section
const editClicked = await page.evaluate(() => {
const summaries = document.querySelectorAll('summary, button')
for (const el of summaries) {
if (
el.textContent?.includes('Edit') &&
el.closest('[class*="social"]')
) {
;(el as HTMLElement).click()
return true
}
}
// Try clicking any Edit button near Social preview text
const labels = document.querySelectorAll('label, h3')
for (const label of labels) {
if (label.textContent?.includes('Social preview')) {
const parent = label.closest('div')
const btn = parent?.querySelector('summary, button')
if (btn) {
;(btn as HTMLElement).click()
return true
}
}
}
return false
})
if (editClicked) {
await delay(1000)
}
// Find the file input (might be hidden, use a more general selector)
let fileInput = await page.$('input[type="file"]')
if (!fileInput) {
// Wait a bit more for it to appear
await page.waitForSelector('input[type="file"]', { timeout: 5000 })
fileInput = await page.$('input[type="file"]')
}
if (!fileInput) {
throw new Error('Could not find file input for social preview upload')
}
await fileInput.uploadFile(imagePath)
await delay(3000)
// Look for and click save button
const buttons = await page.$$('button[type="submit"], button')
for (const button of buttons) {
try {
const text = await button.evaluate(
el => el.textContent?.toLowerCase() || ''
)
if (text.includes('save') || text.includes('update')) {
await button.click()
await delay(2000)
break
}
} catch {
// Skip buttons that aren't clickable
}
}
await page.close()
} finally {
await browser.close()
}
}
export async function uploadAllViaBrowser(
repos: { owner: string; repo: string; imagePath: string }[],
onProgress?: (current: number, total: number, repo: string) => void
): Promise<{ success: number; failed: number }> {
const browser = await puppeteer.launch({
executablePath: getEdgePath(),
userDataDir: getEdgeUserDataDir(),
headless: false,
defaultViewport: null,
args: ['--no-first-run', '--no-default-browser-check'],
ignoreDefaultArgs: ['--disable-extensions']
})
let success = 0
let failed = 0
try {
const page = await browser.newPage()
for (let i = 0; i < repos.length; i++) {
const { owner, repo, imagePath } = repos[i]
if (onProgress) {
onProgress(i + 1, repos.length, repo)
}
try {
// Navigate to repo settings
const settingsUrl = `https://github.com/${owner}/${repo}/settings`
await page.goto(settingsUrl, { waitUntil: 'networkidle2' })
// Look for the social preview edit button or file input
// GitHub's settings page has a "Social preview" section with an Edit button
// Check if we're logged in by looking for the settings form
const settingsForm = await page.$('form[data-turbo="false"], main h2')
if (!settingsForm) {
console.log('\n Please log into GitHub in the browser window...')
console.log(' (Waiting up to 2 minutes for login)\n')
await page.waitForSelector('form[data-turbo="false"], main h2', {
timeout: 120000
})
}
await delay(1000)
// Scroll down to find Social preview section
await page.evaluate(() => {
const labels = document.querySelectorAll('label, h3, summary')
for (const label of labels) {
if (label.textContent?.includes('Social preview')) {
label.scrollIntoView({ behavior: 'smooth', block: 'center' })
break
}
}
})
await delay(1000)
// Click the Edit button or summary to expand the social preview section
const editClicked = await page.evaluate(() => {
const summaries = document.querySelectorAll('summary, button')
for (const el of summaries) {
if (
el.textContent?.includes('Edit') &&
el.closest('[class*="social"]')
) {
;(el as HTMLElement).click()
return true
}
}
// Try clicking any Edit button near Social preview text
const labels = document.querySelectorAll('label, h3')
for (const label of labels) {
if (label.textContent?.includes('Social preview')) {
const parent = label.closest('div')
const btn = parent?.querySelector('summary, button')
if (btn) {
;(btn as HTMLElement).click()
return true
}
}
}
return false
})
if (editClicked) {
await delay(1000)
}
// Find the file input (might be hidden, use a more general selector)
let fileInput = await page.$('input[type="file"]')
if (!fileInput) {
// Wait a bit more for it to appear
await page.waitForSelector('input[type="file"]', { timeout: 5000 })
fileInput = await page.$('input[type="file"]')
}
if (!fileInput) {
throw new Error('Could not find file input for social preview upload')
}
await fileInput.uploadFile(imagePath)
await delay(3000)
// Look for and click save button
const buttons = await page.$$('button[type="submit"], button')
for (const button of buttons) {
try {
const text = await button.evaluate(
el => el.textContent?.toLowerCase() || ''
)
if (text.includes('save') || text.includes('update')) {
await button.click()
await delay(2000)
break
}
} catch {
// Skip buttons that aren't clickable
}
}
await delay(1000)
success++
} catch (error) {
console.error(
` Error uploading to ${owner}/${repo}: ${error instanceof Error ? error.message : error}`
)
failed++
}
}
await page.close()
} finally {
await browser.close()
}
return { success, failed }
}