diff --git a/entry/src/main/ets/common/Constants.ets b/entry/src/main/ets/common/Constants.ets index 8b29a77a3207ee4ddd2aa4769db89bcbfe23eca7..093e1e2705a99b061d20bbcf390fa49ec3e98f5a 100644 --- a/entry/src/main/ets/common/Constants.ets +++ b/entry/src/main/ets/common/Constants.ets @@ -48,4 +48,10 @@ export class Constants { * Index page padding bottom size. */ public static readonly PADDING_BOTTOM_INDEX: number = 16; + + /** + * Set the url whitelist of the current web + */ + public static readonly URL_TRUST: string = + '{\"UrlPermissionList\": [{\"scheme\": \"https\",\"host\": \"developer.huawei.com/consumer/cn/next\"}]}'; } \ No newline at end of file diff --git a/entry/src/main/ets/pages/WebPage.ets b/entry/src/main/ets/pages/WebPage.ets index 45af523a35a6b3d8e226b1226457a4e94c363f3a..3cb000da5097c5b2a841b5349c6bcfb320eff840 100644 --- a/entry/src/main/ets/pages/WebPage.ets +++ b/entry/src/main/ets/pages/WebPage.ets @@ -13,33 +13,33 @@ * limitations under the License. */ -import { webview } from '@kit.ArkWeb'; import { UIContext, NodeController, BuilderNode, FrameNode } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { common } from '@kit.AbilityKit'; +import { webview } from '@kit.ArkWeb'; import { ConfigMapKey, ResourceUtil } from '../common/ResourceUtil'; +import { Constants } from '../common/Constants'; class Data { public url: string = ResourceUtil.getRawFileStringByKey(getContext(this) as common.UIAbilityContext, ConfigMapKey.GALLERY_URL); public controller: WebviewController = new webview.WebviewController(); - - constructor() { - try { - // Set the whitelist to allow access to only the trust web page. - this.controller.setUrlTrustList(ConfigMapKey.GALLERY_URL); - } catch (error) { - hilog.info(0x0000, 'WebviewController', '%{public}s', - `ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); - } - } } @Builder function webBuilder(data: Data) { Column() { Web({ src: data.url, controller: data.controller }) + .onControllerAttached(()=>{ + try { + // Set the whitelist to allow access to only the trust web page. + data.controller.setUrlTrustList(Constants.URL_TRUST); + } catch (error) { + hilog.info(0x0000, 'WebviewController', '%{public}s', + `ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); + } + }) .width($r('app.string.full_height_width')) .height($r('app.string.full_height_width')) .domStorageAccess(true)