electron/atom/renderer/lib/override.js

250 lines
6.6 KiB
JavaScript
Raw Normal View History

2016-01-12 02:40:23 +00:00
var BrowserWindowProxy, a, getHistoryOperation, ipcRenderer, ref, remote, resolveURL, sendHistoryOperation,
slice = [].slice;
ref = require('electron'), ipcRenderer = ref.ipcRenderer, remote = ref.remote;
2016-01-14 18:35:29 +00:00
// Helper function to resolve relative url.
2016-01-12 02:40:23 +00:00
a = window.top.document.createElement('a');
resolveURL = function(url) {
a.href = url;
return a.href;
};
2016-01-14 18:35:29 +00:00
// Window object returned by "window.open".
2016-01-12 02:40:23 +00:00
BrowserWindowProxy = (function() {
BrowserWindowProxy.proxies = {};
BrowserWindowProxy.getOrCreate = function(guestId) {
var base;
return (base = this.proxies)[guestId] != null ? base[guestId] : base[guestId] = new BrowserWindowProxy(guestId);
};
BrowserWindowProxy.remove = function(guestId) {
return delete this.proxies[guestId];
};
function BrowserWindowProxy(guestId1) {
this.guestId = guestId1;
this.closed = false;
ipcRenderer.once("ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_" + this.guestId, (function(_this) {
return function() {
BrowserWindowProxy.remove(_this.guestId);
return _this.closed = true;
};
})(this));
}
BrowserWindowProxy.prototype.close = function() {
return ipcRenderer.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', this.guestId);
};
BrowserWindowProxy.prototype.focus = function() {
return ipcRenderer.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'focus');
};
BrowserWindowProxy.prototype.blur = function() {
return ipcRenderer.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'blur');
};
BrowserWindowProxy.prototype.postMessage = function(message, targetOrigin) {
if (targetOrigin == null) {
targetOrigin = '*';
}
return ipcRenderer.send('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', this.guestId, message, targetOrigin, location.origin);
};
BrowserWindowProxy.prototype["eval"] = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return ipcRenderer.send.apply(ipcRenderer, ['ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'executeJavaScript'].concat(slice.call(args)));
};
return BrowserWindowProxy;
})();
if (process.guestInstanceId == null) {
2016-01-14 18:35:29 +00:00
// Override default window.close.
2016-01-12 02:40:23 +00:00
window.close = function() {
return remote.getCurrentWindow().close();
};
}
2016-01-14 18:35:29 +00:00
// Make the browser window or guest view emit "new-window" event.
2016-01-12 02:40:23 +00:00
window.open = function(url, frameName, features) {
var feature, guestId, i, ints, j, len, len1, name, options, ref1, ref2, value;
if (frameName == null) {
frameName = '';
}
if (features == null) {
features = '';
}
options = {};
ints = ['x', 'y', 'width', 'height', 'min-width', 'max-width', 'min-height', 'max-height', 'zoom-factor'];
2016-01-14 18:35:29 +00:00
// Make sure to get rid of excessive whitespace in the property name
2016-01-12 02:40:23 +00:00
ref1 = features.split(/,\s*/);
for (i = 0, len = ref1.length; i < len; i++) {
feature = ref1[i];
ref2 = feature.split(/\s*=/), name = ref2[0], value = ref2[1];
options[name] = value === 'yes' || value === '1' ? true : value === 'no' || value === '0' ? false : value;
}
if (options.left) {
if (options.x == null) {
options.x = options.left;
}
}
if (options.top) {
if (options.y == null) {
options.y = options.top;
}
}
if (options.title == null) {
options.title = frameName;
}
if (options.width == null) {
options.width = 800;
}
if (options.height == null) {
options.height = 600;
}
2016-01-14 18:35:29 +00:00
// Resolve relative urls.
2016-01-12 02:40:23 +00:00
url = resolveURL(url);
for (j = 0, len1 = ints.length; j < len1; j++) {
name = ints[j];
if (options[name] != null) {
options[name] = parseInt(options[name], 10);
}
}
guestId = ipcRenderer.sendSync('ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, options);
if (guestId) {
return BrowserWindowProxy.getOrCreate(guestId);
} else {
return null;
}
};
2016-01-14 18:35:29 +00:00
// Use the dialog API to implement alert().
2016-01-12 02:40:23 +00:00
window.alert = function(message, title) {
var buttons;
if (title == null) {
title = '';
}
buttons = ['OK'];
message = message.toString();
remote.dialog.showMessageBox(remote.getCurrentWindow(), {
message: message,
title: title,
buttons: buttons
});
2016-01-14 18:35:29 +00:00
// Alert should always return undefined.
2016-01-12 02:40:23 +00:00
};
2016-01-14 18:35:29 +00:00
// And the confirm().
2016-01-12 02:40:23 +00:00
window.confirm = function(message, title) {
var buttons, cancelId;
if (title == null) {
title = '';
}
buttons = ['OK', 'Cancel'];
cancelId = 1;
return !remote.dialog.showMessageBox(remote.getCurrentWindow(), {
message: message,
title: title,
buttons: buttons,
cancelId: cancelId
});
};
2016-01-14 18:35:29 +00:00
// But we do not support prompt().
2016-01-12 02:40:23 +00:00
window.prompt = function() {
throw new Error('prompt() is and will not be supported.');
};
if (process.openerId != null) {
window.opener = BrowserWindowProxy.getOrCreate(process.openerId);
}
ipcRenderer.on('ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', function(event, sourceId, message, sourceOrigin) {
2016-01-14 18:35:29 +00:00
// Manually dispatch event instead of using postMessage because we also need to
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// set event.source.
2016-01-12 02:40:23 +00:00
event = document.createEvent('Event');
event.initEvent('message', false, false);
event.data = message;
event.origin = sourceOrigin;
event.source = BrowserWindowProxy.getOrCreate(sourceId);
return window.dispatchEvent(event);
});
2016-01-14 18:35:29 +00:00
// Forward history operations to browser.
2016-01-12 02:40:23 +00:00
sendHistoryOperation = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return ipcRenderer.send.apply(ipcRenderer, ['ATOM_SHELL_NAVIGATION_CONTROLLER'].concat(slice.call(args)));
};
getHistoryOperation = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return ipcRenderer.sendSync.apply(ipcRenderer, ['ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER'].concat(slice.call(args)));
};
window.history.back = function() {
return sendHistoryOperation('goBack');
};
window.history.forward = function() {
return sendHistoryOperation('goForward');
};
window.history.go = function(offset) {
return sendHistoryOperation('goToOffset', offset);
};
Object.defineProperty(window.history, 'length', {
get: function() {
return getHistoryOperation('length');
}
});
2016-01-14 18:35:29 +00:00
// Make document.hidden and document.visibilityState return the correct value.
2016-01-12 02:40:23 +00:00
Object.defineProperty(document, 'hidden', {
get: function() {
var currentWindow;
currentWindow = remote.getCurrentWindow();
return currentWindow.isMinimized() || !currentWindow.isVisible();
}
});
Object.defineProperty(document, 'visibilityState', {
get: function() {
if (document.hidden) {
return "hidden";
} else {
return "visible";
}
}
});