Skip to content Skip to sidebar Skip to footer

How Can I Properly Use Usparser() To Get My Computers Cpu In Reactjs?

I am fairly new to JavaScript and recently installed UAParser-JS from https://github.com/faisalman/ua-parser-js into my ReactJS project and am trying to figure out how to properly

Solution 1:

I was able to import the UAParser like this: import { UAParser } from 'ua-parser-js'; and now the code works properly.

Solution 2:

Just copie pasted the example on the GitHub repo here's what I've got

var parser = newUAParser();

    // by default it takes ua string from current browser's window.navigator.userAgentconsole.log(parser.getResult());
    /*
        /// this will print an object structured like this:
        {
            ua: "",
            browser: {
                name: "",
                version: ""
            },
            engine: {
                name: "",
                version: ""
            },
            os: {
                name: "",
                version: ""
            },
            device: {
                model: "",
                type: "",
                vendor: ""
            },
            cpu: {
                architecture: ""
            }
        }
    */// let's test a custom user-agent string as an examplevar uastring = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2";
    parser.setUA(uastring);

    var result = parser.getResult();
    // this will also produce the same result (without instantiation):// var result = UAParser(uastring);console.log(result.browser);        // {name: "Chromium", version: "15.0.874.106"}console.log(result.device);         // {model: undefined, type: undefined, vendor: undefined}console.log(result.os);             // {name: "Ubuntu", version: "11.10"}console.log(result.os.version);     // "11.10"console.log(result.engine.name);    // "WebKit"console.log(result.cpu.architecture);   // "amd64"// do some other testsvar uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
    console.log(parser.setUA(uastring2).getBrowser().name); // "Konqueror"console.log(parser.getOS());                            // {name: "OpenBSD", version: undefined}console.log(parser.getEngine());                        // {name: "KHTML", version: "4.1.4"}var uastring3 = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';
    console.log(parser.setUA(uastring3).getDevice().model); // "PlayBook"console.log(parser.getOS())                             // {name: "RIM Tablet OS", version: "1.0.0"}console.log(parser.getBrowser().name);                  // "Safari"
<scriptsrc="https://cdn.jsdelivr.net/npm/ua-parser-js@0/dist/ua-parser.min.js"></script>

Post a Comment for "How Can I Properly Use Usparser() To Get My Computers Cpu In Reactjs?"