Skip to main content

bn.createWebview

createWebview(options): Webview

remark supported jssdk > 3.13.0

description Create a webview displayed above mini game. Only one webview is supported at the same time

example

const webview = bn.createWebview({
// required, resource url, support local file path
src: '/root/src/index.html',
// position
x: 0,
y: 0,
// size
height: 500,
width: 500,
// background color in #rrggbbaa format
backgroundColor: '#FFFFFFFF'
success (res) {}
}
})
// update webview properties
webview.update({
x: 100,
y: 100,
})
// close webview
webview.close()

// send message to webview
webview.postMessage('test')

// listen event on webview
webview.onLoad(() => {})

Parameters

NameTypeDescription
optionsCreateWebviewOptionscreate webview options

Returns

Webview

Webview - the webview instance

CreateWebviewOptions

Ƭ CreateWebviewOptions: Object

Type declaration

NameTypeDescription
srcstringSrc of webview page supports http:// https:// or local file path
height?numberHeight of webview component
width?numberWidth of webview component
x?numberHorizontal position of webview
y?numberVertical position of webview
backgroundColor?string8 digit hex value #ffffffff
injected?string[]Inject specific javascript script file before page loaded
bounce?booleanwhether the page bounces when scroll beyond top/bottom
loadingBar?booleanwhether the page shows loading bar when loading page
webPreferences?{ [key: string]: unknown; userAgent?: string }Setting web preferences, currently only userAgent is supported
webPreferences.userAgent?string-
pageId?numberPage id, used to identify the page to show webview. Required in widget. example ts const pageId = getCurrentInstance().page.getPageId(); createWebview({ ...options, pageId, })
success?CreateWebviewSuccessCallback-
fail?CreateWebviewCallback-
complete?CreateWebviewCallback-

CreateWebviewSuccessCallback

Ƭ CreateWebviewSuccessCallback: ({ errMsg, webview, }: { errMsg: string ; webview: IWebview }) => void

Type declaration

▸ ({ errMsg, webview, }): void

Parameters

NameType
`{
errMsg,
webview,
}`Object
`{
errMsg,
webview,
}.errMsg`string
`{
errMsg,
webview,
}.webview`IWebview

Returns

void


Interface: IWebview

NameTypeDescription
srcstring
xnumber
ynumber
backgroundColorstring
update(options): void
close(): void
postMessage(message): void
canGoBack(): Promise<boolean>
canGoForward(): Promise<boolean>
url(): Promise<string>
title(): Promise<string>
goBack(): Promise<void>
goForward(): Promise<void>
refresh(): Promise<void>
clearCache(): Promise<void>
onMessage(handler): void
offMessage(handler): void
onLoad(handler): void
offLoad(handler): void
onError(handler): void
offError(handler): void

update

update(options): void

Parameters

NameType
optionsOptional<CreateWebviewOptions>

Returns

void


close

close(): void

Returns

void


postMessage

postMessage(message): void

Parameters

NameType
messagestring

Returns

void


canGoBack

canGoBack(): Promise<boolean>

Returns

Promise<boolean>


canGoForward

canGoForward(): Promise<boolean>

Returns

Promise<boolean>


url

url(): Promise<string>

Returns

Promise<string>


title

title(): Promise<string>

Returns

Promise<string>


goBack

goBack(): Promise<void>

Returns

Promise<void>


goForward

goForward(): Promise<void>

Returns

Promise<void>


refresh

refresh(): Promise<void>

Returns

Promise<void>


clearCache

clearCache(): Promise<void>

Returns

Promise<void>


onMessage

onMessage(handler): void

Parameters

NameType
handlerHandler<MessageEvent>

Returns

void


offMessage

offMessage(handler): void

Parameters

NameType
handlerHandler<MessageEvent>

Returns

void


onLoad

onLoad(handler): void

Parameters

NameType
handlerHandler<LoadEvent>

Returns

void


offLoad

offLoad(handler): void

Parameters

NameType
handlerHandler<LoadEvent>

Returns

void


onError

onError(handler): void

Parameters

NameType
handlerHandler<ErrorEvent>

Returns

void


offError

offError(handler): void

Parameters

NameType
handlerHandler<ErrorEvent>

Returns

void

Optional

Ƭ Optional<T>: Object

Type parameters

Name
T

Handler

Ƭ Handler<T>: (ev: T) => void

Type parameters

Name
T

Type declaration

▸ (ev): void

Parameters

NameType
evT

Returns

void


Interface: MessageEvent

NameTypeDescription
dataany

Interface: LoadEvent

NameTypeDescription
srcstring

Interface: ErrorEvent

NameTypeDescription
srcstring
error?string

CreateWebviewCallback

Ƭ CreateWebviewCallback: ({ errMsg }: { errMsg: string }) => void

Type declaration

▸ ({ errMsg }): void

Parameters

NameType
{ errMsg }Object
{ errMsg }.errMsgstring

Returns

void


Class: Webview

NameTypeDescription
_xnumber
_ynumber
_backgroundColorstring
_srcstring
idnumber
rpcNativeNativeRPC
rpcWebviewAppRPC
rendererIdnumber
taskTaskState<TaskContext>
managerKVManager<string, TaskState<TaskContext>>
xnumberHorizontal position of webview• set x(value): voidHorizontal position of webview
ynumberVertical position of webview• set y(value): voidVertical position of webview
backgroundColorstring8 digit hex value #ffffffff• set backgroundColor(value): void8 digit hex value #ffffffff
srcstringSrc of webview page supports http:// https:// or local file path• set src(value): voidSrc of webview page supports http:// https:// or local file path
close(): Promise<void>Close webview
update(options): Promise<{}>Update properties of webview
postMessage(message): Promise<void>Send message to webview page.
canGoBack(): Promise<boolean>Check if can go back from this page
canGoForward(): Promise<boolean>Check if can go forward from this page
url(): Promise<string>Get current url
title(): Promise<string>Get current title
goBack(): Promise<void>Go back to previous page
goForward(): Promise<void>Go forward to next page
refresh(): Promise<void>Refresh current page
clearCache(): Promise<void>Clear webview cache
snapshot(): Promise<string>
onMessage(handler): voidListen message event. Triggered when webview page send message to webview instance.
offMessage(handler): voidUn-listen message event.
onLoad(handler): voidListen load event. Triggered when webview completes loading.
offLoad(handler): voidUn-listen onload event.
onError(handler): voidListen error event. Triggered when error occurs
offError(handler): voidUn-listen error event.
onUrlChange(handler): voidListen url change event. Triggered when url changes
offUrlChange(handler): voidUn-listen url change event.
onTitleChange(handler): voidListen title change event. Triggered when title changes
offTitleChange(handler): voidUn-listen title change event.
onScrollChange(handler): voidListen scroll change event. Triggered when title changes
offScrollChange(handler): voidUn-listen scroll change event.

close

close(): Promise<void>

Close webview

Returns

Promise<void>

Implementation of

IWebview.close


update

update(options): Promise<{}>

Update properties of webview

Parameters

NameType
optionsOptional<CreateWebviewOptions>

Returns

Promise<{}>

Implementation of

IWebview.update


postMessage

postMessage(message): Promise<void>

Send message to webview page.

Parameters

NameType
messagestring

Returns

Promise<void>

Implementation of

IWebview.postMessage


canGoBack

canGoBack(): Promise<boolean>

Check if can go back from this page

Returns

Promise<boolean>

Implementation of

IWebview.canGoBack


canGoForward

canGoForward(): Promise<boolean>

Check if can go forward from this page

Returns

Promise<boolean>

Implementation of

IWebview.canGoForward


url

url(): Promise<string>

Get current url

Returns

Promise<string>

Implementation of

IWebview.url


title

title(): Promise<string>

Get current title

Returns

Promise<string>

Implementation of

IWebview.title


goBack

goBack(): Promise<void>

Go back to previous page

Returns

Promise<void>

Implementation of

IWebview.goBack


goForward

goForward(): Promise<void>

Go forward to next page

Returns

Promise<void>

Implementation of

IWebview.goForward


refresh

refresh(): Promise<void>

Refresh current page

Returns

Promise<void>

Implementation of

IWebview.refresh


clearCache

clearCache(): Promise<void>

Clear webview cache

Returns

Promise<void>

Implementation of

IWebview.clearCache


snapshot

snapshot(): Promise<string>

Returns

Promise<string>


onMessage

onMessage(handler): void

Listen message event. Triggered when webview page send message to webview instance.

Parameters

NameType
handler(ev: MessageEvent) => void

Returns

void

Implementation of

IWebview.onMessage


offMessage

offMessage(handler): void

Un-listen message event.

Parameters

NameType
handler(ev: MessageEvent) => void

Returns

void

Implementation of

IWebview.offMessage


onLoad

onLoad(handler): void

Listen load event. Triggered when webview completes loading.

Parameters

NameType
handler(ev: LoadEvent) => void

Returns

void

Implementation of

IWebview.onLoad


offLoad

offLoad(handler): void

Un-listen onload event.

Parameters

NameType
handler(ev: LoadEvent) => void

Returns

void

Implementation of

IWebview.offLoad


onError

onError(handler): void

Listen error event. Triggered when error occurs

Parameters

NameType
handler(ev: ErrorEvent) => void

Returns

void

Implementation of

IWebview.onError


offError

offError(handler): void

Un-listen error event.

Parameters

NameType
handler(ev: ErrorEvent) => void

Returns

void

Implementation of

IWebview.offError


onUrlChange

onUrlChange(handler): void

Listen url change event. Triggered when url changes

Parameters

NameType
handler(payload?: WebviewUrlChangeEvent) => void

Returns

void


offUrlChange

offUrlChange(handler): void

Un-listen url change event.

Parameters

NameType
handler(payload?: WebviewUrlChangeEvent) => void

Returns

void


onTitleChange

onTitleChange(handler): void

Listen title change event. Triggered when title changes

Parameters

NameType
handler(payload?: WebviewTitleChangeEvent) => void

Returns

void


offTitleChange

offTitleChange(handler): void

Un-listen title change event.

Parameters

NameType
handler(payload?: WebviewTitleChangeEvent) => void

Returns

void


onScrollChange

onScrollChange(handler): void

Listen scroll change event. Triggered when title changes

Parameters

NameType
handler(payload?: WebviewScrollChangeEvent) => void

Returns

void


offScrollChange

offScrollChange(handler): void

Un-listen scroll change event.

Parameters

NameType
handler(payload?: WebviewScrollChangeEvent) => void

Returns

void

close

close(): void

Returns

void


update

update(options): void

Parameters

NameType
optionsOptional<CreateWebviewOptions>

Returns

void


postMessage

postMessage(message): void

Parameters

NameType
messagestring

Returns

void


canGoBack

canGoBack(): Promise<boolean>

Returns

Promise<boolean>


canGoForward

canGoForward(): Promise<boolean>

Returns

Promise<boolean>


url

url(): Promise<string>

Returns

Promise<string>


title

title(): Promise<string>

Returns

Promise<string>


goBack

goBack(): Promise<void>

Returns

Promise<void>


goForward

goForward(): Promise<void>

Returns

Promise<void>


refresh

refresh(): Promise<void>

Returns

Promise<void>


clearCache

clearCache(): Promise<void>

Returns

Promise<void>


Interface: MessageEvent

NameTypeDescription
dataany

onMessage

onMessage(handler): void

Parameters

NameType
handlerHandler<MessageEvent>

Returns

void


offMessage

offMessage(handler): void

Parameters

NameType
handlerHandler<MessageEvent>

Returns

void


Interface: LoadEvent

NameTypeDescription
srcstring

onLoad

onLoad(handler): void

Parameters

NameType
handlerHandler<LoadEvent>

Returns

void


offLoad

offLoad(handler): void

Parameters

NameType
handlerHandler<LoadEvent>

Returns

void


Interface: ErrorEvent

NameTypeDescription
srcstring
error?string

onError

onError(handler): void

Parameters

NameType
handlerHandler<ErrorEvent>

Returns

void


offError

offError(handler): void

Parameters

NameType
handlerHandler<ErrorEvent>

Returns

void

Interface: WebviewUrlChangeEvent

NameTypeDescription
urlstring
canGoBackboolean
canGoForwardboolean

Interface: WebviewTitleChangeEvent

NameTypeDescription
titlestring

Interface: WebviewScrollChangeEvent

NameTypeDescription
scroll"up" | "down"