Remove use of `webview` tag and instead rely on browser window

This commit is contained in:
Jia Hao 2016-01-22 01:32:21 +08:00
parent 871c2c66aa
commit 7a00fd8533
3 changed files with 22 additions and 78 deletions

View File

@ -4,57 +4,10 @@
var ipc = require('electron').ipcRenderer;
var webViewDiv = document.getElementById('webViewDiv');
webViewDiv.style.visibility = 'hidden';
var webView;
document.addEventListener("DOMContentLoaded", function(event) {
// do things
});
ipc.on('params', function (event, message) {
var appArgs = JSON.parse(message);
document.title = appArgs.name;
webView = document.createElement('webview');
webView.setAttribute('id', 'webView');
webView.setAttribute('src', appArgs.targetUrl);
webView.setAttribute('autosize', 'on');
webView.setAttribute('minwidth', '100');
webView.setAttribute('minheight', '100');
webView.addEventListener('dom-ready', function () {
if (appArgs.userAgent) {
webView.setUserAgent(appArgs.userAgent);
}
});
webView.addEventListener('new-window', function (e) {
require('shell').openExternal(e.url);
});
webView.addEventListener('did-finish-load', function (e) {
webViewDiv.style.visibility = 'visible';
var loadingContainer = document.getElementById('loading-container');
loadingContainer.parentNode.removeChild(loadingContainer);
// We check for desktop notifications by listening to a title change in the webview
// Not elegant, but it will have to do
if (appArgs.badge) {
webView.addEventListener('page-title-set', function (event) {
ipc.send('notification-message', 'TITLE_CHANGED');
});
}
});
webViewDiv.appendChild(webView);
});
ipc.on('toggle-dev-tools', function (event, message) {
if (!message) {
return;
}
if (webView.isDevToolsOpened()) {
webView.closeDevTools();
} else {
webView.openDevTools();
}
});

View File

@ -81,22 +81,7 @@ module.exports = function (mainWindow, nativefierVersion, onQuit) {
if (focusedWindow)
focusedWindow.toggleDevTools();
}
},
{
label: 'Toggle Web Developer Tools',
accelerator: (function() {
if (process.platform == 'darwin')
return 'Alt+Command+J';
else
return 'Ctrl+Shift+J';
})(),
click: function(item, focusedWindow) {
if (focusedWindow) {
mainWindow.webContents.send('toggle-dev-tools', true);
}
}
},
}
]
},
{

View File

@ -5,10 +5,9 @@
var fs = require('fs');
var os = require('os');
var electron = require('electron');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var ipc = electron.ipcMain;
var shell = electron.shell;
var buildMenu = require('./buildMenu');
@ -53,18 +52,34 @@ app.on('ready', function () {
'web-preferences': {
javascript: true,
plugins: true,
nodeIntegration: false,
preload: __dirname + '/assets/js/index.js'
}
}
);
buildMenu(mainWindow, appArgs.nativefierVersion, app.quit);
mainWindow.loadURL('file://' + __dirname + '/index.html');
if (appArgs.userAgent) {
mainWindow.webContents.setUserAgent(appArgs.userAgent);
}
mainWindow.webContents.on('did-finish-load', function () {
mainWindow.webContents.send('params', JSON.stringify(appArgs));
});
mainWindow.on('page-title-updated', function () {
if (isOSX() && !mainWindow.isFocused()) {
app.dock.setBadge('●');
}
});
mainWindow.webContents.on('new-window', function (event, url) {
event.preventDefault();
shell.openExternal(url);
});
mainWindow.loadURL(appArgs.targetUrl);
// if the window is focused, clear the badge
mainWindow.on('focus', function () {
if (isOSX()) {
@ -81,15 +96,6 @@ app.on('ready', function () {
});
});
// listen for a notification message
ipc.on('notification-message', function (event, arg) {
if (arg === 'TITLE_CHANGED') {
if (isOSX() && !mainWindow.isFocused()) {
app.dock.setBadge('●');
}
}
});
function isOSX() {
return os.platform() === 'darwin';
}