有些時候,我們在開發過程中需要判斷,當前頁面被打開是否是處於源生的webview裡面,或者NODEJS做伺服器後端支持的時候,判斷請求來源是否來至於源生webview裡面被打開的頁面請求GET/POST/.. 於是寫了一個輪子,用javascript的形式判斷當前設備android或者ios,是不是 ...
有些時候,我們在開發過程中需要判斷,當前頁面被打開是否是處於源生的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屬性/設置中顯式設置的應用程式名稱。