-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.development.js
131 lines (103 loc) · 2.53 KB
/
main.development.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
// @flow
/* eslint-disable camelcase */
import electron from 'electron'
import Config from 'electron-config'
import referer from 'electron-referer'
import wallpaper from 'wallpaper'
import {download} from 'electron-dl'
import appMenu from './menu'
const {app, BrowserWindow, ipcMain, shell} = electron
let mainWindow
require('electron-context-menu')()
function openTweet(url: string) {
const win = new BrowserWindow({width: 800, height: 600})
referer('https://twitter.com', win)
const page = win.webContents
page.on('will-navigate', (event, url) => {
if (/twitter\.com\/intent\/tweet\/complete/.test(url)) {
win.close()
}
event.preventDefault()
})
win.loadURL(url)
}
if (process.env.NODE_ENV === 'development') {
require('electron-debug')()
}
const config = new Config({
defaults: {
bounds: {
width: 960,
height: 680,
},
},
})
function createMainWindow() {
const lastWindowState = config.get('bounds')
const win = new BrowserWindow({
title: 'PixivDeck',
width: lastWindowState.width,
height: lastWindowState.height,
x: lastWindowState.x,
y: lastWindowState.y,
})
if (process.env.NODE_ENV === 'development') {
const loadExtensions = async () => {
const installExtension = require('electron-devtools-installer')
const install = installExtension.default
try {
await install(installExtension.REACT_DEVELOPER_TOOLS)
await install(installExtension.REDUX_DEVTOOLS)
} catch (err) {
console.error(err)
}
}
loadExtensions()
}
win.loadURL(`file://${__dirname}/app/app.html`)
win.on('closed', () => {
mainWindow = null
});
['resize', 'move'].forEach(ev => {
win.on(ev, () => {
config.set('bounds', win.getBounds())
})
})
const {webContents} = win
webContents.on('did-finish-load', () => {
referer('http://www.pixiv.net', win)
})
webContents.on('new-window', (event: Event, url: string) => {
if (/intent\/twitter/.test(url)) {
return
}
event.preventDefault()
shell.openExternal(url)
})
return win
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow()
}
})
app.on('ready', () => {
mainWindow = createMainWindow()
const page = mainWindow.webContents
electron.Menu.setApplicationMenu(appMenu)
ipcMain.on('tweet', (ev, url) => {
openTweet(url)
})
ipcMain.on('wallpaper', async (ev, img) => {
const dl = await download(mainWindow, img)
await wallpaper.set(dl.getSavePath())
})
app.on('before-quit', () => {
page.send('save')
})
})