core.js 7.3 KB

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