Browser.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js;
  23. import js.html.Storage;
  24. import js.html.XMLHttpRequest;
  25. class Browser {
  26. /** The global scope typed with fields available only in a worker context. */
  27. public static var self(get, never):js.html.WorkerGlobalScope;
  28. static inline function get_self():js.html.WorkerGlobalScope {
  29. return js.Lib.global;
  30. }
  31. /** The global window object. */
  32. public static var window(get, never):js.html.Window;
  33. extern inline static function get_window()
  34. return js.Syntax.code("window");
  35. /** Shortcut to Window.document. */
  36. public static var document(get, never):js.html.HTMLDocument;
  37. extern inline static function get_document()
  38. return window.document;
  39. /** Shortcut to Window.location. */
  40. public static var location(get, never):js.html.Location;
  41. extern inline static function get_location()
  42. return js.Lib.global.location;
  43. /** Shortcut to Window.navigator. */
  44. public static var navigator(get, never):js.html.Navigator;
  45. extern inline static function get_navigator()
  46. return js.Lib.global.navigator;
  47. /** Shortcut to Window.console. */
  48. public static var console(get, never):js.html.ConsoleInstance;
  49. extern inline static function get_console()
  50. return js.Lib.global.console;
  51. /**
  52. * True if a window object exists, false otherwise.
  53. *
  54. * This can be used to check if the code is being executed in a non-browser
  55. * environment such as node.js.
  56. */
  57. public static var supported(get, never):Bool;
  58. static function get_supported()
  59. return
  60. js.Syntax.typeof(window) != "undefined" &&
  61. js.Syntax.typeof(window.location) != "undefined" &&
  62. js.Syntax.typeof(window.location.protocol) == "string";
  63. /**
  64. * Safely gets the browser's local storage, or returns null if localStorage is unsupported or
  65. * disabled.
  66. */
  67. public static function getLocalStorage():Storage {
  68. try {
  69. var s = window.localStorage;
  70. s.getItem("");
  71. if (s.length == 0) {
  72. var key = "_hx_" + Math.random();
  73. s.setItem(key, key);
  74. s.removeItem(key);
  75. }
  76. return s;
  77. } catch (e:Dynamic) {
  78. return null;
  79. }
  80. }
  81. /**
  82. * Safely gets the browser's session storage, or returns null if sessionStorage is unsupported
  83. * or disabled.
  84. */
  85. public static function getSessionStorage():Storage {
  86. try {
  87. var s = window.sessionStorage;
  88. s.getItem("");
  89. if (s.length == 0) {
  90. var key = "_hx_" + Math.random();
  91. s.setItem(key, key);
  92. s.removeItem(key);
  93. }
  94. return s;
  95. } catch (e:Dynamic) {
  96. return null;
  97. }
  98. }
  99. /**
  100. * Creates an XMLHttpRequest, with a fallback to ActiveXObject for ancient versions of Internet
  101. * Explorer.
  102. */
  103. public static function createXMLHttpRequest():XMLHttpRequest {
  104. if (js.Syntax.code("typeof XMLHttpRequest") != "undefined") {
  105. return new XMLHttpRequest();
  106. }
  107. if (js.Syntax.code("typeof ActiveXObject") != "undefined") {
  108. return js.Syntax.construct("ActiveXObject", "Microsoft.XMLHTTP");
  109. }
  110. throw "Unable to create XMLHttpRequest object.";
  111. }
  112. /**
  113. Display an alert message box containing the given message. See also `Window.alert()`.
  114. **/
  115. public static inline function alert(v:Dynamic) {
  116. window.alert(Std.string(v));
  117. }
  118. }