core.js 8.7 KB

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