core.js 8.8 KB

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