# hook xhr Combined with Mini Program Safety Acceleration
# Access Guide
- Using ajax-hook
<script src="https://unpkg.com/ajax-hook@3.0.3/dist/ajaxhook.min.js ></script>
- Create a new gwpatch.js File, which reads as follows, modify the GATEWAY_DOMAIN Field for its own gateway domain name, and the resoureceAppid (Fill in any of the Mini Program white list appid That is, only as a source of distinction, not verified), where RESOLVE_GATEWAY_DOMAIN for Domain names that need to be added to the gateway forwarding
const c1 = new cloud.Cloud({
identityless: true,
resourceAppid: '***', // Appid, fill in the access Mini Program appid
config: {
customDomain: 'https://a1d5ad78d-***.sh.wxcloudrun.com' // Gateway access node domain name
}
})
c1.init() // Initialization instance
const gateway = c1.services.Gateway({ domain: 'a1d5ad78d-****.sh.wxcloudrun.com' }) // Gateway access node domain name, does not contain protocol header
const RESOLVE_GATEWAY_DOMAIN = ["https://Domain/"]
const GWFALLBACK=true
const UNPARSE_JSON = false //Controls whether to force json, true JSON is returned as string format
const UNPARES_URL_PERFIX = RESOLVE_GATEWAY_DOMAIN //Configuration whether the solution The path of json
function checkDomain(ob1,ob2 ){
for (var i=0i<ob1.lengthi++)
{
if(ob2.startsWith (ob1 [i])>0){
return true
}
}
return false
}
ah.proxy({
onRequest: (config, handler) => {
//config = Sigcrypto (config) //Tagging logic
if (checkDomain(RESOLVE_GATEWAY_DOMAIN,config.url) ) {
let nheader = config.headers
nheader["X-WX-HTTP-MODE"]= 'REROUTE'
gateway.call({
method:config.method,
path:config.url,
header:nheader,
data:config.body,
apiVersion:3,
}
).then(res => {
if (UNPARSE_JSON && checkDomain(UNPARES_URL_PERFIX,config.url) && res.header['content-type']=='application/json'){
res.data = JSON.stringify(res.data)
}
if (res.errMsg=="gateway.call:ok"){
handler.resolve(
{
config:config,
status:res.statusCode,
response:res.data,
headers:res.header
}
)
}else{
if (GWFALLBACK){
handler.next(config)
}else{
handler.resolve(
{
config:config,
status:res.statusCode,
response:res.data,
headers:res.header
}
)
}
}
}). catch((error)=>{
if (GWFALLBACK){
handler.next(config)
}else{
handler.resolve(
{
config:config,
status:error.errCode,
response:error.errMsg
}
)
}
})
} else {
handler.next(config)
}
}
})
- in HTML Introducing a Security Gateway Web SDK And hook SDK
<script src="https://web-9gikcbug35bad3a8-1304825656.tcloudbaseapp.com /sdk/2.1.0/cloud.js" importance="VeryHigh"></script>
<script src="./ajaxhook.min.js ></script>
<script src="gwpatch.js"></script>
- After successful introduction, all xhr The request will be automatically pulled websdk
- It is enough to test whether the request is normal, and whether it is encrypted. Be careful 2.0.4 of SDK Not allowed to open DevTools Debugging tool, or it will be intercepted.
# FAQ
Q: Can you define the ability to downgrade?
- Can be accessed through the above wxadapter of catch Partially customize the conditions for entering the demotion.
Q: How to close quickly?
- step 3 Cancel the entrance. adapter The application can be.
Q: Do you support active downgrading?
- In the above code, any errors are degraded by default, and you can also turn off the gateway s Web Access capability forces all requests to downgrade.