core.js 7.4 KB

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