core.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 = {}, key = null) {
  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. __invokeKey: key || __TAURI_INVOKE_KEY__,
  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. __invokeKey: key || __TAURI_INVOKE_KEY__,
  122. },
  123. args
  124. )
  125. );
  126. });
  127. }
  128. });
  129. };
  130. // open <a href="..."> links with the Tauri API
  131. function __openLinks() {
  132. document.querySelector("body").addEventListener(
  133. "click",
  134. function (e) {
  135. var target = e.target;
  136. while (target != null) {
  137. if (
  138. target.matches ? target.matches("a") : target.msMatchesSelector("a")
  139. ) {
  140. if (
  141. target.href &&
  142. target.href.startsWith("http") &&
  143. target.target === "_blank"
  144. ) {
  145. window.__TAURI__._invoke('tauri', {
  146. __tauriModule: "Shell",
  147. message: {
  148. cmd: "open",
  149. path: target.href,
  150. },
  151. }, _KEY_VALUE_);
  152. e.preventDefault();
  153. }
  154. break;
  155. }
  156. target = target.parentElement;
  157. }
  158. },
  159. true
  160. );
  161. }
  162. if (
  163. document.readyState === "complete" ||
  164. document.readyState === "interactive"
  165. ) {
  166. __openLinks();
  167. } else {
  168. window.addEventListener(
  169. "DOMContentLoaded",
  170. function () {
  171. __openLinks();
  172. },
  173. true
  174. );
  175. }
  176. // drag region
  177. document.addEventListener('mousedown', (e) => {
  178. if (e.target.hasAttribute("data-tauri-drag-region") && e.buttons === 1) {
  179. // start dragging if the element has a `tauri-drag-region` data attribute and maximize on double-clicking it
  180. e.detail === 2
  181. ? window.__TAURI__._invoke(
  182. "tauri",
  183. {
  184. __tauriModule: "Window",
  185. message: {
  186. cmd: "toggleMaximize",
  187. },
  188. },
  189. _KEY_VALUE_
  190. )
  191. : window.__TAURI__._invoke(
  192. "tauri",
  193. {
  194. __tauriModule: "Window",
  195. message: {
  196. cmd: "startDragging",
  197. },
  198. },
  199. _KEY_VALUE_
  200. );
  201. }
  202. })
  203. window.__TAURI__._invoke('tauri', {
  204. __tauriModule: "Event",
  205. message: {
  206. cmd: "listen",
  207. event: "tauri://window-created",
  208. handler: window.__TAURI__.transformCallback(function (event) {
  209. if (event.payload) {
  210. var windowLabel = event.payload.label;
  211. window.__TAURI__.__windows.push({ label: windowLabel });
  212. }
  213. }),
  214. },
  215. }, _KEY_VALUE_);
  216. let permissionSettable = false;
  217. let permissionValue = "default";
  218. function isPermissionGranted() {
  219. if (window.Notification.permission !== "default") {
  220. return Promise.resolve(window.Notification.permission === "granted");
  221. }
  222. return window.__TAURI__._invoke('tauri', {
  223. __tauriModule: "Notification",
  224. message: {
  225. cmd: "isNotificationPermissionGranted",
  226. },
  227. }, _KEY_VALUE_);
  228. }
  229. function setNotificationPermission(value) {
  230. permissionSettable = true;
  231. window.Notification.permission = value;
  232. permissionSettable = false;
  233. }
  234. function requestPermission() {
  235. return window.__TAURI__
  236. .invoke('tauri', {
  237. __tauriModule: "Notification",
  238. message: {
  239. cmd: "requestNotificationPermission",
  240. },
  241. }, _KEY_VALUE_)
  242. .then(function (permission) {
  243. setNotificationPermission(permission);
  244. return permission;
  245. });
  246. }
  247. function sendNotification(options) {
  248. if (typeof options === "object") {
  249. Object.freeze(options);
  250. }
  251. isPermissionGranted().then(function (permission) {
  252. if (permission) {
  253. return window.__TAURI__._invoke('tauri', {
  254. __tauriModule: "Notification",
  255. message: {
  256. cmd: "notification",
  257. options:
  258. typeof options === "string"
  259. ? {
  260. title: options,
  261. }
  262. : options,
  263. },
  264. }, _KEY_VALUE_);
  265. }
  266. });
  267. }
  268. window.Notification = function (title, options) {
  269. var opts = options || {};
  270. sendNotification(
  271. Object.assign(opts, {
  272. title: title,
  273. })
  274. );
  275. };
  276. window.Notification.requestPermission = requestPermission;
  277. Object.defineProperty(window.Notification, "permission", {
  278. enumerable: true,
  279. get: function () {
  280. return permissionValue;
  281. },
  282. set: function (v) {
  283. if (!permissionSettable) {
  284. throw new Error("Readonly property");
  285. }
  286. permissionValue = v;
  287. },
  288. });
  289. isPermissionGranted().then(function (response) {
  290. if (response === null) {
  291. setNotificationPermission("default");
  292. } else {
  293. setNotificationPermission(response ? "granted" : "denied");
  294. }
  295. });
  296. window.alert = function (message) {
  297. window.__TAURI__._invoke('tauri', {
  298. __tauriModule: "Dialog",
  299. message: {
  300. cmd: "messageDialog",
  301. message: message,
  302. },
  303. }, _KEY_VALUE_);
  304. };
  305. window.confirm = function (message) {
  306. return window.__TAURI__._invoke('tauri', {
  307. __tauriModule: "Dialog",
  308. message: {
  309. cmd: "askDialog",
  310. message: message,
  311. },
  312. }, _KEY_VALUE_);
  313. };
  314. // window.print works on Linux/Windows; need to use the API on macOS
  315. if (navigator.userAgent.includes('Mac')) {
  316. window.print = function () {
  317. return window.__TAURI__._invoke('tauri', {
  318. __tauriModule: "Window",
  319. message: {
  320. cmd: "print"
  321. },
  322. }, _KEY_VALUE_);
  323. }
  324. }
  325. })();