-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
165 lines (142 loc) · 4.08 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
163
164
165
const electron = require( "electron" );
const path = require('path');
const { shell, app, Tray, Menu, BrowserWindow, globalShortcut } = electron;
const HOMEPAGE = "https://play.pocketcasts.com/podcasts"
const fs = require('fs');
let iconPath = path.join(__dirname, 'tray-icon.png');
let mainWindow;
let appIcon = null;
class Store {
constructor(opts) {
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
this.path = path.join(userDataPath, opts.configName + '.json');
this.data = parseDataFile(this.path, opts.defaults);
}
get(key) {
return this.data[key];
}
set(key, val) {
this.data[key] = val;
fs.writeFileSync(this.path, JSON.stringify(this.data));
}
}
function parseDataFile(filePath, defaults) {
try {
return JSON.parse(fs.readFileSync(filePath));
} catch (error) {
return defaults;
}
}
function getValidatedWindowDimension(dimen) {
if (dimen <= 0) return 1
else return dimen
}
// Avoid Multiple Instances
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) { app.quit(); }
app.on( "ready", () => {
const userPrefs = new Store({
configName: 'prefs',
defaults: {
windowWidth: 1200,
windowHeight: 900
}
});
mainWindow = new BrowserWindow({
width: getValidatedWindowDimension(userPrefs.get('windowWidth')),
height: getValidatedWindowDimension(userPrefs.get('windowHeight')),
webPreferences: {
nodeIntegration: false
},
show: false
});
if (userPrefs.get("windowMaximized") == true){
// if the user starts the app maximized, set the unmaximized size to the default window size
mainWindow.setSize(1200, 900)
mainWindow.maximize()
}
mainWindow.show()
var contextMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: function(){
mainWindow.show();
}
},
{
type: 'separator'
},
{
label: 'Play / Pause',
click: function(){
mainWindow.webContents.executeJavaScript( "document.querySelector( '.play_pause_button' ).click()");
}
},
{
label: 'Skip forward 30 sec',
click: function(){
mainWindow.webContents.executeJavaScript( "document.querySelector( '.skip_forward_button' ).click()");
}
},
{
label: 'Skip back 15 sec',
click: function(){
mainWindow.webContents.executeJavaScript( "document.querySelector( '.skip_back_button' ).click()");
}
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Alt+F4',
click: function(){
app.quit();
}
}
]);
if (iconPath.includes('/app.asar/')){
iconPath = iconPath.replace('/app.asar/', '/')
}
appIcon = new Tray(iconPath);
appIcon.setToolTip('Pocket Casts');
appIcon.setContextMenu(contextMenu);
mainWindow.setMenuBarVisibility( false );
mainWindow.loadURL( HOMEPAGE );
mainWindow.webContents.on( "will-navigate", ( ev, url ) => {
parts = url.split( '/' );
if( parts[0] + "//" + parts[2] != HOMEPAGE ){
ev.preventDefault();
shell.openExternal( url );
}
});
// Register media controls
globalShortcut.register( 'MediaPlayPause', () => {
mainWindow.webContents.executeJavaScript( "document.querySelector( '.play_pause_button' ).click()");
});
globalShortcut.register( 'MediaPreviousTrack', () => {
mainWindow.webContents.executeJavaScript( "document.querySelector( '.skip_back_button' ).click()");
});
globalShortcut.register( 'MediaNextTrack', () => {
mainWindow.webContents.executeJavaScript( "document.querySelector( '.skip_forward_button' ).click()");
});
mainWindow.on( "resize", () => {
let { width, height } = mainWindow.getBounds();
userPrefs.set('windowWidth', width);
userPrefs.set('windowHeight', height);
});
mainWindow.on( "maximize", () => {
userPrefs.set('windowMaximized', true);
});
mainWindow.on( "unmaximize", () => {
userPrefs.set('windowMaximized', false);
});
mainWindow.on( "closed", () => {
mainWindow = null;
});
});
app.on( "window-all-closed", () => {
if( process.platform !== "darwin" ){
app.quit()
}
});