开源软件名称(OpenSource Name):kaimallea/isMobile开源软件地址(OpenSource Url):https://github.com/kaimallea/isMobile开源编程语言(OpenSource Language):TypeScript 99.0%开源软件介绍(OpenSource Introduction):isMobileA simple JS library that detects mobile devices in both the browser and NodeJS. Why use isMobile?In the BrowserYou might not need this library. In most cases, responsive design solves the problem of controlling how to render things across different screen sizes. I recommend a mobile first approach. But there are always edge cases. If you have an edge case, then this library might be for you. My edge case at the time was redirecting users to a completely separate mobile site. I tried to keep this script small (currently ~1.3k bytes, minified) and simple, because it would need to execute in the How it works in the browserisMobile runs quickly during initial page load to detect mobile devices; it then creates a JavaScript object with the results. In NodeJSYou might want to use this library to do server-side device detection to minimize the amount of bytes you send back to visitors. Or you have your own arbitrary use case. How is works in NodeJSYou import and call the Devices detected by isMobileIn a browser, the following properties of the global Apple devices
Android devices
Amazon Silk devices (also passes Android checks)
Windows devices
"Other" devices
Aggregate Groupings
UsageNode.jsInstallyarn add ismobilejs or npm install ismobilejs Useimport isMobile from 'ismobilejs';
const userAgent = req.headers['user-agent'];
console.log(isMobile(userAgent).any); Or pass in a // this is just an example. window.navigator is readonly in the browser
window.navigator = {
...
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko)',
platform: 'MacIntel',
maxTouchPoints: 2,
..
} import isMobile from 'ismobilejs';
console.log(isMobile(window.navigator).apple.tablet); BrowserA real-word example: I include the minified version of the script, inline, and at the top of the <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script>
// Minified version of isMobile included in the HTML since it's small
(function () {var a={};var f=/iPhone/i,h=/iPod/i,i=/iPad/i,r=/\biOS-universal(?:.+)Mac\b/i,g=/\bAndroid(?:.+)Mobile\b/i,j=/Android/i,c=/(?:SD4930UR|\bSilk(?:.+)Mobile\b)/i,d=/Silk/i,b=/Windows Phone/i,k=/\bWindows(?:.+)ARM\b/i,m=/BlackBerry/i,n=/BB10/i,o=/Opera Mini/i,p=/\b(CriOS|Chrome)(?:.+)Mobile/i,q=/Mobile(?:.+)Firefox\b/i;function s(l){return function($){return $.test(l)}}function e(l){var $=(l=l||("undefined"!=typeof navigator?navigator.userAgent:"")).split("[FBAN");void 0!==$[1]&&(l=$[0]),void 0!==($=l.split("Twitter"))[1]&&(l=$[0]);var a=s(l),e={apple:{phone:a(f)&&!a(b),ipod:a(h),tablet:!a(f)&&a(i)&&!a(b),universal:a(r),device:(a(f)||a(h)||a(i))&&!a(b)},amazon:{phone:a(c),tablet:!a(c)&&a(d),device:a(c)||a(d)},android:{phone:!a(b)&&a(c)||!a(b)&&a(g),tablet:!a(b)&&!a(c)&&!a(g)&&(a(d)||a(j)),device:!a(b)&&(a(c)||a(d)||a(g)||a(j))||a(/\bokhttp\b/i)},windows:{phone:a(b),tablet:a(k),device:a(b)||a(k)},other:{blackberry:a(m),blackberry10:a(n),opera:a(o),firefox:a(q),chrome:a(p),device:a(m)||a(n)||a(o)||a(q)||a(p)},any:!1,phone:!1,tablet:!1};return e.any=e.apple.universal||e.apple.device||e.android.device||e.windows.device||e.other.device,e.phone=e.apple.phone||e.android.phone||e.windows.phone,e.tablet=e.apple.tablet||e.android.tablet||e.windows.tablet,e}a=e();if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=a}else if(typeof define==="function"&&define.amd){define(function(){return a})}else{this["isMobile"]=a}})();
// My own arbitrary use of isMobile, as an example
(function() {
var MOBILE_SITE = '/mobile/index.html', // site to redirect to
NO_REDIRECT = 'noredirect'; // cookie to prevent redirect
// I only want to redirect iPhones, Android phones
if (isMobile.apple.phone || isMobile.android.phone) {
// Only redirect if the user didn't previously choose
// to explicitly view the full site. This is validated
// by checking if a "noredirect" cookie exists
if (document.cookie.indexOf(NO_REDIRECT) === -1) {
document.location = MOBILE_SITE;
}
}
})();
</script>
</head>
<body>
<!-- imagine lots of html and content -->
</body>
</html> jsDelivr CDNAlternatively, you can include this library via jsDelivr CDN in a
Visit the isMobile jsDelivr page to get the most up-to-date URL pointing to the lastest version. Building manuallyAfter checking out the repo, install dependencies: yarn install Then build the library: yarn build Three versions of the library will be generated:
Additionally, types will be output to ContributingThis library uses Spotify's web-scripts project to build, lint, test, format and release the this library. You must use |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论