当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js判断当前浏览器是否是源生app的webview

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

2019年02月28日  | 移动技术网IT编程  | 我要评论

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

js-is-webview

github:

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属性/设置中显式设置的应用程序名称。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网