electron/atom/renderer/lib/override.coffee

130 lines
4.2 KiB
CoffeeScript
Raw Normal View History

{ipcRenderer, remote} = require 'electron'
2014-03-01 12:00:39 +00:00
# Helper function to resolve relative url.
a = window.top.document.createElement 'a'
2015-11-13 08:03:40 +00:00
resolveURL = (url) ->
a.href = url
a.href
# Window object returned by "window.open".
class BrowserWindowProxy
2015-12-11 01:20:10 +00:00
@proxies: {}
@getOrCreate: (guestId) ->
@proxies[guestId] ?= new BrowserWindowProxy(guestId)
2015-12-11 01:22:05 +00:00
@remove: (guestId) ->
2015-12-11 01:20:10 +00:00
delete @proxies[guestId]
2014-10-28 05:23:25 +00:00
constructor: (@guestId) ->
@closed = false
ipcRenderer.once "ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_#{@guestId}", =>
2015-12-11 01:22:05 +00:00
BrowserWindowProxy.remove(@guestId)
@closed = true
close: ->
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', @guestId
2014-10-27 15:03:05 +00:00
focus: ->
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', @guestId, 'focus'
2014-10-27 15:03:05 +00:00
blur: ->
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', @guestId, 'blur'
2014-10-27 15:03:05 +00:00
2015-03-04 15:58:06 +00:00
postMessage: (message, targetOrigin='*') ->
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', @guestId, message, targetOrigin, location.origin
2014-10-27 15:07:41 +00:00
eval: (args...) ->
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', @guestId, 'executeJavaScript', args...
2014-10-27 15:07:41 +00:00
unless process.guestInstanceId?
# Override default window.close.
window.close = ->
remote.getCurrentWindow().close()
2014-03-01 12:00:39 +00:00
# Make the browser window or guest view emit "new-window" event.
window.open = (url, frameName='', features='') ->
options = {}
ints = [ 'x', 'y', 'width', 'height', 'min-width', 'max-width', 'min-height', 'max-height', 'zoom-factor' ]
# Make sure to get rid of excessive whitespace in the property name
for feature in features.split /,\s*/
[name, value] = feature.split /\s*=/
options[name] =
if value is 'yes' or value is '1'
true
else if value is 'no' or value is '0'
false
else
value
options.x ?= options.left if options.left
options.y ?= options.top if options.top
options.title ?= frameName
options.width ?= 800
options.height ?= 600
2014-03-01 12:00:39 +00:00
# Resolve relative urls.
2015-11-13 08:03:40 +00:00
url = resolveURL url
(options[name] = parseInt(options[name], 10) if options[name]?) for name in ints
guestId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, options
if guestId
2015-12-11 01:20:10 +00:00
BrowserWindowProxy.getOrCreate(guestId)
else
null
# Use the dialog API to implement alert().
window.alert = (message, title='') ->
buttons = ['OK']
message = message.toString()
remote.dialog.showMessageBox remote.getCurrentWindow(), {message, title, buttons}
# Alert should always return undefined.
return
2014-03-01 12:03:49 +00:00
# And the confirm().
window.confirm = (message, title='') ->
buttons = ['OK', 'Cancel']
cancelId = 1
not remote.dialog.showMessageBox remote.getCurrentWindow(), {message, title, buttons, cancelId}
2014-03-01 12:05:52 +00:00
# But we do not support prompt().
window.prompt = ->
throw new Error('prompt() is and will not be supported.')
2015-12-17 13:27:14 +00:00
if process.openerId?
window.opener = BrowserWindowProxy.getOrCreate process.openerId
ipcRenderer.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, sourceId, message, sourceOrigin) ->
# Manually dispatch event instead of using postMessage because we also need to
# set event.source.
event = document.createEvent 'Event'
event.initEvent 'message', false, false
event.data = message
event.origin = sourceOrigin
event.source = BrowserWindowProxy.getOrCreate(sourceId)
window.dispatchEvent event
# Forward history operations to browser.
sendHistoryOperation = (args...) ->
ipcRenderer.send 'ATOM_SHELL_NAVIGATION_CONTROLLER', args...
2015-05-19 17:11:03 +00:00
getHistoryOperation = (args...) ->
ipcRenderer.sendSync 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', args...
2015-05-19 17:11:03 +00:00
window.history.back = -> sendHistoryOperation 'goBack'
window.history.forward = -> sendHistoryOperation 'goForward'
2015-05-11 08:44:01 +00:00
window.history.go = (offset) -> sendHistoryOperation 'goToOffset', offset
2015-05-19 17:11:03 +00:00
Object.defineProperty window.history, 'length',
get: ->
getHistoryOperation 'length'
2015-10-01 10:39:35 +00:00
2015-12-18 22:53:30 +00:00
# Make document.hidden and document.visibilityState return the correct value.
2015-10-01 10:39:35 +00:00
Object.defineProperty document, 'hidden',
2015-12-18 22:53:30 +00:00
get: ->
currentWindow = remote.getCurrentWindow()
2015-12-21 12:20:40 +00:00
currentWindow.isMinimized() || !currentWindow.isVisible()
2015-12-18 22:53:30 +00:00
Object.defineProperty document, 'visibilityState',
get: ->
if document.hidden then "hidden" else "visible"