2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-11-16 18:15:08 +00:00
nativefier/app/src/main.js

161 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-07-05 06:08:13 +00:00
/**
* Created by JiaHao on 4/7/15.
*/
var fs = require('fs');
var os = require('os');
var path = require('path');
var electron = require('electron');
var wurl = require('wurl');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var shell = electron.shell;
2016-01-21 18:39:52 +00:00
var ipcMain = electron.ipcMain;
2015-07-05 06:08:13 +00:00
var buildMenu = require('./components/menu/menu');
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
const ZOOM_INTERVAL = 0.1;
2015-07-05 06:08:13 +00:00
var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
app.on('window-all-closed', function () {
if (!isOSX()) {
2015-07-05 06:08:13 +00:00
app.quit();
}
});
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 () {
var currentZoom = 1;
var mainWindow = new BrowserWindow(
2015-07-05 06:08:13 +00:00
{
width: appArgs.width || 1280,
height: appArgs.height || 800,
2015-07-05 06:08:13 +00:00
'web-preferences': {
javascript: true,
plugins: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
2015-07-05 06:08:13 +00:00
}
}
);
2016-01-19 12:28:58 +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);
if (appArgs.userAgent) {
mainWindow.webContents.setUserAgent(appArgs.userAgent);
}
2015-07-05 06:08:13 +00:00
mainWindow.webContents.on('did-finish-load', function () {
mainWindow.webContents.send('params', JSON.stringify(appArgs));
2015-07-05 06:08:13 +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;
}
app.dock.setBadge('●');
});
}
mainWindow.webContents.on('new-window', function (event, urlToGo) {
if (linkIsInternal(appArgs.targetUrl, urlToGo)) {
return;
}
event.preventDefault();
shell.openExternal(urlToGo);
});
mainWindow.loadURL(appArgs.targetUrl);
// if the window is focused, clear the badge
mainWindow.on('focus', function () {
if (!isOSX()) {
return;
}
if (appArgs.badge || appArgs.counter) {
app.dock.setBadge('');
}
});
2015-07-05 06:08:13 +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-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();
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();
});
});
function isOSX() {
return os.platform() === 'darwin';
}
function linkIsInternal(currentUrl, newUrl) {
var currentDomain = wurl('domain', currentUrl);
var newDomain = wurl('domain', newUrl);
return currentDomain === newDomain;
}