-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
162 lines (146 loc) · 4.37 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
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
const { app, BrowserWindow, screen, ipcMain, nativeImage, session, ipcRenderer, globalShortcut, components } = require('electron')
const path = require('path')
const https = require('https')
const { restoreWindowSize, trackWindowSize } = require('./windowSizeStorage')
const icon = nativeImage.createFromPath('favicon-32x32.png')
const useragent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'
let win;
app.whenReady().then(() => {
components.whenReady().then(() => {
// Open app
const display = screen.getPrimaryDisplay()
const displaySize = display.workAreaSize
const options = {
x: 0,
y: 0,
width: Math.floor(displaySize.width / 2),
height: displaySize.height,
darkTheme: true,
backgroundColor: '#222',
opacity: 0,
icon,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
webviewTag: true,
plugins: true
}
}
restoreWindowSize('home', options)
win = new BrowserWindow(options)
win.setMenuBarVisibility(false)
trackWindowSize(win, 'home')
const readyToShow = () => {
win.off('ready-to-show', readyToShow)
win.webContents.send('useragent', useragent)
win.focus()
setTimeout(() => {
win.setOpacity(1)
}, 200)
}
win.on('ready-to-show', readyToShow)
win.loadFile('home.html')
// Open a window if none are open (macOS)
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
})
// Close app
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// Download and display favicon
// ipcMain.handle('icon-ipc', async (event, iconUrl) => {
// https.get(
// iconUrl,
// {headers: {
// 'User-Agent': useragent
// }},
// (response) => {
// const data = []
// response.on('data', (chunk) => data.push(chunk))
// response.on('end', () => {
// if(response.statusCode !== 200) {
// console.log('Failed to download favicon. Statuscode ', response.statusCode)
// return
// }
// try {
// const buffer = Buffer.concat(data)
// if(!buffer) {
// console.log('Failed to download favicon. No data received.')
// return
// }
// const icon = nativeImage.createFromBuffer(buffer)
// win.setIcon(icon)
// } catch(ex) {
// console.log('Failed to load favicon. Error:', ex.message)
// }
// })
// }
// )
// })
// Download and display favicon
const videoWindows = []
ipcMain.handle('video-ipc', (event, videoUrl, referrer) => {
let windowIndex = videoWindows.findIndex(vw => vw === null)
windowIndex = (windowIndex === -1) ? videoWindows.length : windowIndex
const windowName = `video.${windowIndex}`
const display = screen.getPrimaryDisplay()
const displaySize = display.workAreaSize
const options = {
x: Math.floor(displaySize.width / 2),
y: 0,
width: Math.ceil(displaySize.width / 2),
height: displaySize.height,
darkTheme: true,
backgroundColor: '#222',
opacity: 0,
icon,
frame: false,
titleBarStyle: 'hidden',
titleBarOverlay: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
webviewTag: true,
plugins: true
}
}
restoreWindowSize(windowName, options)
const win = new BrowserWindow(options)
win.setMenuBarVisibility(false)
trackWindowSize(win, windowName)
const videoWindow = {
videoUrl,
win
}
if(windowIndex < videoWindows.length) {
videoWindows[windowIndex] = videoWindow
} else {
videoWindows.push(videoWindow)
}
const readyToShow = () => {
win.off('ready-to-show', readyToShow)
// win.maximize()
win.focus()
setTimeout(() => {
win.setOpacity(1)
}, 200)
const [ noActionVideoUrl ] = videoUrl.split('?')
win.webContents.send('state', {
useragent,
videoId: videoWindows.indexOf(videoWindow),
videoUrl: `${noActionVideoUrl}?action=play`,
referrer
})
}
win.on('ready-to-show', readyToShow)
win.on('closed', () => {
videoWindows[videoWindows.indexOf(videoWindow)] = null
})
win.loadFile('detail.html')
})
ipcMain.handle('video-close-ipc', (event, videoId) => {
videoWindows[videoId]?.win?.close()
})