Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit f1e5daf

Browse files
ajkshfjkajmaikai (A)
andauthored
fix: 将refresh替换为loadUrl (#110)
Co-authored-by: maikai (A) <[email protected]>
1 parent 637422d commit f1e5daf

File tree

3 files changed

+204
-183
lines changed

3 files changed

+204
-183
lines changed

harmony/rn_webview/src/main/ets/RNCWebView.ets

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import webview from '@ohos.web.webview';
2727
import { url as OSUrl } from '@kit.ArkTS';
2828
import { RNC } from '@rnoh/react-native-openharmony/generated';
2929
import Logger from './Logger';
30-
import { BaseOperate } from './webViewBaseOperate';
30+
import { BaseOperate } from './WebViewBaseOperate';
3131
import {
3232
AlertEvent,
3333
CACHE_MODE,
@@ -377,8 +377,8 @@ export struct RNCWebView {
377377
.overScrollMode(this.overScrollMode)
378378
.onProgressChange((event) => {
379379
if (event) {
380-
this.progress = event.newProgress
381-
this.onProgressChange()
380+
this.progress = event.newProgress
381+
this.onProgressChange()
382382
}
383383
})
384384
.onScroll((event) => {
@@ -453,14 +453,6 @@ export struct RNCWebView {
453453
if (!this.hasRegisterJavaScriptProxy) {
454454
this.registerPostMessage()
455455
}
456-
if (this.descriptorWrapper.props.userAgent) {
457-
try {
458-
this.controller.setCustomUserAgent(this.descriptorWrapper.props.userAgent);
459-
} catch (error) {
460-
Logger.debug(TAG,
461-
`[RNOH] setCustomUserAgent ErrorCode: ${error.code}, Message: ${error.message}, userAgent: ${this.descriptorWrapper.props.userAgent}`);
462-
}
463-
}
464456
})
465457
.onAlert((event) => this.onJavascriptAlert(event))
466458
.onConfirm((event) => this.onJavascriptConfirm(event))
@@ -570,7 +562,8 @@ export struct RNCWebView {
570562
}
571563
};
572564
this.controller.registerJavaScriptProxy(bridge, JAVASCRIPT_INTERFACE, ["postMessage"])
573-
this.controller.refresh()
565+
// this.controller.refresh()
566+
this.controller.loadUrl(this.source.uri, this.headers);
574567
this.hasRegisterJavaScriptProxy = true
575568
}
576569
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import { RNC } from '@rnoh/react-native-openharmony/generated/ts'
2+
import { webview } from '@kit.ArkWeb'
3+
import { CACHE_MODE, ONE_HUNDRED, WebViewEventParams } from './Magic';
4+
import Logger from './Logger';
5+
6+
export const TAG = "WebView"
7+
8+
9+
interface ProgressInterface {
10+
progress: number;
11+
}
12+
13+
interface LoadingErrorInterface {
14+
code: number;
15+
description: string
16+
}
17+
18+
interface ScrollInterface {
19+
x: number;
20+
y: number
21+
}
22+
23+
interface CreateWebViewEventInterface {
24+
type: string;
25+
progress: number;
26+
}
27+
28+
export class BaseOperate {
29+
static instance: BaseOperate | null = null
30+
private eventEmitter: RNC.RNCWebView.EventEmitter
31+
private controller: webview.WebviewController
32+
33+
constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) {
34+
this.eventEmitter = eventEmitter
35+
this.controller = controller
36+
}
37+
38+
static getInstance(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController): BaseOperate {
39+
if (BaseOperate.instance === null) {
40+
BaseOperate.instance = new BaseOperate(eventEmitter, controller)
41+
}
42+
return BaseOperate.instance
43+
}
44+
45+
emitProgressChange(params: ProgressInterface) {
46+
try {
47+
this.eventEmitter!.emit('loadingProgress', {
48+
url: this.controller.getUrl(),
49+
loading: params.progress != ONE_HUNDRED,
50+
title: this.controller.getTitle(),
51+
canGoBack: this.controller.accessBackward(),
52+
canGoForward: this.controller.accessForward(),
53+
lockIdentifier: 0,
54+
progress: params.progress / ONE_HUNDRED
55+
})
56+
} catch (error) {
57+
Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`);
58+
}
59+
}
60+
61+
emitLoadingStart(params: ProgressInterface) {
62+
try {
63+
this.eventEmitter!.emit('loadingStart', {
64+
url: this.controller.getUrl(),
65+
loading: params.progress != ONE_HUNDRED,
66+
title: this.controller.getTitle(),
67+
canGoBack: this.controller.accessBackward(),
68+
canGoForward: this.controller.accessForward(),
69+
lockIdentifier: 0,
70+
navigationType: "other",
71+
mainDocumentURL: ""
72+
})
73+
} catch (error) {
74+
Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`);
75+
}
76+
}
77+
78+
emitLoadingFinish(params: ProgressInterface) {
79+
try {
80+
this.eventEmitter!.emit('loadingFinish', {
81+
url: this.controller.getUrl(),
82+
loading: params.progress != ONE_HUNDRED,
83+
title: this.controller.getTitle(),
84+
canGoBack: this.controller.accessBackward(),
85+
canGoForward: this.controller.accessForward(),
86+
lockIdentifier: 0,
87+
navigationType: "other",
88+
mainDocumentURL: ""
89+
})
90+
} catch (error) {
91+
Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`);
92+
}
93+
}
94+
95+
emitLoadingError(params: LoadingErrorInterface) {
96+
try {
97+
this.eventEmitter!.emit('loadingError', {
98+
url: this.controller.getUrl(),
99+
loading: false,
100+
title: this.controller.getTitle(),
101+
canGoBack: this.controller.accessBackward(),
102+
canGoForward: this.controller.accessForward(),
103+
lockIdentifier: 0,
104+
domain: "",
105+
code: params.code,
106+
description: params.description
107+
})
108+
} catch (error) {
109+
Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`);
110+
}
111+
}
112+
113+
emitHttpError(params: LoadingErrorInterface) {
114+
try {
115+
this.eventEmitter!.emit('httpError', {
116+
url: this.controller.getUrl(),
117+
loading: false,
118+
title: this.controller.getTitle(),
119+
canGoBack: this.controller.accessBackward(),
120+
canGoForward: this.controller.accessForward(),
121+
lockIdentifier: 0,
122+
description: params.description,
123+
statusCode: params.code
124+
})
125+
} catch (error) {
126+
Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`);
127+
}
128+
}
129+
130+
emitScroll(params: ScrollInterface) {
131+
this.eventEmitter!.emit('scroll', {
132+
contentInset: {
133+
bottom: 0,
134+
left: 0,
135+
right: 0,
136+
top: 0
137+
},
138+
contentOffset: { y: params.y, x: params.x },
139+
contentSize: { height: 0, width: 0 },
140+
layoutMeasurement: { height: 0, width: 0 }
141+
})
142+
}
143+
144+
emitShouldStartLoadWithRequest(params: ProgressInterface) {
145+
try {
146+
this.eventEmitter!.emit('shouldStartLoadWithRequest', {
147+
url: this.controller.getUrl(),
148+
loading: params.progress != ONE_HUNDRED,
149+
title: this.controller.getTitle(),
150+
canGoBack: this.controller.accessBackward(),
151+
canGoForward: this.controller.accessForward(),
152+
lockIdentifier: 0,
153+
navigationType: "other",
154+
mainDocumentURL: "",
155+
isTopFrame: false
156+
})
157+
} catch (error) {
158+
Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`);
159+
}
160+
}
161+
162+
transCacheMode(cacheMode: CACHE_MODE): CacheMode {
163+
let mode = CacheMode.Default
164+
switch (cacheMode) {
165+
case CACHE_MODE.LOAD_DEFAULT:
166+
mode = CacheMode.Default
167+
break;
168+
case CACHE_MODE.LOAD_CACHE_ELSE_NETWORK:
169+
mode = CacheMode.None
170+
break;
171+
case CACHE_MODE.LOAD_NO_CACHE:
172+
mode = CacheMode.Online
173+
break;
174+
case CACHE_MODE.LOAD_CACHE_ONLY:
175+
mode = CacheMode.Only
176+
break;
177+
default:
178+
break;
179+
}
180+
return mode
181+
}
182+
183+
createWebViewEvent(param: CreateWebViewEventInterface): WebViewEventParams {
184+
let result: WebViewEventParams = new WebViewEventParams(param.type);
185+
result.loading = param.progress != ONE_HUNDRED
186+
try {
187+
result.url = this.controller.getUrl();
188+
result.title = this.controller.getTitle();
189+
result.canGoBack = this.controller.accessBackward();
190+
result.canGoForward = this.controller.accessForward();
191+
} catch (error) {
192+
result.url = "";
193+
result.title = "";
194+
result.canGoBack = false;
195+
result.canGoForward = false;
196+
}
197+
return result;
198+
}
199+
}

0 commit comments

Comments
 (0)