GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing docs.wxpython.org — wx.html2.WebView — wxPython 4.3.2 Docs
|
Add a script message handler with the given name. |
|
Injects the specified script into the webpage’s content. |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Returns |
|
Retrieve whether a zoom type is supported. |
|
Returns |
|
Clears the browsing data of the web view. |
|
Clear the history, this will also remove the visible page. |
|
Clears the current selection. |
|
Copies the current selection. |
|
Creation function for two-step creation. |
|
Cuts the current selection. |
|
Deletes the current selection. |
|
Enable or disable access to dev tools for the user. |
|
Enable or disable if browser accelerator keys are enabled. |
|
Enable or disable the right click context menu. |
|
Enable or disable the history. |
|
Finds a phrase on the current page and if found, the control will scroll the phrase into view and select it. |
|
Returns a list of items in the back history. |
|
Get the title of the current web page, or its URL/path if title is not available. |
|
Get the URL of the currently displayed document. |
|
Returns a list of items in the forward history. |
|
Return the pointer to the native backend used by this control. |
|
Get the HTML source code of the currently displayed document. |
|
Get the text of the current page. |
|
Returns the currently selected source, if any. |
|
Returns the currently selected text, if any. |
|
Returns the current user agent string for the web view. |
|
Get the zoom level of the page. |
|
Get the zoom factor of the page. |
|
Get how the zoom factor is currently interpreted. |
|
Navigate back in the history of visited pages. |
|
Navigate forward in the history of visited pages. |
|
Returns |
|
Returns |
|
Allows to check if a specific backend is currently available. |
|
Returns whether the web control is currently busy (e.g. loading a page). |
|
Returns |
|
Returns whether the web control is currently editable. |
|
Load a web page from a URL. |
|
Sets emulation level. |
|
Factory function to create a new wx.html2.WebView with two-step creation, |
|
Pastes the current data. |
|
Opens a print dialog (with the backend’s default settings) so that the user may print the currently displayed page. |
|
Saves the currently displayed page as a |
|
Redos the last action. |
|
Allows the registering of new backend for wx.html2.WebView. |
|
Registers a custom scheme handler. |
|
Reload the currently displayed URL. |
|
Removes all user scripts from the web view. |
|
Remove a script message handler with the given name that was previously added via |
|
Runs the given JavaScript code. |
|
Runs the given JavaScript code asynchronously and returns the result via a |
|
Selects the entire page. |
|
Set the editable property of the web control. |
|
Set the displayed page source to the contents of the given string. |
|
Set the proxy to use for all requests. |
|
Specify a custom user agent string for the web view. |
|
Set the zoom level of the page. |
|
Set the zoom factor of the page. |
|
Set how to interpret the zoom factor. |
|
Show the dev tools window. |
|
Stop the current page loading process, if any. |
|
Undos the last action. |
Properties Summary¶See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
Class API¶This control may be used to render web (HTML / CSS / JavaScript) documents.
Add a script message handler with the given name.
To use the script message handler from javascript use window.<name>.postMessage(<messageBody>) where <name> corresponds the value of the name parameter. The <messageBody> will be available to the application via a wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED event.
Sample C++ code receiving a script message:
# Install message handler with the name wx_msg self.webView.AddScriptMessageHandler('wx_msg') # Bind an event handler to receive those messages self.webView.Bind(wx.EVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, self.handleMessage)
Sample JavaScript sending a script message:
# Send sample message body window.wx_msg.postMessage('This is a message body')
name (string) – Name of the message handler that can be used from JavaScript
bool
True if the handler could be added, False if it could not be added.
Added in version 4.1/wxWidgets-3.1.5.
Note
The Edge backend only supports a single message handler and the Chromium and IE backends do not support script message handlers.
See also
Injects the specified script into the webpage’s content.
javascript (string) – The JavaScript code to add.
injectionTime (WebViewUserScriptInjectionTime) – Specifies when the script will be executed.
bool
True if the script was added successfully.
Added in version 4.1/wxWidgets-3.1.5.
Note
Please note that this is unsupported by the IE backend and the Edge backend does only support wx.html2.WEBVIEW_INJECT_AT_DOCUMENT_START.
See also
Returns True if browser accelerator keys are enabled.
bool
Added in version 4.3/wxWidgets-3.3.0.
Returns True if the current selection can be copied.
bool
Returns True if the current selection can be cut.
bool
Returns True if it is possible to navigate backward in the history of visited pages.
bool
Returns True if it is possible to navigate forward in the history of visited pages.
bool
Returns True if data can be pasted.
bool
Returns True if there is an action to redo.
bool
Retrieve whether a zoom type is supported.
type (WebViewZoomType) – The zoom type to test.
bool
True if this type of zoom is supported by this backend (and thus can be set through SetZoomType ).
Returns True if there is an action to undo.
bool
Clears the browsing data of the web view.
This function clears the browsing data of the web view, such as cookies, cache, history, etc. The exact data that is cleared depends on the backend used.
This operation is asynchronous and may take some time to complete. When finished wxEVT_WEBVIEW_BROWSING_DATA_CLEARED event is generated.
types (int) – The types of browsing data to clear. By default, it clears all types of browsing data.
since (wx.DateTime) – The time since when the browsing data should be cleared. By default, it clears all browsing data.
bool
False if backend doesn’t support clearing browsing data or an error occurred. Otherwise, True is returned and the browsing data will be cleared asynchronously and the application will receive a wxEVT_WEBVIEW_BROWSING_DATA_CLEARED event when it is done (or if doing it fails later).
Added in version 4.3/wxWidgets-3.3.0.
Note
This is only implemented in the Edge, WebKit2GTK+ and macOS backends.
See also
Clear the history, this will also remove the visible page.
None
Note
This is not implemented on the WebKit2GTK+ backend and macOS.
Clears the current selection.
None
Copies the current selection.
None
Creation function for two-step creation.
Please note that the object creation may be asynchronous when using some backends (e.g., WebViewEdge or WebViewChromium ) and the object is not really created until wxEVT_WEBVIEW_CREATED event is received, so any non-trivial calls to its member functions should be delayed until then.
Cuts the current selection.
None
Deletes the current selection.
Note that for WEBVIEW_BACKEND_WEBKIT the selection must be editable, either through SetEditable or the correct HTML attribute.
None
Enable or disable access to dev tools for the user.
Dev tools are disabled by default.
enable (bool)
None
Added in version 4.1/wxWidgets-3.1.4.
Note
This is not implemented for the IE backend.
See also
Enable or disable if browser accelerator keys are enabled.
Browser accelerator keys are enabled by default.
The webview might implement various accelerator keys. This includes allowing accelerator key access to features such as printing and navigation. In many cases this might not be desired and the default keys can be disabled with this setting.
enable (bool)
None
Added in version 4.3/wxWidgets-3.3.0.
Note
This is only implemented for the Edge backend.
Enable or disable the right click context menu.
By default the standard context menu is enabled, this method can be used to disable it or re-enable it later.
enable (bool)
None
Added in version 2.9.5.
Enable or disable the history.
This will also clear the history.
enable (bool)
None
Note
This is not implemented on the WebKit2GTK+ backend and macOS.
Finds a phrase on the current page and if found, the control will scroll the phrase into view and select it.
text (string) – The phrase to search for.
flags (WebViewFindFlags) – The flags for the search.
int
If search phrase was not found in combination with the flags then NOT_FOUND is returned. If called for the first time with search phrase then the total number of results will be returned. Then for every time its called with the same search phrase it will return the number of the current match.
Added in version 2.9.5.
Note
This function will restart the search if the flags WEBVIEW_FIND_ENTIRE_WORD or WEBVIEW_FIND_MATCH_CASE are changed, since this will require a new search. To reset the search, for example resetting the highlights call the function with an empty search phrase.
Returns a list of items in the back history.
The first item in the vector is the first page that was loaded by the control.
Any
variant (WindowVariant)
VisualAttributes
Get the title of the current web page, or its URL/path if title is not available.
str
Get the URL of the currently displayed document.
str
Returns a list of items in the forward history.
The first item in the vector is the next item in the history with respect to the currently loaded page.
Any
Return the pointer to the native backend used by this control.
This method can be used to retrieve the pointer to the native web engine used by this control. The return value needs to be down-casted to the appropriate type depending on the backend:
Backend |
Platform |
Type |
|---|---|---|
|
MSW |
|
|
MSW |
|
|
|
|
|
macOS |
|
|
MSW, macOS, GTK |
For example, you could set the WebKit options using this method:
# In Python the value returned will be a sip wrapper around a void* type, # and it can be converted to the address being pointed to with int(). webview_ptr = self.webview.GetNativeBackend() # Assuming you are able to get a ctypes, cffi or similar access to the # webview library, you can use that pointer value to give it access to the # WebView backend to operate upon. theWebViewLib.doSomething(int(webview_ptr))
Any
Added in version 2.9.5.
See also
GetBackendVersionInfo
Get the HTML source code of the currently displayed document.
str
The HTML source code, or an empty string if no page is currently shown.
Get the text of the current page.
str
Returns the currently selected source, if any.
str
Returns the currently selected text, if any.
str
Returns the current user agent string for the web view.
str
Added in version 4.1/wxWidgets-3.1.5.
Get the zoom level of the page.
See GetZoomFactor to get more precise zoom scale value other than as provided by WebViewZoom .
The current level of zoom.
Get the zoom factor of the page.
float
The current factor of zoom.
Added in version 4.1/wxWidgets-3.1.4.
Get how the zoom factor is currently interpreted.
How the zoom factor is currently interpreted by the backend.
Navigate back in the history of visited pages.
Only valid if CanGoBack returns True.
None
Navigate forward in the history of visited pages.
Only valid if CanGoForward returns True.
None
Returns True if there is a current selection.
bool
Returns True if dev tools are available to the user.
bool
Added in version 4.1/wxWidgets-3.1.4.
Allows to check if a specific backend is currently available.
For example, to check for Edge backend availability:
if wx.html2.WebView.IsBackendAvailable(wx.html2.WebViewBackendEdge): # Do whatever you need to do when the Edge backend is available
backend (string)
bool
Added in version 4.1/wxWidgets-3.1.4.
Returns whether the web control is currently busy (e.g. loading a page).
bool
Returns True if a context menu will be shown on right click.
bool
Added in version 2.9.5.
Returns whether the web control is currently editable.
bool
Load a web page from a URL.
url (string) – The URL of the page to be loaded.
None
Note
Web engines generally report errors asynchronously, so if you wish to know whether loading the URL was successful, register to receive navigation error events.
Sets emulation level.
This function is useful to change the emulation level of the system browser control used for wx.html2.WebView implementation under MSW, rather than using the currently default, IE7-compatible, level.
Please notice that this function works by modifying the per-user part of MSW registry, which has several implications: first, it is sufficient to call it only once (per user) as the changes done by it are persistent and, second, if you do not want them to be persistent, you need to call it with WEBVIEWIE_EMU_DEFAULT argument explicitly.
In particular, this function should be called to allow RunScript to work for JavaScript code returning arbitrary objects, which is not supported at the default emulation level.
If set to a level higher than installed version, the highest available level will be used instead. WEBVIEWIE_EMU_IE11 is recommended for best performance and experience.
This function is MSW-specific and doesn’t exist under other platforms.
See https://msdn.microsoft.com/en-us/library/ee330730#browser_emulation for more information about browser control emulation levels.
level (WebViewIE_EmulationLevel) – the target emulation level
bool
True on success, False on failure (a warning message is also logged in the latter case).
Added in version 4.1/wxWidgets-3.1.3.
Please explicitly specify emulation level with MSWSetEmulationLevel .
modernLevel (bool) – True to set level to IE8, synonym for WEBVIEWIE_EMU_IE8 . False to reset the emulation level to its default, synonym for WEBVIEWIE_EMU_DEFAULT .
bool
True on success, False on failure (a warning message is also logged in the latter case).
Added in version 4.1/wxWidgets-3.1.1.
Deprecated
This function is kept mostly for backwards compatibility.
New (backend=WebViewBackendDefault)
Factory function to create a new wx.html2.WebView with two-step creation, wx.html2.WebView.Create should be called on the returned object.
backend (string) – The backend to use.
The created wx.html2.WebView.
Added in version 2.9.5.
New (config)
Factory function to create a new wx.html2.WebView with two-step creation, wx.html2.WebView.Create should be called on the returned object.
config (wx.html2.WebViewConfiguration) – a configuration object create with NewConfiguration .
The created wx.html2.WebView
Added in version 4.3/wxWidgets-3.3.0.
New (parent, id=ID_ANY, url=WebViewDefaultURLStr, pos=DefaultPosition, size=DefaultSize, backend=WebViewBackendDefault, style=0, name=WebViewNameStr)
Factory function to create a new wx.html2.WebView using a wx.html2.WebViewFactory.
Note that the returned object may not be immediately usable yet, see Create and wxEVT_WEBVIEW_CREATED.
parent (wx.Window) – Parent window for the control
id (wx.WindowID) – ID of this control
url (string) – Initial URL to load
pos (wx.Point) – Position of the control
size (wx.Size) – Size of the control
backend (string) – The backend web engine to use. WebViewBackendDefault and other backend names constants in format WebViewBackend<Name> (e.g., WebViewBackendWebKit ) are predefined where appropriate. See Backend descriptions.
style (long) – Window style. For generic window styles, please see wx.Window.
name (string) – Window name.
The created wx.html2.WebView, or nullptr if the requested backend is not available
Added in version 2.9.5.
Pastes the current data.
None
Print (self)
Opens a print dialog (with the backend’s default settings) so that the user may print the currently displayed page.
None
Print (self, printData, flags=WEBVIEW_PRINT_HIDE_HEADER_FOOTER)
Prints the currently displayed page using the given print settings.
The printData parameter allows specifying paper size, orientation, number of copies, duplex mode and colour/greyscale output.
The flags parameter is a combination of WebViewPrintFlags values. By default, headers and footers are shown (where supported by the backend). Pass WEBVIEW_PRINT_HIDE_HEADER_FOOTER to suppress them.
This overload is only available when USE_PRINTING_ARCHITECTURE is set to 1. Backends that do not support programmatic print settings will fall back to the parameterless Print .
Currently the Edge backend (MSW) has full support for all wx.PrintData settings including header/footer control. The GTK and macOS backends support paper size, orientation, and copies but ignore flags. The IE and Chromium backends fall back to Print .
printData (wx.PrintData) – The print settings to use.
flags (int) – A combination of WebViewPrintFlags values.
None
Added in version 4.3/wxWidgets-3.3.3.
PrintToPDF (self, filePath)
Saves the currently displayed page as a PDF file to filePath.
This operation is asynchronous. When it completes, a wxEVT_WEBVIEW_PDF_SAVED event is generated. Use wx.html2.WebViewEvent.GetURL to retrieve the output file path and wx.html2.WebViewEvent.IsError to check whether the operation succeeded.
filePath (string)
bool
True if the export was started successfully, False if the backend does not support PDF export or if an error prevented the operation from starting.
Added in version 4.3/wxWidgets-3.3.3.
Note
Currently implemented on the Edge (MSW), WebKit (macOS 11.0+), and WebKit2GTK (GTK) backends. Returns False on all other backends.
See also
PrintToPDF (self, filePath, printData)
Saves the currently displayed page as a PDF file using the given print settings.
This overload allows specifying paper size and orientation via printData. Backend support for these settings varies:
Edge (MSW): paper size (derived from the wx.PrintData paper ID) and orientation are both applied to the output PDF.
macOS: printData is not used; this behaves identically to PrintToPDF .
GTK (WebKit2GTK): paper size, orientation, copies, and collation are all applied to the output PDF.
This overload is only available when USE_PRINTING_ARCHITECTURE is set to 1.
filePath (string)
printData (wx.PrintData)
bool
True if the export was started successfully, False if the backend does not support PDF export.
Added in version 4.3/wxWidgets-3.3.3.
See also
Redos the last action.
None
Allows the registering of new backend for wx.html2.WebView.
backend can be used as an argument to New .
backend (string) – The name for the new backend to be registered under
factory (wx.html2.WebViewFactory) – A shared pointer to the factory which creates the appropriate backend.
None
Added in version 2.9.5.
Registers a custom scheme handler.
handler (wx.html2.WebViewHandler) – A shared pointer to a WebHandler.
None
Note
On macOS in order to use handlers two-step creation has to be used and RegisterHandler has to be called before Create . With the other backends it has to be called after Create .
Note
The Edge backend does not support custom schemes, but the handler is available as a virtual host under https://scheme.wxsite. To customize this virtual host call wx.html2.WebViewHandler.SetVirtualHost before registering the handler.
Reload the currently displayed URL.
flags (WebViewReloadFlags) – A bit array that may optionally contain reload options.
None
Note
The flags are ignored by the Edge backend.
Removes all user scripts from the web view.
None
Added in version 4.1/wxWidgets-3.1.5.
See also
Remove a script message handler with the given name that was previously added via AddScriptMessageHandler .
name (string)
bool
True if the handler could be removed, False if it could not be removed.
Added in version 4.1/wxWidgets-3.1.5.
See also
Runs the given JavaScript code.
JavaScript code is executed inside the browser control and has full access to DOM and other browser-provided functionality. For example, this code
webview.RunScript("document.write('Hello from wx.Widgets!')")
will replace the current page contents with the provided string.
If output is non-null, it is filled with the result of executing this code on success, e.g. a JavaScript value such as a string, a number (integer or floating point), a boolean or JSON representation for non-primitive types such as arrays and objects. For example:
success, result = webview.RunScript( "document.getElementById('some_id').innderHTML") if success: ... result contains the contents of the given element ... else: ... the element with self ID probably doesn't exist ...
This function has a few platform-specific limitations:
When using WebKit v1 in wxGTK2, retrieving the result of JavaScript execution is unsupported and this function will always return False if output is non-null to indicate this. This functionality is fully supported when using WebKit v2 or later in GTK3.
When using WebKit under macOS, code execution is limited to at most 10MiB of memory and 10 seconds of execution time.
When using Chromium backend, retrieving the result of JavaScript execution is unsupported and this function will always return False if output is non-null to indicate this.
When using IE backend under MSW, scripts can only be executed when the current page is fully loaded (i.e. wxEVT_WEBVIEW_LOADED event was received). A script tag inside the page HTML is required in order to run JavaScript.
Also notice that with the IE backend converting JavaScript objects to JSON is not supported in the default emulation mode, it implements its own object-to-JSON conversion as a fallback for this case, however it is not as full-featured, well-tested or performing as the implementation of this functionality in the browser control itself, so it is recommended to use MSWSetEmulationLevel() to change emulation level to a more modern one in which JSON conversion is done by the control itself.
javascript (string) – JavaScript code to execute.
Tuple[bool, str]
True if there is a result, False if there is an error.
Note
Because of various potential issues it’s recommended to use RunScriptAsync instead of this method. This is especially True if you plan to run code from a webview event and will also prevent unintended side effects on the UI outside of the webview.
See also
Runs the given JavaScript code asynchronously and returns the result via a wxEVT_WEBVIEW_SCRIPT_RESULT .
The script result value can be retrieved via wx.html2.WebViewEvent.GetString . If the execution fails wx.html2.WebViewEvent.IsError will return True. In this case additional script execution error information maybe available via wx.html2.WebViewEvent.GetString .
javascript (string) – JavaScript code to execute.
clientData – Arbirary pointer to data that can be retrieved from the result event.
None
Added in version 4.1/wxWidgets-3.1.6.
Note
The IE backend does not support async script execution.
See also
Selects the entire page.
None
Set the editable property of the web control.
Enabling allows the user to edit the page even if the contenteditable attribute is not set. The exact capabilities vary with the backend being used.
enable (bool)
None
Note
This is not implemented for macOS and the Edge backend.
SetPage (self, html, baseUrl)
Set the displayed page source to the contents of the given string.
html (string) – The string that contains the HTML data to display.
baseUrl (string) – URL assigned to the HTML data, to be used to resolve relative paths, for instance.
None
Note
When using WEBVIEW_BACKEND_IE you must wait for the current page to finish loading before calling SetPage . The baseURL parameter is not used in this backend and the Edge backend.
SetPage (self, html, baseUrl)
Set the displayed page source to the contents of the given stream.
html (wx.InputStream) – The stream to read HTML data from.
baseUrl (string) – URL assigned to the HTML data, to be used to resolve relative paths, for instance.
None
Set the proxy to use for all requests.
The proxy string must be a valid proxy specification, e.g. http://my.local.proxy.corp:8080
Currently this function is only implemented in WebKit2, Edge and Chromium backends and only WebKit2 backend allows to set the proxy after creating the webview, so it is recommended to call it before Create :
proxy (string)
bool
True if proxy was set successfully or False if it failed, e.g. because this is not supported by the currently used backend.
Specify a custom user agent string for the web view.
Returns True the user agent could be set.
If your first request should already use the custom user agent please use two step creation and call SetUserAgent before Create .
userAgent (string)
bool
Added in version 4.1/wxWidgets-3.1.5.
Note
This is not implemented for IE.
Set the zoom level of the page.
See SetZoomFactor for more precise scaling other than the measured steps provided by WebViewZoom .
zoom (WebViewZoom) – How much to zoom (scale) the HTML document.
None
Set the zoom factor of the page.
zoom (float) – How much to zoom (scale) the HTML document in arbitrary number.
None
Added in version 4.1/wxWidgets-3.1.4.
Note
zoom scale in IE will be converted into WebViewZoom levels for WebViewZoomType of WEBVIEW_ZOOM_TYPE_TEXT .
Set how to interpret the zoom factor.
zoomType (WebViewZoomType) – How the zoom factor should be interpreted by the backend.
None
Note
invoke CanSetZoomType first, some backends may not support all zoom types.
Show the dev tools window.
Immediately open the dev tools window. Does nothing if this window was already open.
Unlike EnableAccessToDevTools , which allows the user to open the dev tools window from the context menu, this function opens the same window programmatically.
Please note that while ShowDevTools can be used whether access to the dev tools from the context menu was enabled or not, in WebKit2GTK+ backend calling it also enables interactive access to the dev tools from the context menu as a side effect.
bool
True if the dev tools window is shown (even if it had been already shown before this function was called), False if it could not be shown for some reason, most likely because the backend does not provide this functionality.
Added in version 4.3/wxWidgets-3.3.0.
Note
This function is not implemented in the IE and WebKit (GTK/macOS) backends (but is implemented for WebKit2 GTK backend).
Stop the current page loading process, if any.
May trigger an error event of type WEBVIEW_NAV_ERR_USER_CANCELLED . TODO: make WEBVIEW_NAV_ERR_USER_CANCELLED errors uniform across ports.
None
Undos the last action.
None
See GetCurrentTitle
See GetCurrentURL
See GetNativeBackend
See GetPageSource
See GetPageText
See GetSelectedText
See GetUserAgent and SetUserAgent
See GetZoomFactor and SetZoomFactor
See GetZoomType and SetZoomType