core.js 8.8 KB

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