Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

JavaScriptBridge

繼承: Object

單例,在 Web 匯出中將引擎與流覽器的 JavaScript 本文連接。

說明

The JavaScriptBridge singleton is implemented only in the Web export. It's used to access the browser's JavaScript context. This allows interaction with embedding pages or calling third-party JavaScript APIs.

Note: This singleton can be disabled at build-time to improve security. By default, the JavaScriptBridge singleton is enabled. Official export templates also have the JavaScriptBridge singleton enabled. See Compiling for the Web in the documentation for more information.

教學

方法

JavaScriptObject

create_callback(callable: Callable)

Variant

create_object(object: String, ...) vararg

void

download_buffer(buffer: PackedByteArray, name: String, mime: String = "application/octet-stream")

Variant

eval(code: String, use_global_execution_context: bool = false)

void

force_fs_sync()

JavaScriptObject

get_interface(interface: String)

bool

is_js_buffer(javascript_object: JavaScriptObject)

PackedByteArray

js_buffer_to_packed_byte_array(javascript_buffer: JavaScriptObject)

bool

pwa_needs_update() const

Error

pwa_update()


訊號

pwa_update_available() 🔗

在偵測到該漸進式網路套用程式的更新,但因為存在活動的較早版本而等待啟動時觸發。要強制立即執行更新,請參閱 pwa_update()


方法說明

JavaScriptObject create_callback(callable: Callable) 🔗

Creates a reference to a Callable that can be used as a callback by JavaScript. The reference must be kept until the callback happens, or it won't be called at all. See JavaScriptObject for usage.

Note: The callback function must take exactly one Array argument, which is going to be the JavaScript arguments object converted to an array.


Variant create_object(object: String, ...) vararg 🔗

使用 new 建構子建立一個新的 JavaScript 物件。object 必須是 JavaScript window 的有效屬性。有關用法,請參閱 JavaScriptObject


void download_buffer(buffer: PackedByteArray, name: String, mime: String = "application/octet-stream") 🔗

提示使用者下載一個包含指定 buffer 的檔。該檔將具有給定的 namemime 型別。

注意:流覽器可能會覆蓋根據檔 name 的副檔名提供的 MIME 型別

注意:如果 download_buffer() 不是從使用者互動(例如按鈕點擊)中調用的,流覽器可能會阻止下載。

注意:如果快速連續發出多個下載請求,流覽器可能會要求使用者同意或阻止下載。


Variant eval(code: String, use_global_execution_context: bool = false) 🔗

Execute the string code as JavaScript code within the browser window. This is a call to the actual global JavaScript function eval().

If use_global_execution_context is true, the code will be evaluated in the global execution context. Otherwise, it is evaluated in the execution context of a function within the engine's runtime environment.


void force_fs_sync() 🔗

強制同步持久化檔案系統(當啟用時)。

注意:這僅對不能使用 FileAccess 寫入檔的模組或擴充有用。


JavaScriptObject get_interface(interface: String) 🔗

返回一個可以被腳本使用的 JavaScript 物件的介面。interface 必須是 JavaScript window 的有效屬性。回呼函式必須接受單個 Array 參數,它將包含 JavaScript arguments。有關用法,請參閱 JavaScriptObject


bool is_js_buffer(javascript_object: JavaScriptObject) 🔗

Returns true if the given javascript_object is of type ArrayBuffer, DataView, or one of the many typed array objects.


PackedByteArray js_buffer_to_packed_byte_array(javascript_buffer: JavaScriptObject) 🔗

Returns a copy of javascript_buffer's contents as a PackedByteArray. See also is_js_buffer().


bool pwa_needs_update() const 🔗

如果該漸進式網路套用程式有新版本等待啟動,則返回 true

注意:只在匯出為漸進式網路套用程式(Progressive Web App)時相關。


Error pwa_update() 🔗

執行該漸進式網路套用程式的線上更新。強制安裝新版本並重新載入該頁面。

注意:你的套用程式將在所有流覽器標籤頁中重新載入

注意:只在匯出為漸進式網路套用程式(Progressive Web App)且 pwa_needs_update() 返回 true 時相關。