-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
110 lines (92 loc) · 2.23 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
const { app, BrowserWindow, globalShortcut } = require('electron');
let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false
}
});
mainWindow.loadURL('https://music.amazon.in/');
handleExternalNavigation(mainWindow.webContents);
bindHelperShortcuts(mainWindow.webContents);
mainWindow.on('close', function (e) {
if (!mainWindow.forceClose)
e.preventDefault();
mainWindow.hide();
});
mainWindow.on('closed', function () {
mainWindow = null;
});
});
app.on('window-all-closed', () => {
globalShortcut.unregisterAll();
app.quit();
});
app.on('before-quit', () => {
mainWindow.forceClose = true;
});
app.on('activate', () => {
mainWindow.show();
});
/**
* Opens a link using system's open command
*
* @param {*} target
* @param {*} appName
* @param {*} callback
*/
const open = (target, appName, callback) => {
let opener;
if (typeof (appName) === 'function') {
callback = appName;
appName = null;
}
opener = appName ? 'open -a "' + escape(appName) + '"' : 'open';
if (process.env.SUDO_USER)
opener = 'sudo -u ' + process.env.SUDO_USER + ' ' + opener;
return exec(opener + ' "' + escape(target) + '"', callback);
};
/**
* Handle navigation of page when links are clicked.
*
* @param {*} webView
*/
const handleExternalNavigation = (webView) => {
// Navigation restricted to mainWindow only
webView.on('will-navigate', (e, url) => {
e.preventDefault()
// Opening saavn urls
if (url.replace(/https?:\/\//, '').indexOf('music.amazon.in') == 0)
mainWindow.loadURL(url)
else
// Open External URLs in the default web browser
open(url)
});
};
/**
* Bind Media Shortcuts and a couple of helpers
*
* @param {*} webView
*/
const bindHelperShortcuts = (webView) => {
globalShortcut.register('MediaPlayPause', () => {
webView.sendInputEvent({
type: 'keyUp',
keyCode: 'Space'
})
});
globalShortcut.register('MediaNextTrack', () => {
webView.sendInputEvent({
type: 'keyUp',
keyCode: 'Right'
})
});
globalShortcut.register('MediaPreviousTrack', () => {
webView.sendInputEvent({
type: 'keyUp',
keyCode: 'Left'
})
});
};