Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Browser, OS and device detection based on the available user agent string.
it's very rarely a good idea to use user agent sniffing. You can almost always find a better, more broadly compatible way to solve your problem! MDN: Browser detection using the user agent
In case some browser-specific issue cannot be fixed uniformly across the browsers we may need to perform (sad gasp) some browser detection. For example, browser X crashes when function Y from library Z is used, so we have to detect when we are dealing with browser X and disable library Z.
Another legitimate case is when we want to know what browsers, os and devices are most frequently used when our site is accessed. Then we can just analyze the user agent string sent in HTTP request headers on a server side. As a minor variation, alternatively, we can perform the user agent string analysis on the client side and send to the server only the results.
To install the library use either Bower or NPM:
bower install sniffr
Ready to use library is available in the dist folder.
NPM:
npm install sniffr
The library is meant to be used only in a browser, no server-side code is run. Include the library:
<script src="bower_components/dist/sniffr.min.js" />
after the script has loaded the object Sniffr
is available and can be used in the client code.
Example:
//If Windows and Firefox 28 or later
if (Sniffr.os.name === "windows"
&& Sniffr.browser.name === "firefox" && Sniffr.browser.version[0] >= 28) {
//Apply some workaround
}
Example:
//Sending user browser and os information to the server for further analysis
Stats.send(Sniffr.os, Sniffr.browser, Sniffr.device);
Sniffr.os
: operating system
Sniffr.browser
: browser
Sniffr.device
: device
Sniffr.sniff
: function that expects a user agent string as an argument, it is called automatically in a browser
Sniffr can also be used in a Node.js environment in case you need to do some server-side user agent analysis as well.
First install it
npm install sniffr
Then load the module, provide it the agent string and query the results just like in a browser environment:
var Sniffr = require("sniffr");
var s = new Sniffr();
s.sniff("Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
console.log("Operating System:");
console.log(s.os);
console.log("Browser:");
console.log(s.browser);
console.log("Device:");
console.log(s.device);
Some libraries like jQuery provide only browser information and not the OS information. Some like Detectizr are plugins for other libraries that you may not use. And some require server-side code. A few libraries are usable only on the server side or only in a browser.
Sniffr provides simple and symmetric API, does not depend on other libraries, does not require the server part, is tiny, fast and easily extensible. In addition, it can be used either in a browser environment and on the server side.
The original sniffing dog image location is http://publicdomainvectors.org/en/free-clipart/Dog-sniffing-vector-image/11807.html
FAQs
Browser, os and device detection
The npm package sniffr receives a total of 22,259 weekly downloads. As such, sniffr popularity was classified as popular.
We found that sniffr demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.