WebView

class pyto_ui.WebView(url: str = None)

A View that displays web content.

class FrameInfo(wk_frame)

An object that contains information about a frame on a webpage.

property is_main_frame: bool

A boolean indicating whether the frame is the web site’s main frame or a subframe.

Return type:

bool

property security_origin: SecurityOrigin

The frame’s security origin.

Return type:

SecurityOrigin

property url: str

The frame’s current URL.

Return type:

str

exception JavaScriptException

An excpetion while running JavaScript code. Raised by evaluate_js().

class SecurityOrigin(host: str, port: int, protocol: str)

An object that identifies the origin of a particular resource.

host: str

The security origin’s host.

port: int

The security origin’s port.

protocol: str

The security origin’s protocol.

add_user_script(source: str, main_frame_only: bool = False)

Injects the specified script after a webpage has loaded.

Parameters:
  • source – The JavaScript source code.

  • main_frame_only – A boolean that indicates whether to inject the script into the main frame or all frames.

property can_go_back: bool

(Read Only) A boolean indicating whether go_back() can be performed.

Return type:

bool

property can_go_forward: bool

(Read Only) A boolean indicating whether go_forward() can be performed.

Return type:

bool

property did_fail_loading: callable

A function called when the Web View failed to load contents. Takes the sender Web View and a string describing the error as parameters.

Return type:

callable

property did_finish_loading: callable

A function called when the Web View finished loading contents. Takes the sender Web View as parameter.

Return type:

callable

property did_receive_message: callable

A function called when a script message is received from a webpage. Takes the sender Web View, the name of the message handler, the content of the message as parameters and the frame that posted the message.

The following example script shows how to send a message from a JavaScript page and how to receive it from the Web View.

import pyto_ui as ui

def did_receive_message(web_view, name, message, frame):
    print(name, message, frame.url)

web_view = ui.WebView()
web_view.did_receive_message = did_receive_message
web_view.register_message_handler("webView")

web_view.load_html('''

<h1> Hello World </h1>

<script>
    window.webkit.messageHandlers.webView.postMessage({foo:"bar"})
</script>

''')

ui.show_view(web_view, ui.PRESENTATION_MODE_SHEET)
Return type:

callable

property did_start_loading: callable

A function called when the Web View starts loading contents. Takes the sender Web View as parameter.

Return type:

callable

evaluate_js(code: str, frame: FrameInfo = None) → str

Runs JavaScript code and returns a String representation of the evaluation result. Raises a JavaScriptException.

Parameters:
  • code – JavaScript code to run.

  • frame – An optional frame to execute the code in. If None is passed, the code is executed in the main frame.

Return type:

str

go_back()

Goes back.

go_forward()

Goes forward.

property is_loading: bool

(Read Only) A boolean indicating whether the Web View is loading content.

Return type:

bool

load_file_path(path: str)

Loads a file.

Parameters:

path – The path of the file to load.

load_html(html: str, base_url: str = None)

Loads an HTML string.

Parameters:
  • html – The HTML code to load.

  • base_url – An optional URL used to resolve relative paths.

load_url(url: str)

Loads an URL.

Parameters:

url – The URL to laod. Can be ‘http://’, ‘https://’ or ‘file://’.

register_message_handler(name: str)

Adds a script message handler.

Adding a script message handler with name name causes the JavaScript function window.webkit.messageHandlers.name.postMessage(messageBody) to be defined in all frames in all web views that use the user content controller.

Parameters:

name – The name of the message handler.

reload()

Reloads the Web View.

remove_all_user_scripts()

Removes all injected user scripts.

stop()

Stops loading content.

property url: str

(Read Only) The current URL loaded into the Web View.

Return type:

str