defold/extension-webview/webview
Functions and constants for interacting with webview APIs
- GitHub: defold/extension-webview — pinned to
1.5.1(released while Defold 1.13.1 was current)- Pick a release from defold/extension-webview releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as webview from "webview"
- Pick a release from defold/extension-webview releases and add its Source code (zip) URL (or a packaged
Functions
webview.create(callback: function): numberwebview.destroy(webview_id: number)webview.open(webview_id: number, url: string, options: table)webview.open_raw(webview_id: number, html: string, options: table)webview.eval(webview_id: number, code: string): numberwebview.set_transparent(webview_id: number, transparent: boolean)webview.set_visible(webview_id: number, visible: number)webview.is_visible(webview_id: number): numberwebview.set_position(webview_id: number, x: number, y: number, width: number, height: number)
webview.create(callback: function): number
Creates a webview instance. It can show HTML pages as well as evaluate Javascript. The view remains hidden until the first call. There can exist a maximum of 4 webviews at the same time.
On iOS, the callback will never get a webview.CALLBACK_RESULT_EVAL_ERROR, due to the iOS SDK implementation."
local function webview_callback(self, webview_id, request_id, type, data)
if type == webview.CALLBACK_RESULT_URL_OK then
-- the page is now loaded, let's show it
webview.set_visible(webview_id, 1)
elseif type == webview.CALLBACK_RESULT_URL_ERROR then
print("Failed to load url: " .. data["url"])
print("Error: " .. data["error"])
elseif type == webview.CALLBACK_RESULT_URL_LOADING then
-- a page is loading
-- return false to prevent it from loading
-- return true or nil to continue loading the page
if data.url ~= "https://www.defold.com/" then
return false
end
elseif type == webview.CALLBACK_RESULT_EVAL_OK then
print("Eval ok. Result: " .. data['result'])
elseif type == webview.CALLBACK_RESULT_EVAL_ERROR then
print("Eval not ok. Request # " .. request_id)
end
end
local webview_id = webview.create(webview_callback)Parameters
callback:function— A callback which receives info about finished requests taking the following parameters:self:object— The calling scriptwebview_id:number— The webview idrequest_id:number— The request idtype:constant— The type of callbackwebview.CALLBACK_RESULT_URL_OKwebview.CALLBACK_RESULT_URL_ERRORwebview.CALLBACK_RESULT_URL_LOADINGwebview.CALLBACK_RESULT_EVAL_OKwebview.CALLBACK_RESULT_EVAL_ERROR
data:table— A table holding the dataurl:string— The url used in thewebview.open()call.nilotherwise.result:string— Holds the result of either: a failed url open, a successful eval request or a failed eval.nilotherwise.
Returns
number— The id number of the webview
webview.destroy(webview_id: number)
Destroys an instance of a webview.
Parameters
webview_id:number— The webview id (returned by thewebview.create()call)
webview.open(webview_id: number, url: string, options: table)
Opens a web page in the webview, using an URL. Once the request is done, the callback (registered in webview.create()) is invoked.
local request_id = webview.open(webview_id, "http://www.defold.com", {hidden = true})Parameters
webview_id:number— The webview idurl:string— The URL to openoptions:table— A table of options for the request. Currently it holds these options:hidden:boolean— If true, the webview will stay hidden (default=false)headers:table— A table of header keys and valuestransparent:boolean— If true, the webview background will be transparent (default=false)
webview.open_raw(webview_id: number, html: string, options: table)
Opens a web page in the webview, using HTML data. Once the request is done, the callback (registered in webview.create()) is invoked.
local html = sys.load_resource("/main/data/test.html")
local request_id = webview.open_raw(webview_id, html, {hidden = true})Parameters
webview_id:number— The webview idhtml:string— The HTML data to displayoptions:table— A table of options for the request. Seewebview.open()
webview.eval(webview_id: number, code: string): number
Evaluates JavaScript within the context of the currently loaded page (if any). Once the request is done, the callback (registered in webview.create()) is invoked. The callback will get the result in the data["result"] field.
local request_id = webview.eval(webview_id, "GetMyFormData()")Parameters
webview_id:number— The webview idcode:string— The JavaScript code to evaluate
Returns
number— The id number of the request
webview.set_transparent(webview_id: number, transparent: boolean)
Set transparency of webview background
Parameters
webview_id:number— The webview idtransparent:boolean— Iftrue, the webview background becomes transparent, otherwise opaque.
webview.set_visible(webview_id: number, visible: number)
Shows or hides a webview
Parameters
webview_id:number— The webview idvisible:number— If0, hides the webview. If non zero, shows the view
webview.is_visible(webview_id: number): number
Returns the visibility state of the webview.
Parameters
webview_id:number— The webview id
Returns
number— Returns0if not visible,1if it is visible
webview.set_position(webview_id: number, x: number, y: number, width: number, height: number)
Sets the position and size of the webview
Parameters
webview_id:number— The webview idx:number— The x position of the webviewy:number— The y position of the webviewwidth:number— The width of the webview (-1 to match screen width)height:number— The height of the webview (-1 to match screen height)