-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
80 lines (55 loc) · 1.55 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
const path = require('path');
const { app, BrowserWindow, ipcMain } = require('electron')
const cheerio = require('cheerio');
const axios = require("axios");
const url = "https://cp-algorithms.com/";
async function getRandomAlgorithmLink () {
const {data} = await axios.get(url);
const $ = cheerio.load(data);
const AlgosLinksElements = $("a");
const AlgoLinks = [];
const AlgoTitles = [];
AlgosLinksElements.each((index, element) => {
AlgoLinks.push(url + $(element).attr("href").slice(1));
AlgoTitles.push($(element).text());
})
for (let i = 0; i < 4; i++) {
AlgoTitles.shift();
AlgoLinks.shift();
}
for (let i = 0; i < 3; i++) {
AlgoLinks.pop();
AlgoTitles.pop();
}
let index = Math.floor(Math.random() * (AlgoTitles.length));
let algo = {
title: AlgoTitles[index],
url: AlgoLinks[index]
}
return algo;
}
let win = null;
async function createWindow () {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration:true,
contextIsolation:false
}
})
win.loadFile('index.html');
let algo = await getRandomAlgorithmLink();
win.webContents.send("data", algo);
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
})