On macOS 11+, this will just call through to window.webkit.messageHandlers.x.postMessage

Eg: for a foo message defined in Swift that accepted the payload {"bar": "baz"}, the following would occur:

const json = await window.webkit.messageHandlers.foo.postMessage({ bar: "baz" });
const response = JSON.parse(json)

On macOS 10 however, the process is a little more involved. A method will be appended to window that allows the response to be delivered there instead. It's not exactly this, but you can visualize the flow as being something along the lines of:

// add the window method
window["_0123456"] = (response) => {
// decrypt `response` and deliver the result to the caller here
// then remove the temporary method
delete window['_0123456']
};

// send the data + `messageHanding` values
window.webkit.messageHandlers.foo.postMessage({
bar: "baz",
messagingHandling: {
methodName: "_0123456",
secret: "super-secret",
key: [1, 2, 45, 2],
iv: [34, 4, 43],
}
});

// later in swift, the following JavaScript snippet will be executed
(() => {
window['_0123456']({
ciphertext: [12, 13, 4],
tag: [3, 5, 67, 56]
})
})()

Constructors

Properties

globals: {
    addEventListener: any;
    Arrayfrom: {
        <T>(arrayLike: ArrayLike<T>): T[];
        <T, U>(
            arrayLike: ArrayLike<T>,
            mapfn: (v: T, k: number) => U,
            thisArg?: any,
        ): U[];
        <T>(iterable: Iterable<T> | ArrayLike<T>): T[];
        <T, U>(
            iterable: Iterable<T> | ArrayLike<T>,
            mapfn: (v: T, k: number) => U,
            thisArg?: any,
        ): U[];
    };
    capturedWebkitHandlers: Record<string, any>;
    Error: ErrorConstructor;
    getRandomValues: any;
    JSONparse: (
        text: string,
        reviver?: (this: any, key: string, value: any) => any,
    ) => any;
    JSONstringify: {
        (
            value: any,
            replacer?: (this: any, key: string, value: any) => any,
            space?: string | number,
        ): string;
        (
            value: any,
            replacer?: null | (string | number)[],
            space?: string | number,
        ): string;
    };
    ObjectDefineProperty: <T>(
        o: T,
        p: PropertyKey,
        attributes: PropertyDescriptor & ThisType<any>,
    ) => T;
    Promise: PromiseConstructor;
    ReflectDeleteProperty: any;
    TextDecoder: new (
        label?: string,
        options?: TextDecoderOptions,
    ) => TextDecoder;
    TextEncoder: new () => TextEncoder;
    Uint16Array: Uint16ArrayConstructor;
    Uint32Array: Uint32ArrayConstructor;
    Uint8Array: Uint8ArrayConstructor;
    window: Window & typeof globalThis;
}

Type declaration

  • addEventListener: any
  • Arrayfrom: {
        <T>(arrayLike: ArrayLike<T>): T[];
        <T, U>(
            arrayLike: ArrayLike<T>,
            mapfn: (v: T, k: number) => U,
            thisArg?: any,
        ): U[];
        <T>(iterable: Iterable<T> | ArrayLike<T>): T[];
        <T, U>(
            iterable: Iterable<T> | ArrayLike<T>,
            mapfn: (v: T, k: number) => U,
            thisArg?: any,
        ): U[];
    }
  • capturedWebkitHandlers: Record<string, any>
  • Error: ErrorConstructor
  • getRandomValues: any
  • JSONparse: (text: string, reviver?: (this: any, key: string, value: any) => any) => any
  • JSONstringify: {
        (
            value: any,
            replacer?: (this: any, key: string, value: any) => any,
            space?: string | number,
        ): string;
        (
            value: any,
            replacer?: null | (string | number)[],
            space?: string | number,
        ): string;
    }
  • ObjectDefineProperty: <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T
  • Promise: PromiseConstructor
  • ReflectDeleteProperty: any
  • TextDecoder: new (label?: string, options?: TextDecoderOptions) => TextDecoder
  • TextEncoder: new () => TextEncoder
  • Uint16Array: Uint16ArrayConstructor
  • Uint32Array: Uint32ArrayConstructor
  • Uint8Array: Uint8ArrayConstructor
  • window: Window & typeof globalThis
messagingContext: MessagingContext

Methods

  • When required (such as on macos 10.x), capture the postMessage method on each webkit messageHandler

    Parameters

    • handlerNames: string[]

    Returns void