file_window_close.html (1972B)
1 <html> 2 <head> 3 <script> 4 var b = new BroadcastChannel("windowclose"); 5 6 function isInitialLoad() { 7 return location.search.substr(1) != "withhistory" || history.length == 1; 8 } 9 10 function run() { 11 if (location.search.substr(1) == "withhistory") { 12 // An entry for the initial load, pushState, iframe and the next page. 13 if (history.length == 3) { 14 // We're coming back from history. 15 function listener(m) { 16 if (m.message.includes("Scripts may only close windows that were opened by a script.")) { 17 SpecialPowers.postConsoleSentinel(); 18 SpecialPowers.pushPrefEnv({ set: [["dom.allow_scripts_to_close_windows", true]]}).then( 19 function() { 20 window.onunload = function() { 21 b.postMessage('blocked'); 22 b.close(); 23 }; 24 window.close(); 25 }); 26 } 27 } 28 SpecialPowers.registerConsoleListener(listener); 29 window.onunload = function() { 30 SpecialPowers.postConsoleSentinel(); 31 b.postMessage('closed'); 32 b.close(); 33 }; 34 window.close(); 35 } else { 36 // Load a page which will call history.back() 37 location.href = "file_window_close_2.html"; 38 } 39 } else { 40 onunload = function() { 41 b.postMessage('closed'); 42 b.close(); 43 }; 44 window.close(); 45 } 46 } 47 48 function init() { 49 if (isInitialLoad()) { 50 // Add some data to the session history. 51 history.pushState("foo", "foo"); 52 var ifr = document.getElementsByTagName("iframe")[0]; 53 ifr.onload = run; 54 ifr.src = "data:text/html,random data"; 55 } else { 56 run(); 57 } 58 } 59 window.onpageshow = () => { 60 setTimeout(init); 61 } 62 63 </script> 64 </head> 65 <body> 66 <iframe></iframe> 67 </body> 68 </html>