core.js 7.2 KB

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