sandbox.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <html>
  2. <script type="text/javascript" charset="utf-8">
  3. function timeout(ms) {
  4. return new Promise((resolve) => {
  5. setTimeout(resolve, ms)
  6. })
  7. }
  8. async function invokeGc () {
  9. // it seems calling window.gc once does not guarantee garbage will be
  10. // collected, so we repeat 10 times with interval of 100 ms
  11. for (let i = 0; i < 10; i++) {
  12. window.gc()
  13. await timeout(100)
  14. }
  15. }
  16. const [,test] = window.location.href.split('?')
  17. if (window.opener && test !== 'reload-remote') {
  18. window.callback = () => {
  19. opener.require('electron').ipcRenderer.send('answer', document.body.innerHTML)
  20. }
  21. } else {
  22. const {ipcRenderer, remote} = require('electron')
  23. const tests = {
  24. 'reload-remote-child': () => {
  25. open(`${location.protocol}//${location.pathname}?reload-remote`)
  26. },
  27. 'reload-remote': async () => {
  28. const p = ipcRenderer.sendSync('get-remote-module-path')
  29. const Hello = remote.require(p)
  30. if (!ipcRenderer.sendSync('reloaded')) {
  31. ipcRenderer.send('reload')
  32. return
  33. }
  34. await invokeGc()
  35. ipcRenderer.send('answer', new Hello().say())
  36. },
  37. 'allocate-memory': async () => {
  38. await invokeGc()
  39. const {privateBytes: bytesBeforeOpen} = process.getProcessMemoryInfo()
  40. let w = open('./allocate-memory.html')
  41. await invokeGc()
  42. const {privateBytes: bytesAfterOpen} = process.getProcessMemoryInfo()
  43. w.close()
  44. w = null
  45. await invokeGc()
  46. const {privateBytes: bytesAfterClose} = process.getProcessMemoryInfo()
  47. ipcRenderer.send('answer', {
  48. bytesBeforeOpen, bytesAfterOpen, bytesAfterClose
  49. })
  50. },
  51. 'window-events': () => {
  52. document.title = 'changed'
  53. },
  54. 'webcontents-stop': () => {
  55. stop()
  56. },
  57. 'webcontents-events': () => {
  58. addEventListener('load', () => {
  59. location.hash = 'in-page-navigate'
  60. setTimeout(() => {
  61. location.reload()
  62. }, 50)
  63. })
  64. },
  65. 'exit-event': () => {
  66. process.on('exit', () => {
  67. ipcRenderer.send('answer', location.href)
  68. })
  69. location.assign('http://www.google.com')
  70. },
  71. 'window-open': () => {
  72. addEventListener('load', () => {
  73. popup = open(window.location.href, 'popup!', 'top=60,left=50,width=500,height=600')
  74. popup.addEventListener('DOMContentLoaded', () => {
  75. popup.document.write('<h1>scripting from opener</h1>')
  76. popup.callback()
  77. }, false)
  78. })
  79. },
  80. 'window-open-external': () => {
  81. addEventListener('load', () => {
  82. ipcRenderer.once('child-loaded', (e) => {
  83. try {
  84. let childDoc = popup.document
  85. } catch (e) {
  86. ipcRenderer.send('answer', e.message)
  87. }
  88. })
  89. popup = open('http://www.google.com/#q=electron', '', 'top=65,left=55,width=505,height=605')
  90. })
  91. },
  92. 'verify-ipc-sender': () => {
  93. popup = open()
  94. ipcRenderer.once('verified', () => {
  95. ipcRenderer.send('parent-answer')
  96. })
  97. popup.ipcRenderer.once('verified', () => {
  98. popup.ipcRenderer.send('child-answer')
  99. })
  100. setTimeout(() => {
  101. ipcRenderer.send('parent-ready')
  102. popup.ipcRenderer.send('child-ready')
  103. }, 50)
  104. }
  105. }
  106. addEventListener('unload', () => {
  107. if (window.popup)
  108. popup.close()
  109. }, false)
  110. if (tests.hasOwnProperty(test))
  111. tests[test]()
  112. }
  113. </script>
  114. </html>