js判斷當前瀏覽器是否是源生app的webview


有些時候,我們在開發過程中需要判斷,當前頁面被打開是否是處於源生的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屬性/設置中顯式設置的應用程序名稱。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM