tauri.js 7.5 KB

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