diff --git a/README.md b/README.md index e9d1d93..4a3bcf2 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,18 @@ The report object has the following keys and value types. "version": (string) }, "ip": (string), + "country": { + "name": (string), + "code": (string), + "city": (string), + "latitude" : (string), + "longitude": (string), + "timezone": (string) + }, "java": { "version": (string) }, - "lang": (array || string), + "lang": (array), "os": { "name": (string), "version": (string) @@ -75,6 +83,14 @@ The report object has the following keys and value types. "websockets": (boolean) } +## TypeScript Usage + + import 'browser-report'; + + window.browserReport((error, result) => { + console.log(result.ip + ' ' + result.browser.name); + }); + # Coverage The goal is to correctly identify all browsers included on caniuse.com according to their [browser usage table][3]. As of January 18, 2017, their browser usage table accounts for 98.19% of global usage based on data from [StatCounter GlobalStats][4]. @@ -105,6 +121,10 @@ In chronological order (oldest first); not in order of priority. #Change Log +*2.2.9 – June 27, 2017* +* added extended country info from freegeoip +* added typescript definition + *2.2.8 – January 18, 2017* * added test cases for detecting Opera Mobile @@ -183,7 +203,7 @@ In chronological order (oldest first); not in order of priority. * the previous synchronous function can still be found in `browserReportSync()` * the remote client IP address is now reported from ipify.org * a random UUID is now generated for cookie test instead of using the same UUID every time -* report now includes the client's preferred language(s) for displaying pages +* report now includes the client's preferred languages for displaying pages * report now includes a timestamp with the client's locale date, time and time zone *1.0.0 — September 11, 2015* diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..c5852aa --- /dev/null +++ b/index.d.ts @@ -0,0 +1,56 @@ +// Type definitions for browser-report v2.2.9 +// Project: https://github.com/JTOne123/browser-report +// Definitions by: Paul Datsiuk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface Window { + browserReport(result: (error: ErrorEvent, report: ReportResult) => any): void; + browserReportSync(): ReportResult; +} + +interface ReportResult { + "browser": { + "name": string, + "version": string + }, + "cookies": boolean, + "flash": { + "version": string + }, + "ip": string, + "country": { + "name": string, + "code": string, + "city": string, + "latitude" : string, + "longitude": string, + "timezone": string + }, + "java": { + "version": string + }, + "lang": Array, + "os": { + "name": string, + "version": string + }, + "screen": { + "colors": number, + "dppx": number, + "height": number, + "width": number + }, + "scripts": boolean, + "timestamp": string, + "userAgent": string, + "viewport": { + "height": number, + "layout": { + "height": number, + "width": number + } + "width": number, + "zoom": number + } + "websockets": boolean +} \ No newline at end of file diff --git a/index.js b/index.js index 3326cc5..f49c741 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,14 @@ "version": null }, "ip": null, + "country": { + "name": null, + "code": null, + "city": null, + "latitude" : null, + "longitude": null, + "timezone": null + }, "java": { "version": null }, @@ -459,8 +467,8 @@ report.websockets = !!window.WebSocket; - // preferred language(s) for displaying pages - report.lang = navigator.languages || navigator.language; + // preferred languages for displaying pages + report.lang = navigator.languages || [navigator.language]; // local date, time, and time zone @@ -473,7 +481,7 @@ /* * asynchronous version includes the remote client IP address - * uses ipify.org API + * uses freegeoip.net API */ window.browserReport = window.browserReport || function (callback) { var report, newScriptTag, existingScriptTag; @@ -481,15 +489,21 @@ report = extractDataFromClient(); // use ipify.org to get the remote client ip address - // define function to handle data from ipify.org - window.getip = window.getip || function (data) { - report.ip = data.ip; + // define function to handle data from freegeoip.net + window.geoip = window.geoip || function (geoip) { + report.ip = geoip.ip; + report.country.name = geoip.country_name; + report.country.code = geoip.country_code; + report.country.city = geoip.city; + report.country.latitude = geoip.latitude; + report.country.longitude = geoip.longitude; + report.country.timezone = geoip.time_zone; callback(null, report); }; - // inject script tag get JSONP response from ipify.org + // inject script tag get JSONP response from freegeoip.net newScriptTag = document.createElement("script"); - newScriptTag.src = "https://api.ipify.org?format=jsonp&callback=getip"; + newScriptTag.src = "https://freegeoip.net/json/?callback=geoip"; existingScriptTag = document.getElementsByTagName("script")[0]; existingScriptTag.parentNode.insertBefore(newScriptTag, existingScriptTag); diff --git a/sample.html b/sample.html index 306bd54..59006d2 100644 --- a/sample.html +++ b/sample.html @@ -118,6 +118,14 @@

{{screen.colors}} + + + Country info + + + {{country.name}} {{country.city}} + +

Your full user agent string is: