js判断当前浏览器是否是源生app的webview

本文介绍了一个javascript库`js-is-webview`,用于判断当前页面是否在Android或iOS的源生webview中打开,或者是在浏览器中打开。该库支持Node.js服务端和浏览器环境,并通过User-Agent嗅探来识别HTTP请求是否由UIWebview/Webview发起。文章详细讲解了安装、使用方法以及API接口。

有些时候,我们在开发过程中需要判断,当前页面被打开是否是处于源生的webview里面,或者NODEJS做服务器后端支持的时候,判断请求来源是否来至于源生webview里面被打开的页面请求GET/POST/..
于是写了一个轮子,用javascript的形式判断当前设备android或者ios,是不是又webview来装载页面内容 还是 浏览器(非webview)来装载页面内容。

js-is-webview

github:https://github.com/wuliqiangqiang/is-webview

use javascrupt to know it is webview ?
support node and the browser(webpack)
pass User-Agent sniffing,know the HTTP/HTTPS request is performed by a UIWebview/Webview or not.

安装

npm install js-is-webview --save

Node 服务端的用法

const isWebview = require("js-is-webview");
const is_webview = new isWebview();

const server = (req, res, next) => {
    - let userAgent = {
        - userAgent:req.headers['User-Agent']
    }
    // if true
    if(isWebview.check(userAgent))){
        //...
        next();
    }
};

浏览器 的用法

const isWebview = require("js-is-webview");
const is_webview = new isWebview();

let userAgent = {
    userAgent:navigator.userAgent
}

if(isWebview.check(userAgent))){
    //...
}

API

唯一API方法,检查是否来自webview

iswebview.check({userAgent})

  • userAgent (Object.key): Browser User-Agent header string
if(isWebview.check({userAgent}))){
    //...
}

iswebview.check({userAgent, configObject})

  • userAgent (Object.key): Browser User-Agent header string - 浏览器标识
  • configObject (Object)
    • appName (String): the app name explicitly set in your WebView properties/settings. -在WebView属性/设置中显式设置的应用程序名称。

转载于:https://www.cnblogs.com/sheep-sheep/p/10446277.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值