preload.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // put this preload for main-window to give it prompt()
  2. const { ipcRenderer, } = require('electron')
  3. window.prompt = function(title, val){
  4. return ipcRenderer.sendSync('prompt', {title, val})
  5. }
  6. const sendPinokio = (action) => {
  7. console.log("window.parent == window.top?", window.parent === window.top, action, location.href)
  8. if (window.parent === window.top) {
  9. window.parent.postMessage({
  10. action
  11. }, "*")
  12. }
  13. }
  14. // ONLY WHEN IN CHILD FRAME
  15. if (window.parent === window.top) {
  16. if (window.location !== window.parent.location) {
  17. let prevUrl = document.location.href
  18. sendPinokio({
  19. type: "location",
  20. url: prevUrl
  21. })
  22. setInterval(() => {
  23. const currUrl = document.location.href;
  24. // console.log({ currUrl, prevUrl })
  25. if (currUrl != prevUrl) {
  26. // URL changed
  27. prevUrl = currUrl;
  28. console.log(`URL changed to : ${currUrl}`);
  29. sendPinokio({
  30. type: "location",
  31. url: currUrl
  32. })
  33. }
  34. }, 100);
  35. window.addEventListener("message", (event) => {
  36. if (event.data) {
  37. console.log("event.data = ", event.data)
  38. console.log("location.href = ", location.href)
  39. if (event.data.action === "back") {
  40. history.back()
  41. } else if (event.data.action === "forward") {
  42. history.forward()
  43. } else if (event.data.action === "refresh") {
  44. location.reload()
  45. }
  46. }
  47. })
  48. }
  49. }
  50. //document.addEventListener("DOMContentLoaded", (e) => {
  51. // if (window.parent === window.top) {
  52. // window.parent.postMessage({
  53. // action: {
  54. // type: "title",
  55. // text: document.title
  56. // }
  57. // }, "*")
  58. // }
  59. //})
  60. window.electronAPI = {
  61. send: (type, msg) => {
  62. ipcRenderer.send(type, msg)
  63. }
  64. }