-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
78 lines (64 loc) · 2.21 KB
/
main.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
'use strict'
const path = require('path')
const { setTimeout } = require('node:timers/promises')
const { app, BrowserWindow, ipcMain } = require('electron')
const robots = require('./robots.js')
const workflows = require('./workflows.js')
const knownPublishers = Object.keys(workflows)
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker')
puppeteer.use(AdblockerPlugin({ blockTrackers: true }))
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html', { query: { knownPublishers } })
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
ipcMain.on('get-editors', async (event, publisher) => {
try {
// TODO: Add {headless: false} as an option for debugging.
const browser = await puppeteer.launch({
headless: false,
defaultViewport: { width: 1400, height: 1200 },
executablePath: require('puppeteer').executablePath().replace('app.asar', 'app.asar.unpacked')
})
const page = await browser.newPage()
// Wrap page.goto to add robots.txt support.
const originalGoto = page.goto.bind(page)
page.goto = async function (url) {
if (await robots.canCrawl(url)) {
// Use crawl delay requested by publisher.
const crawlDelay = await robots.getCrawlDelay(url)
await setTimeout(crawlDelay * 1000)
return originalGoto(url)
}
throw new Error('Blocked by robots.txt')
}
const workflow = workflows[publisher]
if (workflow.timeout) {
page.setDefaultNavigationTimeout(workflow.timeout)
}
await page.goto(workflow.start)
await workflow.getData(page, event)
await browser.close()
} catch (err) {
event.reply('print', 'There was an unexpected error:\n')
event.reply('print', `${err.stack}\n`)
}
event.reply('done')
})