__tauri.js 7.5 KB

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