useragent.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. /*
  33. * I hate doing this, but we need some way to determine if the user is on a Mac
  34. * The reason is that users have different expectations of their key combinations.
  35. *
  36. * Take copy as an example, Mac people expect to use CMD or APPLE + C
  37. * Windows folks expect to use CTRL + C
  38. */
  39. exports.OS = {
  40. LINUX: "LINUX",
  41. MAC: "MAC",
  42. WINDOWS: "WINDOWS"
  43. };
  44. /*
  45. * Return an exports.OS constant
  46. */
  47. exports.getOS = function() {
  48. if (exports.isMac) {
  49. return exports.OS.MAC;
  50. } else if (exports.isLinux) {
  51. return exports.OS.LINUX;
  52. } else {
  53. return exports.OS.WINDOWS;
  54. }
  55. };
  56. // this can be called in non browser environments (e.g. from ace/requirejs/text)
  57. if (typeof navigator != "object")
  58. return;
  59. var os = (navigator.platform.match(/mac|win|linux/i) || ["other"])[0].toLowerCase();
  60. var ua = navigator.userAgent;
  61. // Is the user using a browser that identifies itself as Windows
  62. exports.isWin = (os == "win");
  63. // Is the user using a browser that identifies itself as Mac OS
  64. exports.isMac = (os == "mac");
  65. // Is the user using a browser that identifies itself as Linux
  66. exports.isLinux = (os == "linux");
  67. // Windows Store JavaScript apps (aka Metro apps written in HTML5 and JavaScript) do not use the "Microsoft Internet Explorer" string in their user agent, but "MSAppHost" instead.
  68. exports.isIE =
  69. (navigator.appName == "Microsoft Internet Explorer" || navigator.appName.indexOf("MSAppHost") >= 0)
  70. ? parseFloat((ua.match(/(?:MSIE |Trident\/[0-9]+[\.0-9]+;.*rv:)([0-9]+[\.0-9]+)/)||[])[1])
  71. : parseFloat((ua.match(/(?:Trident\/[0-9]+[\.0-9]+;.*rv:)([0-9]+[\.0-9]+)/)||[])[1]); // for ie
  72. exports.isOldIE = exports.isIE && exports.isIE < 9;
  73. // Is this Firefox or related?
  74. exports.isGecko = exports.isMozilla = (window.Controllers || window.controllers) && window.navigator.product === "Gecko";
  75. // oldGecko == rev < 2.0
  76. exports.isOldGecko = exports.isGecko && parseInt((ua.match(/rv\:(\d+)/)||[])[1], 10) < 4;
  77. // Is this Opera
  78. exports.isOpera = window.opera && Object.prototype.toString.call(window.opera) == "[object Opera]";
  79. // Is the user using a browser that identifies itself as WebKit
  80. exports.isWebKit = parseFloat(ua.split("WebKit/")[1]) || undefined;
  81. exports.isChrome = parseFloat(ua.split(" Chrome/")[1]) || undefined;
  82. exports.isAIR = ua.indexOf("AdobeAIR") >= 0;
  83. exports.isIPad = ua.indexOf("iPad") >= 0;
  84. exports.isTouchPad = ua.indexOf("TouchPad") >= 0;
  85. exports.isChromeOS = ua.indexOf(" CrOS ") >= 0;
  86. });