core.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. // polyfills
  5. if (!String.prototype.startsWith) {
  6. String.prototype.startsWith = function (searchString, position) {
  7. position = position || 0;
  8. return this.substr(position, searchString.length) === searchString;
  9. };
  10. }
  11. (function () {
  12. function uid() {
  13. const length = new Int8Array(1)
  14. window.crypto.getRandomValues(length)
  15. const array = new Uint8Array(Math.max(16, Math.abs(length[0])))
  16. window.crypto.getRandomValues(array)
  17. return array.join('')
  18. }
  19. function ownKeys(object, enumerableOnly) {
  20. var keys = Object.keys(object);
  21. if (Object.getOwnPropertySymbols) {
  22. var symbols = Object.getOwnPropertySymbols(object);
  23. if (enumerableOnly)
  24. symbols = symbols.filter(function (sym) {
  25. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  26. });
  27. keys.push.apply(keys, symbols);
  28. }
  29. return keys;
  30. }
  31. function _objectSpread(target) {
  32. for (var i = 1; i < arguments.length; i++) {
  33. var source = arguments[i] != null ? arguments[i] : {};
  34. if (i % 2) {
  35. ownKeys(source, true).forEach(function (key) {
  36. _defineProperty(target, key, source[key]);
  37. });
  38. } else if (Object.getOwnPropertyDescriptors) {
  39. Object.defineProperties(
  40. target,
  41. Object.getOwnPropertyDescriptors(source)
  42. );
  43. } else {
  44. ownKeys(source).forEach(function (key) {
  45. Object.defineProperty(
  46. target,
  47. key,
  48. Object.getOwnPropertyDescriptor(source, key)
  49. );
  50. });
  51. }
  52. }
  53. return target;
  54. }
  55. function _defineProperty(obj, key, value) {
  56. if (key in obj) {
  57. Object.defineProperty(obj, key, {
  58. value: value,
  59. enumerable: true,
  60. configurable: true,
  61. writable: true,
  62. });
  63. } else {
  64. obj[key] = value;
  65. }
  66. return obj;
  67. }
  68. if (!window.__TAURI__) {
  69. window.__TAURI__ = {};
  70. }
  71. window.__TAURI__.transformCallback = function transformCallback(
  72. callback,
  73. once
  74. ) {
  75. var identifier = uid();
  76. window[identifier] = function (result) {
  77. if (once) {
  78. delete window[identifier];
  79. }
  80. return callback && callback(result);
  81. };
  82. return identifier;
  83. };
  84. window.__TAURI__.invoke = function invoke(cmd, args = {}) {
  85. var _this = this;
  86. return new Promise(function (resolve, reject) {
  87. var callback = _this.transformCallback(function (r) {
  88. resolve(r);
  89. delete window[error];
  90. }, true);
  91. var error = _this.transformCallback(function (e) {
  92. reject(e);
  93. delete window[callback];
  94. }, true);
  95. if (typeof cmd === "string") {
  96. args.cmd = cmd;
  97. } else if (typeof cmd === "object") {
  98. args = cmd;
  99. } else {
  100. return reject(new Error("Invalid argument type."));
  101. }
  102. if (window.rpc) {
  103. window.rpc.notify(
  104. cmd,
  105. _objectSpread(
  106. {
  107. callback: callback,
  108. error: error,
  109. },
  110. args
  111. )
  112. );
  113. } else {
  114. window.addEventListener("DOMContentLoaded", function () {
  115. window.rpc.notify(
  116. cmd,
  117. _objectSpread(
  118. {
  119. callback: callback,
  120. error: error,
  121. },
  122. args
  123. )
  124. );
  125. });
  126. }
  127. });
  128. };
  129. // open <a href="..."> links with the Tauri API
  130. function __openLinks() {
  131. document.querySelector("body").addEventListener(
  132. "click",
  133. function (e) {
  134. var target = e.target;
  135. while (target != null) {
  136. if (
  137. target.matches ? target.matches("a") : target.msMatchesSelector("a")
  138. ) {
  139. if (
  140. target.href &&
  141. target.href.startsWith("http") &&
  142. target.target === "_blank"
  143. ) {
  144. window.__TAURI__.invoke('tauri', {
  145. __tauriModule: "Shell",
  146. message: {
  147. cmd: "open",
  148. path: target.href,
  149. },
  150. });
  151. e.preventDefault();
  152. }
  153. break;
  154. }
  155. target = target.parentElement;
  156. }
  157. },
  158. true
  159. );
  160. }
  161. if (
  162. document.readyState === "complete" ||
  163. document.readyState === "interactive"
  164. ) {
  165. __openLinks();
  166. } else {
  167. window.addEventListener(
  168. "DOMContentLoaded",
  169. function () {
  170. __openLinks();
  171. },
  172. true
  173. );
  174. }
  175. window.__TAURI__.invoke('tauri', {
  176. __tauriModule: "Event",
  177. message: {
  178. cmd: "listen",
  179. event: "tauri://window-created",
  180. handler: window.__TAURI__.transformCallback(function (event) {
  181. if (event.payload) {
  182. var windowLabel = event.payload.label;
  183. window.__TAURI__.__windows.push({ label: windowLabel });
  184. }
  185. }),
  186. },
  187. });
  188. let permissionSettable = false;
  189. let permissionValue = "default";
  190. function isPermissionGranted() {
  191. if (window.Notification.permission !== "default") {
  192. return Promise.resolve(window.Notification.permission === "granted");
  193. }
  194. return window.__TAURI__.invoke('tauri', {
  195. __tauriModule: "Notification",
  196. message: {
  197. cmd: "isNotificationPermissionGranted",
  198. },
  199. });
  200. }
  201. function setNotificationPermission(value) {
  202. permissionSettable = true;
  203. window.Notification.permission = value;
  204. permissionSettable = false;
  205. }
  206. function requestPermission() {
  207. return window.__TAURI__
  208. .invoke('tauri', {
  209. __tauriModule: "Notification",
  210. mainThread: true,
  211. message: {
  212. cmd: "requestNotificationPermission",
  213. },
  214. })
  215. .then(function (permission) {
  216. setNotificationPermission(permission);
  217. return permission;
  218. });
  219. }
  220. function sendNotification(options) {
  221. if (typeof options === "object") {
  222. Object.freeze(options);
  223. }
  224. isPermissionGranted().then(function (permission) {
  225. if (permission) {
  226. return window.__TAURI__.invoke('tauri', {
  227. __tauriModule: "Notification",
  228. message: {
  229. cmd: "notification",
  230. options:
  231. typeof options === "string"
  232. ? {
  233. title: options,
  234. }
  235. : options,
  236. },
  237. });
  238. }
  239. });
  240. }
  241. window.Notification = function (title, options) {
  242. var opts = options || {};
  243. sendNotification(
  244. Object.assign(opts, {
  245. title: title,
  246. })
  247. );
  248. };
  249. window.Notification.requestPermission = requestPermission;
  250. Object.defineProperty(window.Notification, "permission", {
  251. enumerable: true,
  252. get: function () {
  253. return permissionValue;
  254. },
  255. set: function (v) {
  256. if (!permissionSettable) {
  257. throw new Error("Readonly property");
  258. }
  259. permissionValue = v;
  260. },
  261. });
  262. isPermissionGranted().then(function (response) {
  263. if (response === null) {
  264. setNotificationPermission("default");
  265. } else {
  266. setNotificationPermission(response ? "granted" : "denied");
  267. }
  268. });
  269. window.alert = function (message) {
  270. window.__TAURI__.invoke('tauri', {
  271. __tauriModule: "Dialog",
  272. mainThread: true,
  273. message: {
  274. cmd: "messageDialog",
  275. message: message,
  276. },
  277. });
  278. };
  279. window.confirm = function (message) {
  280. return window.__TAURI__.invoke('tauri', {
  281. __tauriModule: "Dialog",
  282. mainThread: true,
  283. message: {
  284. cmd: "askDialog",
  285. message: message,
  286. },
  287. });
  288. };
  289. })();