WebBrowser Reference
Reference for the web browser control (CTRL_WB / CTRL_SW) embedded in the main window of Tablacus Explorer and in its sub-windows (the options screen and dialogs).
- Related: te Reference (
te.Ctrl/te.CreateCtrl(CTRL_SW, ...)/te.Arguments), api Reference, FolderView (ShellBrowser) Reference - Source: the implementation of
methodWB[]andCteWebBrowser(constructor,Invoke,Close) inTE/TE.cpp, and of the sub-window window procedureWndProc2
Notation: P = property, M = method. Member names are matched case-insensitively.
Table of Contents
- Overview
- How to Obtain
- Methods
- Properties
- Event (OnClose) and Close Handling
- Differences Between the Main Window and Sub-Windows
- Constants
- Examples
1. Overview
The WebBrowser object represents a window that displays an HTML page. Internally, one of the following is used.
| Kind | Description |
|---|---|
| WebView2 (Chromium based) | Used with priority. Used when the WebView2 component prepared by TE could be loaded (the internal flag g_nBlink is 1). Edge's Chakra is also selected as the script engine |
| IE's WebBrowser control (MSHTML) | The fallback when the above could not be used (g_nBlink is 2) |
In either case, you can access the DOM and the window object of the page being displayed through the Document and Window of this object.
From a loaded page, you can call TE's features through external (= the te object).
2. How to Obtain
| Way | Description |
|---|---|
te.Ctrl(CTRL_WB) |
Returns the WebBrowser of the window in which the calling script is running. From a script in the main window it is the main WebBrowser, and from a page in a sub-window it is the WebBrowser of that window |
te.Ctrls(CTRL_WB) |
All existing WebBrowsers |
te.CreateCtrl(CTRL_SW, url, arguments, parent, width, height [, x, y]) |
Creates a new sub-window and returns its WebBrowser (te Reference) |
The Ctrl argument of events |
In OnBeforeNavigate and similar events, a WebBrowser if Ctrl.Type == CTRL_WB (used to detect a drop onto the address bar and the like) |
In asynchronous scripts (the WebView2 side, such as ui.js), await is needed, as in await te.Ctrl(CTRL_WB).
3. Methods
| Name | Return value | Description |
|---|---|---|
Close() |
none | Closes the window. It makes the window transparent immediately and then sends WM_CLOSE (OnClose is not called) |
PreventClose() |
none | When called inside OnClose, it cancels the close operation (Chapter 5) |
Focus() |
none | Moves focus. With WebView2 it reactivates IOleInPlaceObject, and with the IE version it brings the parent window to the front |
TranslateAccelerator(hwnd, msg, wParam, lParam) |
HRESULT | Passes window messages such as key input to the accelerator handling of the internal browser control. Requires 4 arguments. If arguments are missing, it returns E_NOTIMPL |
4. Properties
| Name | Kind | Description |
|---|---|---|
Type |
P (read-only) | CTRL_WB for the WebBrowser of the main window, and CTRL_SW for a sub-window |
Data |
P | A value the script can hold freely |
hwnd |
P (read-only) | The window handle of the browser control |
Document |
P | The document of the page being displayed (IWebBrowser2.Document). With WebView2, assigning a value replaces the internal document property |
Window |
P (read-only) | The window (HTML window) object of the page being displayed. Empty if it cannot be obtained |
Application |
P (read-only) | The internal IWebBrowser2.Application |
Visible |
P | Show / hide the browser control |
DropMode |
P (write-only) | Whether the browser control accepts dropped files (RegisterAsDropTarget). Effective only with WebView2; the IE version does nothing. No value is returned when read |
OnClose |
P (write-only) | The function called when the window is being closed. Its argument is the WebBrowser itself (Chapter 5) |
5. Event (OnClose) and Close Handling
If you set WebBrowser.OnClose = function (WB) { ... }, then when the user tries to close the window with the window's Close button (×) or the like, the window is not closed immediately; instead, this function is called. Inside the function, do one of the following.
| Call | Behavior |
|---|---|
WB.Close() |
Continues the close processing (the window moves to a blank page and is then destroyed) |
WB.PreventClose() |
Cancels the close operation (the window stays open) |
- If a close operation occurs in a sub-window where
OnCloseis not set, it is simply closed. OnCloseis called only in the window (sub-window) where the close request was issued. If the window is closed from a script withWB.Close(),OnCloseis not called.- An asynchronous function that can use
awaitcan also be passed asOnClose(options.jsusesasync function (WB)). However, unless you callClose()/PreventClose()once the work is finished (once the asynchronous confirmation is finished), the window remains in a state where it cannot be closed. - After the close processing, it is a two-stage process: the window moves to a blank page on
DocumentCompleteand then is destroyed. - Actual use:
InitOptionsinoptions.jsshows a "Do you want to save?" confirmation when there are changes, callsWebBrowser.PreventClose()inContinueOptionsto cancel closing, and closes withCloseWB(WebBrowser, true). Dialog-type windows setReturnDialogResult(which returns the result and callsWB.Close()) asOnClose.
6. Differences Between the Main Window and Sub-Windows
| Item | Main (CTRL_WB) |
Sub-window (CTRL_SW) |
|---|---|---|
| How to get it | te.Ctrl(CTRL_WB) (from the main script) |
te.CreateCtrl(CTRL_SW, …) / te.Ctrl(CTRL_WB) inside the sub-window |
OnClose |
Not called (the close processing is on the te.OnClose side) |
Called |
| Navigating to a file or path | If the URL is a file system path, the ::{…} form, or a URL that contains / other than file:// and data:, it is opened in a TE window/tab instead of the browser |
No such navigation cancel is performed |
New windows (such as window.open) |
Replaced by opening on the TE side (it is not opened in the browser) | Same as the left |
| Arguments | — | The opened page can get the arguments of te.CreateCtrl(CTRL_SW, url, arguments, …) as te.Arguments, only once |
7. Constants
| Constant | Value | Description |
|---|---|---|
CTRL_WB |
0x20000 |
The WebBrowser of the main window |
CTRL_SW |
0x20001 |
The WebBrowser of a sub-window |
8. Examples
// 1. Open a sub-window (close to the options screen example)
const WB = te.CreateCtrl(CTRL_SW, "file:///C:/path/page.html", { name: "value" }, te.hwnd, 600, 400); // returns a WebBrowser
WB.OnClose = function (WB) {
// close without confirmation
WB.Close();
};
// 2. Confirm before closing, and cancel
WB.OnClose = function (WB) {
// IDNO = 7 (the return value of api.MessageBox)
if (api.MessageBox(te.hwnd, "Close this window?", TITLE, MB_YESNO) == IDNO) {
WB.PreventClose(); // cancel
return;
}
WB.Close();
};
// 3. Read the DOM of the page
const doc = WB.Document;
const title = doc.title;
const win = WB.Window;
win.alert("hello");
// 4. On the opened page side (inside the sub-window)
// te.Arguments receives this value only once, and then clears it
const args = te.Arguments;
const WebBrowser = te.Ctrl(CTRL_WB); // its own WebBrowser
WebBrowser.Close(); // close itself
// 5. The main WebBrowser (detecting the drop target)
te.OnDrop = function (Ctrl, dataObj, pgrfKeyState, pt, pdwEffect) {
if (Ctrl.Type == CTRL_WB) {
// dropped onto the address bar or the like
}
};
For how to specify the arguments of
te.CreateCtrl(CTRL_SW, ...)above, see theCreateCtrlentry in the te Reference. The arguments ofOnDropfollow the order in thesync1.jsimplementation (Ctrl, dataObj, pgrfKeyState, pt, pdwEffect).TITLE,MB_YESNO, andIDNOare assumed to be the ones defined inconsts.jsand elsewhere (needs verification).