Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions harmony/rn_webview/src/main/ets/Magic.ets
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,17 @@ export interface ScrollInterface {
export enum WebViewOverScrollMode {
ALWAYS = "always",
NEVER = "never"
}

export enum COMMAND_NAME {
INJECTJAVASCRIPT = 'injectJavaScript',
POSTMESSAGE = 'postMessage',
RELOAD = 'reload',
GOBACK = 'goBack',
GOFORWARD = 'goForward',
REQUESTFOCUS = 'requestFocus',
CLEARCACHE = 'clearCache',
CLEARHISTORY = 'clearHistory',
STOPLOADING = 'stopLoading',
LOADURL='loadUrl'
}
123 changes: 3 additions & 120 deletions harmony/rn_webview/src/main/ets/RNCWebView.ets
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import {
WebViewEventParams,
WebViewNewSource,
WebViewOverScrollMode,
ZERO
ZERO,
COMMAND_NAME
} from './Magic';
import { bundleManager } from '@kit.AbilityKit';

Expand Down Expand Up @@ -152,125 +153,7 @@ export struct RNCWebView {
registerCommandCallback() {
this.cleanupCommandCallback = this.ctx.componentCommandReceiver.registerCommandCallback(
this.tag,
(command, args: string[]) => {
switch (command) {
case "injectJavaScript":
Logger.debug(TAG, `[RNOH] injectJavaScript,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.runJavaScript("(function() {\n" + args[0] + ";\n})();")
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "postMessage":
Logger.debug(TAG, `[RNOH] postMessage,${JSON.stringify(args)}`)
let data = JSON.stringify({ data: args[0] })
let result: string = "(function () {" +
"var event;" +
"var data = " + data.toString() + ";" +
"try {" +
"event = new MessageEvent('message', data);" +
"} catch (e) {" +
"event = document.createEvent('MessageEvent');" +
"event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
"}" +
"document.dispatchEvent(event);" +
"})();"
if (this.controllerAttached) {
try {
this.controller.runJavaScript(result)
} catch (error) {
Logger.error(TAG, "error:" + error)
}
}
break;
case "reload":
Logger.debug(TAG, `[RNOH] reload,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.refresh();
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "goBack":
Logger.debug(TAG, `[RNOH] goBack,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.backward();
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "goForward":
Logger.debug(TAG, `[RNOH] goForward,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.forward();
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "requestFocus":
Logger.debug(TAG, `[RNOH] requestFocus,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.requestFocus();
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "clearCache":
Logger.debug(TAG, `[RNOH] clearCache,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
const removeFlag = !!args[0] === true;
this.controller.removeCache(removeFlag);
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "clearHistory":
Logger.debug(TAG, `[RNOH] clearHistory,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.clearHistory();
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "stopLoading":
Logger.debug(TAG, `[RNOH] stopLoading,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.stop();
} catch (error) {
Logger.error(TAG, "error: " + error)
}
}
break
case "loadUrl":
Logger.debug(TAG, `[RNOH] loadUrl,${JSON.stringify(args)}`)
if (this.controllerAttached) {
try {
this.controller.loadUrl(args[0]);
} catch (error) {
Logger.error(TAG, `[RNOH] loadUrl Errorcode: ${JSON.stringify(error)}`);
}
}
break
default:
break
}
});
(command: COMMAND_NAME, args: string[]) => this.webViewBaseOperate?.registerCommandCallback(command, args));
}

onLoadingStart() {
Expand Down
129 changes: 127 additions & 2 deletions harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RNC } from '@rnoh/react-native-openharmony/generated/ts'
import { webview } from '@kit.ArkWeb'
import { CACHE_MODE, ONE_HUNDRED, WebViewEventParams } from './Magic';
import { CACHE_MODE, COMMAND_NAME, ONE_HUNDRED, WebViewEventParams } from './Magic';
import Logger from './Logger';

export const TAG = "WebView"
Expand Down Expand Up @@ -214,7 +214,132 @@ export class BaseOperate {
}
}

onErrorReceive(){
injectJavaScript(args: string[]) {
try {
this.controller.runJavaScript("(function() {\n" + args[0] + ";\n})();")
} catch (error) {
Logger.error(TAG, `[RNOH] injectJavaScript Errorcode: ${error.code}`);
}
}

postMessage(args: string[]) {
let data = JSON.stringify({ data: args[0] })
let result: string = "(function () {" +
"var event;" +
"var data = " + data.toString() + ";" +
"try {" +
"event = new MessageEvent('message', data);" +
"} catch (e) {" +
"event = document.createEvent('MessageEvent');" +
"event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
"}" +
"document.dispatchEvent(event);" +
"})();"
try {
this.controller.runJavaScript(result)
} catch (error) {
Logger.error(TAG, `[RNOH] postMessage Errorcode: ${error.code}`);
}
}

reload() {
try {
this.controller.refresh();
} catch (error) {
Logger.error(TAG, `[RNOH] reload Errorcode: ${error.code}`);
}
}

goBack() {
try {
this.controller.backward();
} catch (error) {
Logger.error(TAG, `[RNOH] goBack Errorcode: ${error.code}`);
}
}

goForward() {
try {
this.controller.forward();
} catch (error) {
Logger.error(TAG, `[RNOH] goForward Errorcode: ${error.code}`);
}
}

requestFocus() {
try {
this.controller.requestFocus();
} catch (error) {
Logger.error(TAG, `[RNOH] requestFocus Errorcode: ${error.code}`);
}
}

clearCache(args: string[]) {
try {
const removeFlag = !!args[0] === true;
this.controller.removeCache(removeFlag);
} catch (error) {
Logger.error(TAG, `[RNOH] clearCache Errorcode: ${error.code}`);
}
}

clearHistory() {
try {
this.controller.clearHistory();
} catch (error) {
Logger.error(TAG, `[RNOH] clearHistory Errorcode: ${error.code}`);
}
}

stopLoading() {
try {
this.controller.stop();
} catch (error) {
Logger.error(TAG, `[RNOH] stopLoading Errorcode: ${error.code}`);
}
}

loadUrl(args: string[]) {
try {
this.controller.loadUrl(args[0]);
} catch (error) {
Logger.error(TAG, `[RNOH] loadUrl Errorcode: ${error.code}`);
}
}

registerCommandCallback(commandName: COMMAND_NAME, args: string[]) {
switch (commandName) {
case COMMAND_NAME.INJECTJAVASCRIPT:
this.injectJavaScript(args)
break
case COMMAND_NAME.POSTMESSAGE:
this.postMessage(args)
break
case COMMAND_NAME.RELOAD:
this.reload()
break
case COMMAND_NAME.GOBACK:
this.goBack()
break
case COMMAND_NAME.GOFORWARD:
this.goForward()
break
case COMMAND_NAME.REQUESTFOCUS:
this.requestFocus()
case COMMAND_NAME.CLEARCACHE:
this.clearCache(args)
case COMMAND_NAME.CLEARHISTORY:
this.clearHistory()
break
case COMMAND_NAME.STOPLOADING:
this.stopLoading()
break
case COMMAND_NAME.LOADURL:
this.loadUrl(args)
break
default:
break
}

}
}