core.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. return new Promise(function (resolve, reject) {
  86. var callback = window.__TAURI__.transformCallback(function (r) {
  87. resolve(r);
  88. delete window[error];
  89. }, true);
  90. var error = window.__TAURI__.transformCallback(function (e) {
  91. reject(e);
  92. delete window[callback];
  93. }, true);
  94. if (typeof cmd === "string") {
  95. args.cmd = cmd;
  96. } else if (typeof cmd === "object") {
  97. args = cmd;
  98. } else {
  99. return reject(new Error("Invalid argument type."));
  100. }
  101. if (window.rpc) {
  102. window.rpc.notify(
  103. cmd,
  104. _objectSpread(
  105. {
  106. callback: callback,
  107. error: error,
  108. },
  109. args
  110. )
  111. );
  112. } else {
  113. window.addEventListener("DOMContentLoaded", function () {
  114. window.rpc.notify(
  115. cmd,
  116. _objectSpread(
  117. {
  118. callback: callback,
  119. error: error,
  120. },
  121. args
  122. )
  123. );
  124. });
  125. }
  126. });
  127. };
  128. // open <a href="..."> links with the Tauri API
  129. function __openLinks() {
  130. document.querySelector("body").addEventListener(
  131. "click",
  132. function (e) {
  133. var target = e.target;
  134. while (target != null) {
  135. if (
  136. target.matches ? target.matches("a") : target.msMatchesSelector("a")
  137. ) {
  138. if (
  139. target.href &&
  140. target.href.startsWith("http") &&
  141. target.target === "_blank"
  142. ) {
  143. window.__TAURI__.invoke('tauri', {
  144. __tauriModule: "Shell",
  145. message: {
  146. cmd: "open",
  147. path: target.href,
  148. },
  149. });
  150. e.preventDefault();
  151. }
  152. break;
  153. }
  154. target = target.parentElement;
  155. }
  156. },
  157. true
  158. );
  159. }
  160. if (
  161. document.readyState === "complete" ||
  162. document.readyState === "interactive"
  163. ) {
  164. __openLinks();
  165. } else {
  166. window.addEventListener(
  167. "DOMContentLoaded",
  168. function () {
  169. __openLinks();
  170. },
  171. true
  172. );
  173. }
  174. // drag region
  175. document.addEventListener('mousedown', (e) => {
  176. if (e.target.classList.contains('drag-region') && e.buttons === 1) {
  177. window.__TAURI__.invoke('tauri', {
  178. __tauriModule: "Window",
  179. message: {
  180. cmd: "startDragging",
  181. }
  182. })
  183. }
  184. })
  185. window.__TAURI__.invoke('tauri', {
  186. __tauriModule: "Event",
  187. message: {
  188. cmd: "listen",
  189. event: "tauri://window-created",
  190. handler: window.__TAURI__.transformCallback(function (event) {
  191. if (event.payload) {
  192. var windowLabel = event.payload.label;
  193. window.__TAURI__.__windows.push({ label: windowLabel });
  194. }
  195. }),
  196. },
  197. });
  198. let permissionSettable = false;
  199. let permissionValue = "default";
  200. function isPermissionGranted() {
  201. if (window.Notification.permission !== "default") {
  202. return Promise.resolve(window.Notification.permission === "granted");
  203. }
  204. return window.__TAURI__.invoke('tauri', {
  205. __tauriModule: "Notification",
  206. message: {
  207. cmd: "isNotificationPermissionGranted",
  208. },
  209. });
  210. }
  211. function setNotificationPermission(value) {
  212. permissionSettable = true;
  213. window.Notification.permission = value;
  214. permissionSettable = false;
  215. }
  216. function requestPermission() {
  217. return window.__TAURI__
  218. .invoke('tauri', {
  219. __tauriModule: "Notification",
  220. mainThread: true,
  221. message: {
  222. cmd: "requestNotificationPermission",
  223. },
  224. })
  225. .then(function (permission) {
  226. setNotificationPermission(permission);
  227. return permission;
  228. });
  229. }
  230. function sendNotification(options) {
  231. if (typeof options === "object") {
  232. Object.freeze(options);
  233. }
  234. isPermissionGranted().then(function (permission) {
  235. if (permission) {
  236. return window.__TAURI__.invoke('tauri', {
  237. __tauriModule: "Notification",
  238. message: {
  239. cmd: "notification",
  240. options:
  241. typeof options === "string"
  242. ? {
  243. title: options,
  244. }
  245. : options,
  246. },
  247. });
  248. }
  249. });
  250. }
  251. window.Notification = function (title, options) {
  252. var opts = options || {};
  253. sendNotification(
  254. Object.assign(opts, {
  255. title: title,
  256. })
  257. );
  258. };
  259. window.Notification.requestPermission = requestPermission;
  260. Object.defineProperty(window.Notification, "permission", {
  261. enumerable: true,
  262. get: function () {
  263. return permissionValue;
  264. },
  265. set: function (v) {
  266. if (!permissionSettable) {
  267. throw new Error("Readonly property");
  268. }
  269. permissionValue = v;
  270. },
  271. });
  272. isPermissionGranted().then(function (response) {
  273. if (response === null) {
  274. setNotificationPermission("default");
  275. } else {
  276. setNotificationPermission(response ? "granted" : "denied");
  277. }
  278. });
  279. window.alert = function (message) {
  280. window.__TAURI__.invoke('tauri', {
  281. __tauriModule: "Dialog",
  282. mainThread: true,
  283. message: {
  284. cmd: "messageDialog",
  285. message: message,
  286. },
  287. });
  288. };
  289. window.confirm = function (message) {
  290. return window.__TAURI__.invoke('tauri', {
  291. __tauriModule: "Dialog",
  292. mainThread: true,
  293. message: {
  294. cmd: "askDialog",
  295. message: message,
  296. },
  297. });
  298. };
  299. })();