Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.
Merged
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
42 changes: 27 additions & 15 deletions harmony/rn_webview/src/main/ets/RNCWebView.ets
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export struct RNCWebView {
renderMode: RenderMode = RenderMode.SYNC_RENDER;
scrollEnabled = true;
nestedScroll = NestedScrollMode.SELF_FIRST;
injectedJavaScriptBeforeContentLoaded: Array<ScriptItem> = [
{ script: '', scriptRules: ["*"] }
];
headers: Array<webview.WebHeader> = []
allowPageStartInProgress = true;
@State webviewWidth: number = 0
@State webviewHeight: number = 0
Expand All @@ -158,7 +162,6 @@ export struct RNCWebView {
private descriptorWrapper: RNC.RNCWebView.DescriptorWrapper = {} as RNC.RNCWebView.DescriptorWrapper

private onDescriptorWrapperChange(descriptorWrapper: RNC.RNCWebView.DescriptorWrapper) {
this.descriptorWrapper = descriptorWrapper
this.initVariable()
if (this.html != "" && this.html != this.source.html) {
Logger.debug(TAG, "[RNOH] html is update")
Expand All @@ -180,7 +183,7 @@ export struct RNCWebView {
Logger.debug(TAG, `[RNOH] newDescriptor props update uri: ` + this.source.uri);
this.url = this.source.uri as string;
if (this.controllerAttached) {
this.controller.loadUrl(this.descriptorWrapper.props.newSource.uri)
this.controller.loadUrl(this.descriptorWrapper.props.newSource.uri, this.headers)
}
}
if (this.controllerAttached) {
Expand All @@ -190,9 +193,10 @@ export struct RNCWebView {

aboutToAppear() {
this.eventEmitter = new RNC.RNCWebView.EventEmitter(this.ctx.rnInstance, this.tag)
this.descriptorWrapper = this.ctx.descriptorRegistry.findDescriptorWrapperByTag<RNC.RNCWebView.DescriptorWrapper>(this.tag)!

this.initVariable()
this.url = this.source.uri as string;
webview.WebviewController.setWebDebuggingAccess(this.descriptorWrapper.props.webviewDebuggingEnabled)
this.cleanUpCallbacks.push(this.ctx.descriptorRegistry.subscribeToDescriptorChanges(this.tag,
(_descriptor, newDescriptorWrapper) => {
this.onDescriptorWrapperChange(newDescriptorWrapper! as RNC.RNCWebView.DescriptorWrapper)
Expand All @@ -211,6 +215,7 @@ export struct RNCWebView {
}

private initVariable() {
this.descriptorWrapper = this.ctx.descriptorRegistry.findDescriptorWrapperByTag<RNC.RNCWebView.DescriptorWrapper>(this.tag)!
this.javaScriptEnable = this.descriptorWrapper.props.javaScriptEnabled;
this.cacheMode =
this.descriptorWrapper.props.cacheEnabled ? this.transCacheMode(this.descriptorWrapper.props.cacheMode as CACHE_MODE) : CacheMode.Online;
Expand All @@ -219,7 +224,15 @@ export struct RNCWebView {
method: this.descriptorWrapper.props.newSource.method ?? "",
body: this.descriptorWrapper.props.newSource.body ?? "",
html: this.descriptorWrapper.props.newSource.html ?? "",
baseUrl: this.descriptorWrapper.props.newSource.baseUrl ?? ""
baseUrl: this.descriptorWrapper.props.newSource.baseUrl ?? "",
headers: this.descriptorWrapper.props.newSource.headers ?? []
}
if (this.source.headers) {
this.source.headers.forEach(item => {
if (item.name && item.value) {
this.headers.push({ headerKey: item.name, headerValue: item.value })
}
})
}
this.html = this.source.html
this.webviewWidth = this.descriptorWrapper.width
Expand All @@ -230,7 +243,6 @@ export struct RNCWebView {
if (this.source.uri && this.source.uri.toString().startsWith("asset://")) {
this.source.uri = $rawfile(this.source.uri.toString().replace("asset://", this.ctx.rnInstance.getAssetsDest()));
}
this.url = this.source.uri as string;

if(this.descriptorWrapper.props.overScrollMode === 'always') {
this.overScrollMode = OverScrollMode.ALWAYS
Expand All @@ -239,6 +251,12 @@ export struct RNCWebView {
}

this.overScrollMode = this.descriptorWrapper.props.bounces ? OverScrollMode.ALWAYS : OverScrollMode.NEVER;

if (this.descriptorWrapper.props.injectedJavaScriptBeforeContentLoaded) {
this.injectedJavaScriptBeforeContentLoaded = [
{ script: this.descriptorWrapper.props.injectedJavaScriptBeforeContentLoaded, scriptRules: ["*"] }
];
}
}

private registerPostMessage() {
Expand Down Expand Up @@ -615,6 +633,7 @@ export struct RNCWebView {
.overScrollMode(this.overScrollMode)
.backgroundColor(Color.Transparent)
.javaScriptAccess(this.javaScriptEnable)
.javaScriptOnDocumentStart(this.injectedJavaScriptBeforeContentLoaded)
.horizontalScrollBarAccess(this.descriptorWrapper.props.showsHorizontalScrollIndicator)
.verticalScrollBarAccess(this.descriptorWrapper.props.showsVerticalScrollIndicator)
.overviewModeAccess(this.descriptorWrapper.props.scalesPageToFit)
Expand Down Expand Up @@ -715,16 +734,9 @@ export struct RNCWebView {
Logger.error(TAG, "error:" + error)
}
} else if (uri != undefined && uri != "") {
let header = this.source.headers;
if (header != undefined) {
let headers: Array<webview.WebHeader> = [];
header.forEach(item => {
if (item.name && item.value) {
headers.push({ headerKey: item.name, headerValue: item.value })
}
})
this.controller.loadUrl(uri, headers);
}
this.controller.loadUrl(uri, this.headers);
} else {
this.controller.loadUrl(uri, this.headers);
}
if (!this.hasRegisterJavaScriptProxy) {
this.registerPostMessage()
Expand Down