feat: introduce LocationProxy for BrowserWindowProxy
This commit is contained in:
parent
1f55f1635f
commit
e80e3a53e9
1 changed files with 49 additions and 11 deletions
|
@ -23,7 +23,7 @@
|
|||
// - document.hidden
|
||||
// - document.visibilityState
|
||||
|
||||
const { defineProperty } = Object
|
||||
const { defineProperty, defineProperties } = Object
|
||||
|
||||
// Helper function to resolve relative url.
|
||||
const a = window.top.document.createElement('a')
|
||||
|
@ -54,18 +54,56 @@ const removeProxy = (guestId) => {
|
|||
delete windowProxies[guestId]
|
||||
}
|
||||
|
||||
function LocationProxy (ipcRenderer, guestId) {
|
||||
const getGuestURL = function () {
|
||||
const urlString = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
|
||||
try {
|
||||
return new URL(urlString)
|
||||
} catch (e) {
|
||||
console.error('LocationProxy: failed to parse string', urlString, e)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const propertyProxyFor = function (property) {
|
||||
return {
|
||||
get: function () {
|
||||
const guestURL = getGuestURL()
|
||||
return guestURL ? guestURL[property] : ''
|
||||
},
|
||||
set: function (newVal) {
|
||||
const guestURL = getGuestURL()
|
||||
if (guestURL) {
|
||||
guestURL[property] = newVal
|
||||
return ipcRenderer.sendSync(
|
||||
'ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC',
|
||||
guestId, 'loadURL', guestURL.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
defineProperties(this, {
|
||||
hash: propertyProxyFor('hash'),
|
||||
href: propertyProxyFor('href'),
|
||||
host: propertyProxyFor('host'),
|
||||
hostname: propertyProxyFor('hostname'),
|
||||
origin: propertyProxyFor('origin'),
|
||||
pathname: propertyProxyFor('pathname'),
|
||||
port: propertyProxyFor('port'),
|
||||
protocol: propertyProxyFor('protocol'),
|
||||
search: propertyProxyFor('search')
|
||||
})
|
||||
|
||||
this.toString = function () {
|
||||
return this.href
|
||||
}
|
||||
}
|
||||
|
||||
function BrowserWindowProxy (ipcRenderer, guestId) {
|
||||
this.closed = false
|
||||
|
||||
defineProperty(this, 'location', {
|
||||
get: function () {
|
||||
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
|
||||
},
|
||||
set: function (url) {
|
||||
url = resolveURL(url)
|
||||
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'loadURL', url)
|
||||
}
|
||||
})
|
||||
this.location = new LocationProxy(ipcRenderer, guestId)
|
||||
|
||||
ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => {
|
||||
removeProxy(guestId)
|
||||
|
|
Loading…
Add table
Reference in a new issue