Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
platform-detect
Advanced tools
🃏 Minimalistic isomorphic library for detection of platform, runtime, APIs and more.
🃏 Minimalistic isomorphic library for detection of platform, runtime, APIs and more.
npm install platform-detect
ur use CDN like unpkg.com/platform-detect (comes with)
<script src="https://unpkg.com/platform-detect"></script>
Import everything
import platform from 'platform-detect'
// The script has no GUI to render content to.
// It only runs in console / terminal. (Might be a Node script or WebWorker)
platform.terminal
// App has a window, access to DOM. Can render GUI.
platform.gui
var platform = require('platform-detect')
// Fully functional Node & core modules are available. (Might be an Electron / NWJS app or a good old Node console script)
platform.node
// App has been loaded as a plain website in a browser.
platform.website
// App is served from the web (could be website or PWA)
platform.web
// App is packaged, compiled or bundled and not served from the web. Could be UWP, Electron, NW.JS, Chrome App, Cordova, etc...
platform.packaged
// Script is executed inside Web Worker.
platform.worker
Or import just what you need
import {windows, android, linux, macos, tizen} from 'platform-detect/os.mjs'
import {chrome, edge, safari} from 'platform-detect/browser.mjs'
import {input, mouse, touch, formfactor} from 'platform-detect/formfactor.mjs'
if (formfactor === 'tv' && tizen) {
console.log(`I'm a Samsung Smart TV!`)
}
if (windows && edge && (uwp || pwa)) {
console.log(`I should use Fluent Design System`)
} else if (android || chromeos) {
console.log(`I should use Material Design Language`)
}
if (touch) {
console.log(`I'm a device with touchscreen`)
if (mouse) {
console.log(`But I also have a mouse (it's the primary input type now)`)
console.log(`I'm a laptop with touchscreen or a Surface Pro with attached keyboard`)
} else {
console.log(`Mouse is currently not the primary input type`)
console.log(`I might be phone, tablet, or Surface Pro in tablet mode`)
}
}
You can choose between the old UMD module.
<script src="./node_modules/platform-detect/index.js"></script>
<script>
var platform = window['platform-detect']
console.log('pixel ratio of this device is', platform.pixelRatio)
</script>
Or the new ES Modules.
<script type="module">
import platform from './node_modules/platform-detect/index.mjs'
console.log(platform.pwa ? `I'm installed PWA app` : `I'm just a website`)
platform.on('orientation', orientation => console.log(orientation))
</script>
Be advised: The library uses various ES6 and newer features. The syntax is compiled down to ES5 using babel. But you need to polyfill the built-in methods in case you target browsers that don't implement them.
Namely String.prototype.includes
and Array.from
.
For example:
if (!String.prototype.includes) {
String.prototype.includes = function(item) {
return this.indexOf(item) !== -1
}
}
if (!Array.from) {
Array.from = function(nodelist) {
return Array.prototype.slice.call(nodelist)
}
}
Check out the demo for full table of APIs
By importing the index.js
bundle you get all of the below. Or you can pick only certain file with the checks you need.
terminal
bool
gui
bool
runtime.mjs
node
bool
pwa
bool
web
bool
website
bool
packaged
bool
worker
bool
nwjs
boolelectron
boolcordova
booluwp
boolformfactor.mjs
orientation
string, event
portrait
or landscape
portrait
bool, eventlandscape
bool, eventformfactor
string, event
phone
, tablet
, desktop
or tv
pixelRatio
float
input
string, event
mouse
or touch
touch
bool
true
if the device has a touchscreen, regardless of current primary input type, tablet mode, etc...mouse
bool, event
gamepad
boolbrowser.mjs
Browsers:
edge
bool (all of them, legacy, the new chromium, all the webkit/blink mobile verions)chrome
boolfirefox
boolopera
boolsamsungBrowser
boolsafari
boolie
booledgeAndroid
, edgeIos
, chromeIos
, firefoxIos
(these are in fact blink or webkit)Rendering engines:
edgeHtml
bool (the old one)edgeChromium
bool (the new one)blink
bool (chrome and all Android browswers)webkit
bool (safari and all iOs browswers)gecko
booltrident
boolos.mjs
android
boolchromeos
booltizen
boolios
boollinuxBased
boolwindows
boolmacos
boollinux
boolSome aspects can change during app's life. You can listen to these changes with EventEmitter-like api.
platform.on('orientation', orientation => console.log('orientation changed': orientation))
platform.on('input', input => {
if (input === 'mouse')
console.log('keyboard attached')
else
console.log('tablet mode')
})
[3.0.1]
FAQs
🃏 Minimalistic isomorphic library for detection of platform, runtime, APIs and more.
The npm package platform-detect receives a total of 10,492 weekly downloads. As such, platform-detect popularity was classified as popular.
We found that platform-detect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.