diff --git a/app/nativefier.json b/app/nativefier.json index 99a5586..331cc24 100644 --- a/app/nativefier.json +++ b/app/nativefier.json @@ -3,6 +3,5 @@ "targetUrl": "http:\/\/www.google.com", "badge": false, "width": 1280, - "height": 800, - "showDevTools": false + "height": 800 } diff --git a/app/src/components/login/loginWindow.js b/app/src/components/login/loginWindow.js new file mode 100644 index 0000000..ada1416 --- /dev/null +++ b/app/src/components/login/loginWindow.js @@ -0,0 +1,21 @@ +var electron = require('electron'); +var BrowserWindow = electron.BrowserWindow; +var ipcMain = electron.ipcMain; + +function createLoginWindow(loginCallback) { + var loginWindow = new BrowserWindow({ + width: 300, + height: 400, + frame: false, + resizable: false + }); + loginWindow.loadURL('file://' + __dirname + '/static/login/login.html'); + + ipcMain.once('login-message', function(event, usernameAndPassword) { + loginCallback(usernameAndPassword[0], usernameAndPassword[1]); + loginWindow.close(); + }); + return loginWindow; +} + +module.exports = createLoginWindow; diff --git a/app/src/components/mainWindow/mainWindow.js b/app/src/components/mainWindow/mainWindow.js new file mode 100644 index 0000000..4c5c118 --- /dev/null +++ b/app/src/components/mainWindow/mainWindow.js @@ -0,0 +1,108 @@ +var path = require('path'); +var electron = require('electron'); +var helpers = require('./../../helpers/helpers'); +var createMenu = require('./../menu/menu'); + +var BrowserWindow = electron.BrowserWindow; +var shell = electron.shell; +var isOSX = helpers.isOSX; +var linkIsInternal = helpers.linkIsInternal; + +const ZOOM_INTERVAL = 0.1; + +/** + * + * @param {{}} options AppArgs from nativefier.json + * @param {electron.app.quit} onAppQuit + * @param {electron.app.dock.setBadge} setDockBadge + * @returns {electron.BrowserWindow} + */ +function createMainWindow(options, onAppQuit, setDockBadge) { + console.log(__dirname); + var mainWindow = new BrowserWindow( + { + width: options.width || 1280, + height: options.height || 800, + 'web-preferences': { + javascript: true, + plugins: true, + nodeIntegration: false, + preload: path.join(__dirname, 'static', 'preload.js') + } + } + ); + + var currentZoom = 1; + + var onZoomIn = function () { + currentZoom += ZOOM_INTERVAL; + mainWindow.webContents.send('change-zoom', currentZoom); + }; + + var onZoomOut = function () { + currentZoom -= ZOOM_INTERVAL; + mainWindow.webContents.send('change-zoom', currentZoom); + }; + + createMenu(options.nativefierVersion, onAppQuit, onZoomIn, onZoomOut); + + if (options.userAgent) { + mainWindow.webContents.setUserAgent(options.userAgent); + } + + mainWindow.webContents.on('did-finish-load', function () { + mainWindow.webContents.send('params', JSON.stringify(options)); + }); + + if (options.badge || options.counter) { + mainWindow.on('page-title-updated', function () { + + if (!isOSX() || mainWindow.isFocused()) { + return; + } + + if (options.counter) { + var itemCountRegex = /[\(](\d*?)[\)]/; + var match = itemCountRegex.exec(mainWindow.getTitle()); + console.log(mainWindow.getTitle(), match); + if (match) { + setDockBadge(match[1]); + } + return; + } + + setDockBadge('●'); + }); + } + + mainWindow.webContents.on('new-window', function (event, urlToGo) { + if (linkIsInternal(options.targetUrl, urlToGo)) { + return; + } + event.preventDefault(); + shell.openExternal(urlToGo); + }); + + mainWindow.loadURL(options.targetUrl); + // if the window is focused, clear the badge + mainWindow.on('focus', function () { + if (!isOSX()) { + return; + } + if (options.badge || options.counter) { + setDockBadge(''); + } + }); + + mainWindow.on('close', (e) => { + if (isOSX()) { + // this is called when exiting from clicking the cross button on the window + e.preventDefault(); + mainWindow.hide(); + } + }); + + return mainWindow; +} + +module.exports = createMainWindow; diff --git a/app/src/components/menu/menu.js b/app/src/components/menu/menu.js index 07aff01..b992b1b 100644 --- a/app/src/components/menu/menu.js +++ b/app/src/components/menu/menu.js @@ -2,7 +2,14 @@ var electron = require('electron'); var Menu = electron.Menu; var shell = electron.shell; -module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZoomOut) { +/** + * + * @param {string} nativefierVersion + * @param {electron.app.quit} onQuit should be from app.quit + * @param {function} onZoomIn + * @param {function} onZoomOut + */ +function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut) { if (Menu.getApplicationMenu()) return; @@ -51,57 +58,57 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo { label: 'Reload', accelerator: 'CmdOrCtrl+R', - click: function(item, focusedWindow) { + click: function (item, focusedWindow) { if (focusedWindow) focusedWindow.reload(); } }, { label: 'Toggle Full Screen', - accelerator: (function() { + accelerator: (function () { if (process.platform == 'darwin') return 'Ctrl+Command+F'; else return 'F11'; })(), - click: function(item, focusedWindow) { + click: function (item, focusedWindow) { if (focusedWindow) focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); } }, { label: 'Zoom In', - accelerator: (function() { + accelerator: (function () { if (process.platform == 'darwin') return 'Command+='; else return 'Ctrl+='; })(), - click: function() { + click: function () { onZoomIn(); } }, { label: 'Zoom Out', - accelerator: (function() { + accelerator: (function () { if (process.platform == 'darwin') return 'Command+-'; else return 'Ctrl+-'; })(), - click: function() { + click: function () { onZoomOut(); } }, { label: 'Toggle Window Developer Tools', - accelerator: (function() { + accelerator: (function () { if (process.platform == 'darwin') return 'Alt+Command+I'; else return 'Ctrl+Shift+I'; })(), - click: function(item, focusedWindow) { + click: function (item, focusedWindow) { if (focusedWindow) focusedWindow.toggleDevTools(); } @@ -130,11 +137,15 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo submenu: [ { label: `Built with Nativefier v${nativefierVersion}`, - click: function() { shell.openExternal('https://github.com/jiahaog/nativefier') } + click: function () { + shell.openExternal('https://github.com/jiahaog/nativefier') + } }, { label: 'Report an Issue', - click: function() { shell.openExternal('https://github.com/jiahaog/nativefier/issues') } + click: function () { + shell.openExternal('https://github.com/jiahaog/nativefier/issues') + } } ] } @@ -172,7 +183,9 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo { label: 'Quit', accelerator: 'Command+Q', - click: function() { onQuit(); } + click: function () { + onQuit(); + } }, ] }); @@ -189,4 +202,6 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo var menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu); -}; +} + +module.exports = createMenu; diff --git a/app/src/helpers/helpers.js b/app/src/helpers/helpers.js new file mode 100644 index 0000000..1dcc2c9 --- /dev/null +++ b/app/src/helpers/helpers.js @@ -0,0 +1,17 @@ +var wurl = require('wurl'); +var os = require('os'); + +function isOSX() { + return os.platform() === 'darwin'; +} + +function linkIsInternal(currentUrl, newUrl) { + var currentDomain = wurl('domain', currentUrl); + var newDomain = wurl('domain', newUrl); + return currentDomain === newDomain; +} + +module.exports = { + isOSX: isOSX, + linkIsInternal: linkIsInternal +}; diff --git a/app/src/main.js b/app/src/main.js index 7af4a81..89ce4c9 100644 --- a/app/src/main.js +++ b/app/src/main.js @@ -3,24 +3,22 @@ */ var fs = require('fs'); -var os = require('os'); var path = require('path'); var electron = require('electron'); - -var wurl = require('wurl'); +var createMainWindow = require('./components/mainWindow/mainWindow'); +var createLoginWindow = require('./components/login/loginWindow'); +var helpers = require('./helpers/helpers'); var app = electron.app; -var BrowserWindow = electron.BrowserWindow; -var shell = electron.shell; -var ipcMain = electron.ipcMain; - -var buildMenu = require('./components/menu/menu'); +var isOSX = helpers.isOSX; const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json'); -const ZOOM_INTERVAL = 0.1; var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8')); + +var mainWindow; + app.on('window-all-closed', function () { if (!isOSX()) { app.quit(); @@ -49,112 +47,11 @@ app.on('before-quit', () => { }); app.on('ready', function () { - var currentZoom = 1; - - var mainWindow = new BrowserWindow( - { - width: appArgs.width || 1280, - height: appArgs.height || 800, - 'web-preferences': { - javascript: true, - plugins: true, - nodeIntegration: false, - preload: path.join(__dirname, 'preload.js') - } - } - ); - - 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); - } - - mainWindow.webContents.on('did-finish-load', function () { - mainWindow.webContents.send('params', JSON.stringify(appArgs)); - }); - - 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(''); - } - }); - - mainWindow.on('close', (e) => { - if (isOSX()) { - // this is called when exiting from clicking the cross button on the window - e.preventDefault(); - mainWindow.hide(); - } - }); + mainWindow = createMainWindow(appArgs, app.quit, app.dock.setBadge); }); app.on('login', function(event, webContents, request, authInfo, callback) { + // for http authentication event.preventDefault(); - var loginWindow = new BrowserWindow({ - width: 300, - height: 400, - frame: false, - resizable: false - }); - loginWindow.loadURL('file://' + __dirname + '/components/login/login.html'); - - ipcMain.once('login-message', function(event, usernameAndPassword) { - callback(usernameAndPassword[0], usernameAndPassword[1]); - loginWindow.close(); - }); + createLoginWindow(callback); }); - -function isOSX() { - return os.platform() === 'darwin'; -} - -function linkIsInternal(currentUrl, newUrl) { - var currentDomain = wurl('domain', currentUrl); - var newDomain = wurl('domain', newUrl); - return currentDomain === newDomain; -} diff --git a/app/src/components/login/login.css b/app/src/static/login/login.css similarity index 100% rename from app/src/components/login/login.css rename to app/src/static/login/login.css diff --git a/app/src/components/login/login.html b/app/src/static/login/login.html similarity index 100% rename from app/src/components/login/login.html rename to app/src/static/login/login.html diff --git a/app/src/components/login/login.js b/app/src/static/login/login.js similarity index 100% rename from app/src/components/login/login.js rename to app/src/static/login/login.js diff --git a/app/src/preload.js b/app/src/static/preload.js similarity index 92% rename from app/src/preload.js rename to app/src/static/preload.js index 410665f..2b3afa7 100644 --- a/app/src/preload.js +++ b/app/src/static/preload.js @@ -17,7 +17,6 @@ ipc.on('params', function (event, message) { ipc.on('change-zoom', function (event, message) { - console.log('new zom', message); webFrame.setZoomFactor(message); });