__tauri.js 7.3 KB

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