browser.js (3485B)
1 /* import-globals-from common.js */ 2 3 var { AppConstants } = ChromeUtils.importESModule( 4 "resource://gre/modules/AppConstants.sys.mjs" 5 ); 6 7 /** 8 * Load the browser with the given url and then invokes the given function. 9 */ 10 function openBrowserWindow(aFunc, aURL, aRect) { 11 gBrowserContext.testFunc = aFunc; 12 gBrowserContext.startURL = aURL; 13 gBrowserContext.browserRect = aRect; 14 15 addLoadEvent(openBrowserWindowIntl); 16 } 17 18 /** 19 * Close the browser window. 20 */ 21 function closeBrowserWindow() { 22 gBrowserContext.browserWnd.close(); 23 } 24 25 /** 26 * Return the browser window object. 27 */ 28 function browserWindow() { 29 return gBrowserContext.browserWnd; 30 } 31 32 /** 33 * Return the document of the browser window. 34 */ 35 function browserDocument() { 36 return browserWindow().document; 37 } 38 39 /** 40 * Return tab browser object. 41 */ 42 function tabBrowser() { 43 return browserWindow().gBrowser; 44 } 45 46 /** 47 * Return browser element of the current tab. 48 */ 49 function currentBrowser() { 50 return tabBrowser().selectedBrowser; 51 } 52 53 /** 54 * Return DOM document of the current tab. 55 */ 56 function currentTabDocument() { 57 return currentBrowser().contentDocument; 58 } 59 60 /** 61 * Return window of the current tab. 62 */ 63 function currentTabWindow() { 64 return currentTabDocument().defaultView; 65 } 66 67 /** 68 * Return browser element of the tab at the given index. 69 */ 70 function browserAt(aIndex) { 71 return tabBrowser().getBrowserAtIndex(aIndex); 72 } 73 74 /** 75 * Return DOM document of the tab at the given index. 76 */ 77 function tabDocumentAt(aIndex) { 78 return browserAt(aIndex).contentDocument; 79 } 80 81 /** 82 * Return input element of address bar. 83 */ 84 function urlbarInput() { 85 return browserWindow().document.getElementById("urlbar").inputField; 86 } 87 88 /** 89 * Return reload button. 90 */ 91 function reloadButton() { 92 return browserWindow().document.getElementById("urlbar-reload-button"); 93 } 94 95 // ////////////////////////////////////////////////////////////////////////////// 96 // private section 97 98 var gBrowserContext = { 99 browserWnd: null, 100 testFunc: null, 101 startURL: "", 102 }; 103 104 function openBrowserWindowIntl() { 105 var params = "chrome,all,dialog=no,non-remote"; 106 var rect = gBrowserContext.browserRect; 107 if (rect) { 108 if ("left" in rect) { 109 params += ",left=" + rect.left; 110 } 111 if ("top" in rect) { 112 params += ",top=" + rect.top; 113 } 114 if ("width" in rect) { 115 params += ",width=" + rect.width; 116 } 117 if ("height" in rect) { 118 params += ",height=" + rect.height; 119 } 120 } 121 122 gBrowserContext.browserWnd = 123 window.browsingContext.topChromeWindow.openDialog( 124 AppConstants.BROWSER_CHROME_URL, 125 "_blank", 126 params, 127 gBrowserContext.startURL || "data:text/html,<html></html>" 128 ); 129 130 whenDelayedStartupFinished(browserWindow(), function () { 131 addA11yLoadEvent(startBrowserTests, browserWindow()); 132 }); 133 } 134 135 function startBrowserTests() { 136 if (gBrowserContext.startURL) { 137 // Make sure the window is the one loading our URL. 138 if (currentBrowser().contentWindow.location != gBrowserContext.startURL) { 139 setTimeout(startBrowserTests, 0); 140 return; 141 } 142 // wait for load 143 addA11yLoadEvent(gBrowserContext.testFunc, currentBrowser().contentWindow); 144 } else { 145 gBrowserContext.testFunc(); 146 } 147 } 148 149 function whenDelayedStartupFinished(aWindow, aCallback) { 150 Services.obs.addObserver(function observer(aSubject, aTopic) { 151 if (aWindow == aSubject) { 152 Services.obs.removeObserver(observer, aTopic); 153 setTimeout(aCallback, 0); 154 } 155 }, "browser-delayed-startup-finished"); 156 }