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

有些时候,我们在开发过程中需要判断,当前页面被打开是否是处于源生的webview里面,或者NODEJS做服务器后端支持的时候,判断请求来源是否来至于源生webview里面被打开的页面请求GET/POST/.. 于...

有些时候,我们在开发过程中需要判断,当前页面被打开是否是处于源生的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属性/设置中显式设置的应用程序名称。
  • 发表于 2019-02-27 22:20
  • 阅读 ( 329 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除