The bridge object wrapper for CefView.
// create CefViewBridge instanceletmyBridge=newCefViewBridge("MyBridge");///////////////////////////////////////////////////////////////// listen for native eventsfunctiononChangeColorEvent(color){console.log("request change background color to: ",color);// change the background colordocument.getElementById("main").style.backgroundColor=color;}// add event listener for colorChange eventmyBridge.addEventListener("colorChange",onChangeColorEvent);// remove event listener for colorChange eventmyBridge.removeEventListener("colorChange",onChangeColorEvent);///////////////////////////////////////////////////////////////// invoke native methodsletarg={d1: true,d2: 5678,d3: "test object",d4: [1,"2",false],d5: {d1: true,d2: 5678,d3: "nested object",d4: [1,"2",true],},};// invoke native method with parameters// The native method prototype is:// void TestMethod(int arg1, bool arg2, std::string arg3, JsonObject d4);myBridge.invoke("TestMethod",1,false,"arg3",arg);///////////////////////////////////////////////////////////////// exchange data with native using CefViewQueryvarrequest={a: 1234,b: false,c: "test string",d: {d1: true,d2: 5678,d3: "nested object",d4: [1,"2",true],},};letqueryId=myBridge.cefViewQuery(request,(response)=>{alert(response);},(error_code,error_message)=>{alert(`query failed, code: ${error_code}, msg: ${error_message}`);});// you can cancel the query before it is processed by nativemyBridge.cefViewQueryCancel(queryId);Bridge class for communicating with the CefView backend.
constructor(bridgeName: string)- bridgeName: The name of the bridge object on the window.
addEventListener(e: string,handler: (...args: any[])=>void): void- Adds an event listener to the bridge.
- e: Event name.
- handler: Event handler function.
removeEventListener(e: string,handler: (...args: any[])=>void): void- Removes an event listener from the bridge.
- e: Event name.
- handler: Event handler function.
invoke(method: string, ...args: any[]): void- Invokes a method on the bridge.
- method: Method name.
- args: Arguments to pass to the method.
query(request: object,onSuccess: (response: string)=>void,onFailure: (error: number,message: string)=>void): number- Sends a query to the CefView backend.
- request: Request object.
- onSuccess: Success callback,
(res) => {}. - onFailure: Failure callback,
(error, message) => {}. - Returns: Query id.
cancelQuery(queryId: number): void- Cancels a previously sent query.
- queryId: Query id to cancel.
Contributions are welcome! Please open issues or submit pull requests for bug fixes, improvements, or new features.
- Fork the repository
- Create a new branch (
git checkout -b feature/your-feature) - Commit your changes
- Push to your fork and submit a pull request
This project is licensed under the MIT License. See LICENSE for details.