From 3eb1c1c352a0b1874a31d5d8054e233bf17ee940 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Fri, 15 Feb 2013 19:02:44 +0700 Subject: [PATCH] Revise jQuery plugin API --- package.json | 2 +- readme.md | 5 +++-- ua-parser-js.jquery.json | 2 +- ua-parser.js | 18 +++++++++++++----- ua-parser.min.js | 4 ++-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 4fcf748..409d61e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "title": "UA-Parser.JS", "name": "ua-parser-js", - "version": "0.5.12", + "version": "0.5.13", "author": "Faisal Salman (http://faisalman.com)", "description": "Lightweight JavaScript-based user-agent string parser", "keywords": [ diff --git a/readme.md b/readme.md index cd766f7..4b179cd 100644 --- a/readme.md +++ b/readme.md @@ -104,7 +104,7 @@ console.log(parser.getEngine().name); // "WebKit" ## Using jQuery -If you're using jQuery, `$.ua` object will be created automatically based on container's user-agent. To change different user-agent use `$.setUA(uastring)`. In case you need, `UAParser` is still present in global though. +If you're using jQuery, `$.ua` object will be created automatically based on container's user-agent. Use `$.ua.get()` / `$.ua.set(uastring)` to get/set user-agent. In case you need, `UAParser` constructor is still present in global though. ```js // In browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0': @@ -113,9 +113,10 @@ If you're using jQuery, `$.ua` object will be created automatically based on con console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"} console.log($.ua.os); // {name: "Android", version: "2.3.4"} console.log($.ua.os.name); // "Android" +console.log($.ua.get()); // "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0" // reset to custom user-agent -$.setUA('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13'); +$.ua.set('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13'); // Test again console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"} diff --git a/ua-parser-js.jquery.json b/ua-parser-js.jquery.json index c4afa7d..bbbbd86 100644 --- a/ua-parser-js.jquery.json +++ b/ua-parser-js.jquery.json @@ -1,7 +1,7 @@ { "title": "UA-Parser.JS", "name": "ua-parser-js", - "version": "0.5.12", + "version": "0.5.13", "description": "Lightweight JavaScript-based user-agent string parser", "keywords": [ "user-agent", diff --git a/ua-parser.js b/ua-parser.js index 3e6d8f3..e32dc0d 100644 --- a/ua-parser.js +++ b/ua-parser.js @@ -1,4 +1,4 @@ -// UA-Parser.JS v0.5.12 +// UA-Parser.JS v0.5.13 // Lightweight JavaScript-based User-Agent string parser // https://github.com/faisalman/ua-parser-js // @@ -427,10 +427,18 @@ global.UAParser = UAParser; // jQuery specific if (typeof global.jQuery !== UNDEF) { - global.jQuery.ua = new UAParser().getResult(); - global.jQuery.setUA = function (uastring) { - global.jQuery.ua = new UAParser(uastring).getResult(); - } + var parser = new UAParser(); + global.jQuery.ua = parser.getResult(); + global.jQuery.ua.get = function() { + return parser.getUA(); + }; + global.jQuery.ua.set = function(uastring) { + parser.setUA(uastring); + var result = parser.getResult(); + for (var prop in result) { + global.jQuery.ua[prop] = result[prop]; + } + }; } } })(this); diff --git a/ua-parser.min.js b/ua-parser.min.js index 93fc9db..71e0a9a 100644 --- a/ua-parser.min.js +++ b/ua-parser.min.js @@ -1,7 +1,7 @@ -// UA-Parser.JS v0.5.12 +// UA-Parser.JS v0.5.13 // Lightweight JavaScript-based User-Agent string parser // https://github.com/faisalman/ua-parser-js // // Copyright © 2012-2013 Faisalman // Dual licensed under GPLv2 & MIT -(function(e,t){"use strict";var n="",r="function",i="undefined",s="object",o="major",u="model",a="name",f="type",l="vendor",c="version",h="console",p="mobile",d="tablet",v={regex:function(){var e,o,u,a,f,l,c=arguments;for(o=0;o0){for(var o=0;o0){for(var o=0;o