-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
195 lines (144 loc) · 5.48 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Created by hamza on 7/18/15.
*/
var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
var path = require('path');
var Menu = require("menu");
var Tray = require('tray');
var events = require('events');
var fs = require('fs');
var globalShortcut = require('global-shortcut');
var clipboard = require('clipboard');
var express = require('express');
var webapp = express();
var http = require('http').Server(webapp);
// load locallydb
var locallydb = require('locallydb');
// load the database (folder) in './mydb', will be created if doesn't exist
var db = new locallydb(path.join(app.getPath('appData'), 'yt-database-links'));
// load the collection (file) in './mydb/monsters', will be created if doesn't exist
global.collection = db.collection('links');
var iconNotify = (process.platform === 'win32') ? 'alertLarge.png' : 'alert.png';
global.alertIcon = path.join(__dirname, 'app/images/' + iconNotify);
global.app = app;
// Report crashes to our server.
require('crash-reporter').start();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;
app.on('window-all-closed', function() {
if(process.platform !== 'darwin') {
app.quit();
}
});
// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
// Register a 'ctrl+x' shortcut listener.
var ret = globalShortcut.register(process.platform === 'win32' ? 'ctrl+g' : 'cmd+g', function() {
console.log('cmd+g/ctrl+g is pressed');
console.log(clipboard.readText('selection'));
var url = clipboard.readText('selection');
mainWindow.webContents.executeJavaScript("addNewURL('"+url+"');");
});
if (!ret) {
console.log('registration failed');
}
if (app.dock) app.dock.hide();
var iconPath = path.join(__dirname, '/app/images/IconTemplate.png'); // default cat icon
var electronScreen = require('screen');
var cachedBounds; // cachedBounds are needed for double-clicked event
appTry = new Tray(iconPath);
appTry.setToolTip("Youtube Music Player");
appTry
.on('click', clicked);
createWindow(false);
function clicked (e, bounds) {
if (mainWindow && mainWindow.isVisible()) return hideWindow();
// workarea takes the taskbar/menubar height in consideration
var size = electronScreen.getDisplayNearestPoint(electronScreen.getCursorScreenPoint()).workArea;
if (bounds) cachedBounds = bounds;
// ensure bounds is an object
bounds = bounds || {x:0,y:0};
// bounds may not be populated on all OSes
if (bounds.x === 0 && bounds.y === 0) {
// default to bottom on windows
bounds.x = size.width + size.x - (340 / 2); // default to right
cachedBounds = bounds;
}
showWindow(bounds);
}
function showWindow (trayPos) {
var x = Math.floor(trayPos.x - ((340 / 2) || 200) + (trayPos.width / 2));
var y = process.platform === 'win32' ? trayPos.y - 600 : trayPos.y;
if (!mainWindow) {
createWindow(true, x, y)
}
if (mainWindow) {
mainWindow.show();
//mainWindow.openDevTools();
mainWindow.setPosition(x, y);
}
}
function hideWindow () {
if (!mainWindow) return;
mainWindow.hide();
}
function createWindow (show, x, y) {
// Create the browser window.
mainWindow = new BrowserWindow({
show: show,
width: 340,
height: 594,
resizable : false,
frame: false,
'always-on-top': false,
'web-preferences': {
'web-security': true,
'plugins': true ,
'overlay-fullscreen-video': true
},
icon: __dirname + '/app/images/IconTemplate.png'
});
if (show) {
mainWindow.setPosition(x, y);
}
// Open the devtools.
// TODO: depending on which environment
// mainWindow.openDevTools();
// and load the index.html of the app.
mainWindow.loadUrl('http://127.0.0.1:3000');
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
var template = [{
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
]}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
});
webapp.use('/vendor', express.static(__dirname +'/vendor'));
webapp.use('/stylesheets', express.static(__dirname +'/app/stylesheets'));
webapp.use('/scripts', express.static(__dirname +'/app/scripts'));
webapp.use('/images', express.static(__dirname +'/app/images'));
webapp.get('/', function (req, res) {
res.sendfile(__dirname + '/app/index.html');
});
var server = http.listen(3000, '127.0.0.1', function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});