This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
polyfills.js
65 lines (58 loc) · 2.41 KB
/
polyfills.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* global MutationObserver */
import console from './console.js'
import ipc from './ipc.js'
export function applyPolyfills (window) {
Object.defineProperties(window, Object.getOwnPropertyDescriptors({
resizeTo (width, height) {
const index = window.__args.index
const o = new URLSearchParams({ index, width, height }).toString()
return ipc.postMessage(`ipc://window.setSize?${o}`)
},
// TODO(@heapwolf) the properties do not yet conform to the MDN spec
async showOpenFilePicker (o) {
console.warn('window.showOpenFilePicker may not conform to the standard')
const { data } = await ipc.send('dialog', { type: 'open', ...o })
return typeof data === 'string' ? data.split('\n') : []
},
// TODO(@heapwolf) the properties do not yet conform to the MDN spec
async showSaveFilePicker (o) {
console.warn('window.showSaveFilePicker may not conform to the standard')
const { data } = await ipc.send('dialog', { type: 'save', ...o })
return typeof data === 'string' ? data.split('\n') : []
},
// TODO(@heapwolf) the properties do not yet conform to the MDN spec
async showDirectoryFilePicker (o) {
console.warn('window.showDirectoryFilePicker may not conform to the standard')
const { data } = await ipc.send('dialog', { allowDirs: true, ...o })
return typeof data === 'string' ? data.split('\n') : []
}
}))
// create <title> tag in document if it doesn't exist
window.document.title ||= ''
// initial value
window.addEventListener('DOMContentLoaded', async () => {
const title = window.document.title
if (title.length !== 0) {
const index = window.__args.index
const o = new URLSearchParams({ value: title, index }).toString()
ipc.postMessage(`ipc://window.setTitle?${o}`)
}
})
//
// window.document is uncofigurable property so we need to use MutationObserver here
//
const observer = new MutationObserver((mutationList) => {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
const index = window.__args.index
const title = mutation.addedNodes[0].textContent
const o = new URLSearchParams({ value: title, index }).toString()
ipc.postMessage(`ipc://window.setTitle?${o}`)
}
}
})
const titleElement = document.querySelector('head > title')
if (titleElement) {
observer.observe(titleElement, { childList: true })
}
}