2015-07-05 06:08:13 +00:00
|
|
|
/**
|
|
|
|
* Created by JiaHao on 4/7/15.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs');
|
2016-01-21 04:25:33 +00:00
|
|
|
var os = require('os');
|
2016-01-22 11:57:39 +00:00
|
|
|
var path = require('path');
|
2016-01-19 03:28:04 +00:00
|
|
|
var electron = require('electron');
|
2016-01-21 18:05:50 +00:00
|
|
|
|
2016-01-22 10:03:35 +00:00
|
|
|
var wurl = require('wurl');
|
|
|
|
|
2016-01-19 03:28:04 +00:00
|
|
|
var app = electron.app;
|
|
|
|
var BrowserWindow = electron.BrowserWindow;
|
2016-01-21 17:32:21 +00:00
|
|
|
var shell = electron.shell;
|
2016-01-21 18:39:52 +00:00
|
|
|
var ipcMain = electron.ipcMain;
|
2015-07-05 06:08:13 +00:00
|
|
|
|
2016-01-22 11:57:39 +00:00
|
|
|
var buildMenu = require('./components/menu/menu');
|
2016-01-19 11:53:10 +00:00
|
|
|
|
2016-01-22 11:57:39 +00:00
|
|
|
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
|
2016-01-23 01:27:09 +00:00
|
|
|
const ZOOM_INTERVAL = 0.1;
|
2015-07-05 06:08:13 +00:00
|
|
|
|
2015-07-06 02:31:09 +00:00
|
|
|
var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
|
|
|
|
|
2016-01-21 04:25:33 +00:00
|
|
|
app.on('window-all-closed', function () {
|
|
|
|
if (!isOSX()) {
|
2015-07-05 06:08:13 +00:00
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-21 04:25:33 +00:00
|
|
|
app.on('activate', function (event, hasVisibleWindows) {
|
|
|
|
if (isOSX()) {
|
|
|
|
// this is called when the dock is clicked
|
|
|
|
if (!hasVisibleWindows) {
|
|
|
|
mainWindow.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('before-quit', () => {
|
|
|
|
// not fired when the close button on the window is clicked
|
|
|
|
if (isOSX()) {
|
|
|
|
// need to force a quit as a workaround here to simulate the osx app hiding behaviour
|
|
|
|
// Somehow sokution at https://github.com/atom/electron/issues/444#issuecomment-76492576 does not work,
|
|
|
|
// e.prevent default appears to persist
|
|
|
|
|
|
|
|
// might cause issues in the future as before-quit and will-quit events are not called
|
|
|
|
app.exit(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('ready', function () {
|
2016-01-23 01:27:09 +00:00
|
|
|
var currentZoom = 1;
|
|
|
|
|
|
|
|
var mainWindow = new BrowserWindow(
|
2015-07-05 06:08:13 +00:00
|
|
|
{
|
2015-07-06 02:31:09 +00:00
|
|
|
width: appArgs.width || 1280,
|
|
|
|
height: appArgs.height || 800,
|
2015-07-05 06:08:13 +00:00
|
|
|
'web-preferences': {
|
|
|
|
javascript: true,
|
|
|
|
plugins: true,
|
2016-01-21 17:32:21 +00:00
|
|
|
nodeIntegration: false,
|
2016-01-22 11:57:39 +00:00
|
|
|
preload: path.join(__dirname, 'preload.js')
|
2015-07-05 06:08:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2016-01-19 12:28:58 +00:00
|
|
|
|
2016-01-23 01:27:09 +00:00
|
|
|
var onZoomIn = function () {
|
|
|
|
currentZoom += ZOOM_INTERVAL;
|
|
|
|
mainWindow.webContents.send('change-zoom', currentZoom);
|
|
|
|
};
|
|
|
|
|
|
|
|
var onZoomOut = function () {
|
|
|
|
currentZoom -= ZOOM_INTERVAL;
|
|
|
|
mainWindow.webContents.send('change-zoom', currentZoom);
|
|
|
|
};
|
|
|
|
|
|
|
|
buildMenu(mainWindow, appArgs.nativefierVersion, app.quit, onZoomIn, onZoomOut);
|
2016-01-19 03:30:42 +00:00
|
|
|
|
2016-01-21 17:32:21 +00:00
|
|
|
if (appArgs.userAgent) {
|
|
|
|
mainWindow.webContents.setUserAgent(appArgs.userAgent);
|
|
|
|
}
|
2015-07-05 06:08:13 +00:00
|
|
|
|
2016-01-21 04:25:33 +00:00
|
|
|
mainWindow.webContents.on('did-finish-load', function () {
|
2015-07-06 02:31:09 +00:00
|
|
|
mainWindow.webContents.send('params', JSON.stringify(appArgs));
|
2015-07-05 06:08:13 +00:00
|
|
|
});
|
|
|
|
|
2016-01-22 03:35:05 +00:00
|
|
|
if (appArgs.badge || appArgs.counter) {
|
|
|
|
mainWindow.on('page-title-updated', function () {
|
|
|
|
|
|
|
|
if (!isOSX() || mainWindow.isFocused()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appArgs.counter) {
|
|
|
|
var itemCountRegex = /[\(](\d*?)[\)]/;
|
|
|
|
var match = itemCountRegex.exec(mainWindow.getTitle());
|
|
|
|
console.log(mainWindow.getTitle(), match);
|
|
|
|
if (match) {
|
|
|
|
app.dock.setBadge(match[1]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:32:21 +00:00
|
|
|
app.dock.setBadge('●');
|
2016-01-22 03:35:05 +00:00
|
|
|
});
|
|
|
|
}
|
2016-01-21 17:32:21 +00:00
|
|
|
|
2016-01-21 18:05:50 +00:00
|
|
|
mainWindow.webContents.on('new-window', function (event, urlToGo) {
|
|
|
|
if (linkIsInternal(appArgs.targetUrl, urlToGo)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 17:32:21 +00:00
|
|
|
event.preventDefault();
|
2016-01-22 10:04:02 +00:00
|
|
|
shell.openExternal(urlToGo);
|
2016-01-21 17:32:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.loadURL(appArgs.targetUrl);
|
2015-07-06 01:38:04 +00:00
|
|
|
// if the window is focused, clear the badge
|
|
|
|
mainWindow.on('focus', function () {
|
2016-01-22 03:35:05 +00:00
|
|
|
if (!isOSX()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (appArgs.badge || appArgs.counter) {
|
2015-08-06 03:34:32 +00:00
|
|
|
app.dock.setBadge('');
|
|
|
|
}
|
2015-07-06 01:38:04 +00:00
|
|
|
});
|
2015-07-05 06:08:13 +00:00
|
|
|
|
2016-01-21 04:25:33 +00:00
|
|
|
mainWindow.on('close', (e) => {
|
|
|
|
if (isOSX()) {
|
|
|
|
// this is called when exiting from clicking the cross button on the window
|
|
|
|
e.preventDefault();
|
|
|
|
mainWindow.hide();
|
|
|
|
}
|
2015-07-06 01:38:04 +00:00
|
|
|
});
|
|
|
|
});
|
2015-07-05 06:08:13 +00:00
|
|
|
|
2016-01-21 18:39:52 +00:00
|
|
|
app.on('login', function(event, webContents, request, authInfo, callback) {
|
|
|
|
event.preventDefault();
|
2016-01-21 19:25:23 +00:00
|
|
|
var loginWindow = new BrowserWindow({
|
|
|
|
width: 300,
|
|
|
|
height: 400,
|
|
|
|
frame: false,
|
|
|
|
resizable: false
|
|
|
|
});
|
2016-01-21 18:39:52 +00:00
|
|
|
loginWindow.loadURL('file://' + __dirname + '/components/login/login.html');
|
|
|
|
|
|
|
|
ipcMain.once('login-message', function(event, usernameAndPassword) {
|
|
|
|
callback(usernameAndPassword[0], usernameAndPassword[1]);
|
|
|
|
loginWindow.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-21 04:25:33 +00:00
|
|
|
function isOSX() {
|
|
|
|
return os.platform() === 'darwin';
|
|
|
|
}
|
2016-01-21 18:05:50 +00:00
|
|
|
|
|
|
|
function linkIsInternal(currentUrl, newUrl) {
|
|
|
|
var currentDomain = wurl('domain', currentUrl);
|
|
|
|
var newDomain = wurl('domain', newUrl);
|
|
|
|
return currentDomain === newDomain;
|
|
|
|
}
|