core.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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: 'manage',
  190. data: {
  191. cmd: {
  192. type: e.detail === 2 ? '__toggleMaximize' : 'startDragging'
  193. }
  194. }
  195. }
  196. },
  197. _KEY_VALUE_
  198. )
  199. }
  200. })
  201. window.__TAURI__._invoke(
  202. 'tauri',
  203. {
  204. __tauriModule: 'Event',
  205. message: {
  206. cmd: 'listen',
  207. event: 'tauri://window-created',
  208. handler: window.__TAURI__.transformCallback(function (event) {
  209. if (event.payload) {
  210. var windowLabel = event.payload.label
  211. window.__TAURI__.__windows.push({ label: windowLabel })
  212. }
  213. })
  214. }
  215. },
  216. _KEY_VALUE_
  217. )
  218. let permissionSettable = false
  219. let permissionValue = 'default'
  220. function isPermissionGranted() {
  221. if (window.Notification.permission !== 'default') {
  222. return Promise.resolve(window.Notification.permission === 'granted')
  223. }
  224. return window.__TAURI__._invoke(
  225. 'tauri',
  226. {
  227. __tauriModule: 'Notification',
  228. message: {
  229. cmd: 'isNotificationPermissionGranted'
  230. }
  231. },
  232. _KEY_VALUE_
  233. )
  234. }
  235. function setNotificationPermission(value) {
  236. permissionSettable = true
  237. window.Notification.permission = value
  238. permissionSettable = false
  239. }
  240. function requestPermission() {
  241. return window.__TAURI__
  242. ._invoke(
  243. 'tauri',
  244. {
  245. __tauriModule: 'Notification',
  246. message: {
  247. cmd: 'requestNotificationPermission'
  248. }
  249. },
  250. _KEY_VALUE_
  251. )
  252. .then(function (permission) {
  253. setNotificationPermission(permission)
  254. return permission
  255. })
  256. }
  257. function sendNotification(options) {
  258. if (typeof options === 'object') {
  259. Object.freeze(options)
  260. }
  261. isPermissionGranted().then(function (permission) {
  262. if (permission) {
  263. return window.__TAURI__._invoke(
  264. 'tauri',
  265. {
  266. __tauriModule: 'Notification',
  267. message: {
  268. cmd: 'notification',
  269. options:
  270. typeof options === 'string'
  271. ? {
  272. title: options
  273. }
  274. : options
  275. }
  276. },
  277. _KEY_VALUE_
  278. )
  279. }
  280. })
  281. }
  282. window.Notification = function (title, options) {
  283. var opts = options || {}
  284. sendNotification(
  285. Object.assign(opts, {
  286. title: title
  287. })
  288. )
  289. }
  290. window.Notification.requestPermission = requestPermission
  291. Object.defineProperty(window.Notification, 'permission', {
  292. enumerable: true,
  293. get: function () {
  294. return permissionValue
  295. },
  296. set: function (v) {
  297. if (!permissionSettable) {
  298. throw new Error('Readonly property')
  299. }
  300. permissionValue = v
  301. }
  302. })
  303. isPermissionGranted().then(function (response) {
  304. if (response === null) {
  305. setNotificationPermission('default')
  306. } else {
  307. setNotificationPermission(response ? 'granted' : 'denied')
  308. }
  309. })
  310. window.alert = function (message) {
  311. window.__TAURI__._invoke(
  312. 'tauri',
  313. {
  314. __tauriModule: 'Dialog',
  315. message: {
  316. cmd: 'messageDialog',
  317. message: message
  318. }
  319. },
  320. _KEY_VALUE_
  321. )
  322. }
  323. window.confirm = function (message) {
  324. return window.__TAURI__._invoke(
  325. 'tauri',
  326. {
  327. __tauriModule: 'Dialog',
  328. message: {
  329. cmd: 'askDialog',
  330. message: message
  331. }
  332. },
  333. _KEY_VALUE_
  334. )
  335. }
  336. // window.print works on Linux/Windows; need to use the API on macOS
  337. if (navigator.userAgent.includes('Mac')) {
  338. window.print = function () {
  339. return window.__TAURI__._invoke(
  340. 'tauri',
  341. {
  342. __tauriModule: 'Window',
  343. message: {
  344. cmd: 'manage',
  345. data: {
  346. cmd: {
  347. type: 'print'
  348. }
  349. }
  350. }
  351. },
  352. _KEY_VALUE_
  353. )
  354. }
  355. }
  356. })()