Article directory
I. Introduction
Project Requirements : Embed H5 page through , and uni-app
is the access address of the Internet page. There are many clickable links in the page. If there is no restriction, you can jump to other external link addresses at will, which has potential safety and compliance risks.webview
webview
src
webview
overrideUrlLoading
The URL request of the interception window can be implemented Webview
.
二、overrideUrlLoading
The syntax is as follows:
void wobj.overrideUrlLoading(options, callback);
illustrate:
- After intercepting
URL
the request,Webview
the window will not jump to the newURL
address, and thecallback
interceptedURL
address will be returned through the callback method (you can open a newWebview
window to loadURL
the page, etc.). - This method can only intercept the network hyperlink jump of the window (including
loadURL
the jump triggered by calling the method), and cannot intercept the page request resource request (such ascss/js/png
the request for loading and other resources). - When called multiple times
overrideUrlLoading
, only the parameter value set in the last call takes effect.
Parameters :
-
options
: (WebviewOverrideUrlOptions
) Optional intercept URL request parameters. -
effect
:(String type)URL
The effective timing of the interception request.
Possible values: -
"
instant
" - Indicates that it takes effect immediately, that is,overrideUrlLoading
it takes effect immediately after calling the method; -
"
touchstart
" - Indicates that it will take effect after the user operatesWebview
the window (triggerstouchstart
the event). If the user does not operateWebview
the window, the URL request operation will not be intercepted.
The default value is "instant".
-
mode
: (String type) interception mode. Possible values: "allow
", "reject
", the default value is "reject
".- "
allow
" indicates thatmatch
the url will not be intercepted to continue loading when the conditions defined by the attribute are met, and the jump will be intercepted and a callback will be triggeredmatch
when the conditions defined by the attribute are not met ;url
callback
- "
reject
"Indicates that the jump is intercepted and the callback is triggeredmatch
when the attribute definition is satisfied , and the loading is not intercepted when the attribute definition is not met.url
callback
match
url
- "
-
match
: (String type) Whether the request needs to be processed or notURL
. Regular expressions are supported, and the default value isURL
valid for all addresses (equivalent to the regular expression ".*"). Ifmode
the value is "allow
", the redirection of the regional allocationURL
request is allowed;mode
if the value is "reject
", the regional allocationURL
request is intercepted. -
exclude
: (String type) Exclude interception processing request types. Do not intercept and process specified types ofURL
requests, and directly use the system's default processing logic. Possible values:- "
none
"means not to exclude anyURL
request (that is, to intercept and process allURL
requests); - "
redirect
"Indicates to exclude interception processing301/302
jump requests (use with caution, non-a
labelhref
triggeredURL
requests may be misjudged as302
jumps). The default value is "none
".
- "
-
callback
: ( OverrideUrlLoadingCallback ) OptionalURL
callback function for intercepting requests.
Parameters:
event
: ( Event ) Mandatory Webview
The window intercepts URL
jump event data; the intercepted address
can be obtained through event
the property .url
URL
Return value :
void
: None
Platform support :
Android
- ALL (support) :5+APP
You need to select the "Run after decompressing resources" mode to intercept URL requests for application resources.iOS
- ALL (support)
2. Code solution
onReady() {
var that = this;
// #ifdef APP-PLUS
var pages = getCurrentPages();
var page = pages[pages.length - 1];
var currentWebview = page.$getAppWebview();
// 或者通过以下方式获得:const currentWebview = this.$mp.page.$getAppWebview();
var url = currentWebview.children()[0].getURL();
// 延时是为了正确获取到 child 也就是打开外链地址的web-view对象,overrideUrlLoading阻止外层对象没有用
setTimeout(()=>{
var wv = currentWebview.children()[0];
// 拦截所有页面的跳转,可使用参数拦截 .jd.com
wv.overrideUrlLoading({
// “reject" 表示满足match属性定义的提交时拦截url跳转并触发callback回调,不满足match属性定义的条件时不拦截url继续加载。
// “allow” 表示满足match属性定义的条件时不拦截url继续加载,不满足match属性定义的条件时拦截url跳转并触发callback回调;
mode: 'allow',
match: '.*jd\.com/.*'
}, function(e) {
try {
// 根据自己的业务需求,处理链接上的参数,进行跳转
var navURL =`/pages/index/order/order?id=${
xxx}`;
uni.navigateTo({
url: navURL,
animationType: 'pop-in'
})
} catch (e) {
console.log(e)
//TODO handle the exception
}
console.log('reject Url', url);
})
}, 500);
// #endif
},
The above is based on your own business needs, and you can complete webview
the request interception in the H5 page.