-
Notifications
You must be signed in to change notification settings - Fork 331
/
preload.js
68 lines (64 loc) · 1.74 KB
/
preload.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
66
67
68
// put this preload for main-window to give it prompt()
const { ipcRenderer, } = require('electron')
window.prompt = function(title, val){
return ipcRenderer.sendSync('prompt', {title, val})
}
const sendPinokio = (action) => {
console.log("window.parent == window.top?", window.parent === window.top, action, location.href)
if (window.parent === window.top) {
window.parent.postMessage({
action
}, "*")
}
}
// ONLY WHEN IN CHILD FRAME
if (window.parent === window.top) {
if (window.location !== window.parent.location) {
let prevUrl = document.location.href
sendPinokio({
type: "location",
url: prevUrl
})
setInterval(() => {
const currUrl = document.location.href;
// console.log({ currUrl, prevUrl })
if (currUrl != prevUrl) {
// URL changed
prevUrl = currUrl;
console.log(`URL changed to : ${currUrl}`);
sendPinokio({
type: "location",
url: currUrl
})
}
}, 100);
window.addEventListener("message", (event) => {
if (event.data) {
console.log("event.data = ", event.data)
console.log("location.href = ", location.href)
if (event.data.action === "back") {
history.back()
} else if (event.data.action === "forward") {
history.forward()
} else if (event.data.action === "refresh") {
location.reload()
}
}
})
}
}
//document.addEventListener("DOMContentLoaded", (e) => {
// if (window.parent === window.top) {
// window.parent.postMessage({
// action: {
// type: "title",
// text: document.title
// }
// }, "*")
// }
//})
window.electronAPI = {
send: (type, msg) => {
ipcRenderer.send(type, msg)
}
}