Skip to content

How to define an object in WebView2 and expose it to JavaScript? #14

Description

@ZJRui

In WebView2, when defining a single object to inject into JavaScript from AutoHotkey (AHK), how should the methods of that object be defined?

; AHK Script

static getInjectStudent() {

    StudentAhKObject := {
        name: "zhangsan",
        getName: getName,
        setName: setName,
        standardMethod: standardMethod,
        standardMethod2: (this) => MsgBox(Type(this)),
        getLowerName: (this) => this.name,
    }
 
    standardMethod(this, paramA, paramB) {
        this.name := paramA . "-" . paramB
    }

    ; standardMethod2: (this) => MsgBox(Type(this)),

    getName() {
        return StudentAhKObject.name
    }
    
    setName(newName) {
        OutputDebug("newName:" newName)
        StudentAhKObject.name := newName
        OutputDebug(JSON.stringify(StudentAhKObject))
    }

    return StudentAhKObject
}

webCommandAppWindow.AddHostObjectToScript("AhkStudent", WebCommandWindow.getInjectStudent())
// JavaScript code
await window.chrome.webview.hostObjects.AhkStudent.setName("from web page");   // works correctly
await window.chrome.webview.hostObjects.AhkStudent.getName();                // works correctly
await window.chrome.webview.hostObjects.AhkStudent.standardMethod("paramA","paramB"); // error
await window.chrome.webview.hostObjects.AhkStudent.standardMethod2();        // error
await window.chrome.webview.hostObjects.AhkStudent.getLowerName();           // error

In the AHK script, when defining an object to expose to JavaScript, are its methods not invoked in the form objA.method()? Instead, are they called directly as method(), causing the this parameter to be unavailable? Is that the case?

When JavaScript calls the corresponding AHK methods—such as getName, setName, or standardMethod—how is this handled inside the AHK method during execution?

Is the method invoked in an object-oriented style (i.e., as a method of an object), where the first parameter automatically represents this?
Or is it executed as a plain function call, where the first parameter is not this?

In fact, the answer can be obtained by using Type(this).
Testing reveals that this is a Class, not an Object.

So I know the answer—but I don’t understand why this inside the method isn’t the AhkStudent object.

From my perspective, the JavaScript code
js
await window.chrome.webview.hostObjects.AhkStudent.getName();

clearly expresses the intent to call the getName method on the AhkStudent object.

Therefore, when AhkStudent invokes the getName method in AHK, I would expect the first parameter to be this (referring to the AhkStudent instance).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions