tauri.esm.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* eslint-disable */
  2. /**
  3. * * THIS FILE IS GENERATED AUTOMATICALLY.
  4. * DO NOT EDIT.
  5. *
  6. * Please whitelist these API functions in tauri.conf.js
  7. *
  8. **/
  9. // open <a href="..."> links with the Tauri API
  10. /**
  11. * @module tauri
  12. * @description This API interface makes powerful interactions available
  13. * to be run on client side applications. They are opt-in features, and
  14. * must be enabled in tauri.conf.js
  15. *
  16. * Each binding MUST provide these interfaces in order to be compliant,
  17. * and also whitelist them based upon the developer's settings.
  18. */
  19. function s4() {
  20. return Math.floor((1 + Math.random()) * 0x10000)
  21. .toString(16)
  22. .substring(1)
  23. }
  24. const uid = function () {
  25. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  26. s4() + '-' + s4() + s4() + s4()
  27. }
  28. <% if (ctx.dev) { %>
  29. /**
  30. * @name __whitelistWarning
  31. * @description Present a stylish warning to the developer that their API
  32. * call has not been whitelisted in tauri.conf.js
  33. * @param {String} func - function name to warn
  34. * @private
  35. */
  36. const __whitelistWarning = function (func) {
  37. console.warn('%c[Tauri] Danger \ntauri.' + func + ' not whitelisted 💣\n%c\nAdd to tauri.conf.js: \n\ntauri: \n whitelist: { \n ' + func + ': true \n\nReference: https://tauri-apps.org/docs/api#' + func , 'background: red; color: white; font-weight: 800; padding: 2px; font-size:1.5em', ' ')
  38. }
  39. <% } %>
  40. <% if (ctx.dev) { %>
  41. /**
  42. * @name __reject
  43. * @description is a private promise used to deflect un-whitelisted tauri API calls
  44. * Its only purpose is to maintain thenable structure in client code without
  45. * breaking the application
  46. * * @type {Promise<any>}
  47. * @private
  48. */
  49. <% } %>
  50. const __reject = new Promise((reject) => { reject })
  51. window.tauri = {
  52. <% if (ctx.dev) { %>
  53. /**
  54. * @name invoke
  55. * @description Calls a Tauri Core feature, such as setTitle
  56. * @param {Object} args
  57. */
  58. <% } %>
  59. invoke (args) {
  60. Object.freeze(args)
  61. window.external.invoke(JSON.stringify(args))
  62. },
  63. <% if (ctx.dev) { %>
  64. /**
  65. * @name listen
  66. * @description Add an event listener to Tauri backend
  67. * @param {String} event
  68. * @param {Function} handler
  69. * @param {Boolean} once
  70. */
  71. <% } %>
  72. listen (event, handler, once = false) {
  73. this.invoke({
  74. cmd: 'listen',
  75. event,
  76. handler: this.transformCallback(handler, once),
  77. once
  78. })
  79. },
  80. <% if (ctx.dev) { %>
  81. /**
  82. * @name emit
  83. * @description Emits an evt to the Tauri back end
  84. * @param {String} evt
  85. * @param {Object} payload
  86. */
  87. <% } %>
  88. emit (evt, payload) {
  89. this.invoke({
  90. cmd: 'emit',
  91. event: evt,
  92. payload
  93. })
  94. },
  95. <% if (ctx.dev) { %>
  96. /**
  97. * @name transformCallback
  98. * @description Registers a callback with a uid
  99. * @param {Function} callback
  100. * @param {Boolean} once
  101. * @returns {*}
  102. */
  103. <% } %>
  104. transformCallback (callback, once = true) {
  105. const identifier = Object.freeze(uid())
  106. window[identifier] = (result) => {
  107. if (once) {
  108. delete window[identifier]
  109. }
  110. return callback && callback(result)
  111. }
  112. return identifier
  113. },
  114. <% if (ctx.dev) { %>
  115. /**
  116. * @name promisified
  117. * @description Turns a request into a chainable promise
  118. * @param {Object} args
  119. * @returns {Promise<any>}
  120. */
  121. <% } %>
  122. promisified (args) {
  123. return new Promise((resolve, reject) => {
  124. this.invoke({
  125. callback: this.transformCallback(resolve),
  126. error: this.transformCallback(reject),
  127. ...args
  128. })
  129. })
  130. },
  131. <% if (ctx.dev) { %>
  132. /**
  133. * @name readTextFile
  134. * @description Accesses a non-binary file on the user's filesystem
  135. * and returns the content. Permissions based on the app's PID owner
  136. * @param {String} path
  137. * @returns {*|Promise<any>|Promise}
  138. */
  139. <% } %>
  140. readTextFile (path) {
  141. <% if (tauri.whitelist.readTextFile === true || tauri.whitelist.all === true) { %>
  142. Object.freeze(path)
  143. return this.promisified({ cmd: 'readTextFile', path })
  144. <% } else { %>
  145. <% if (ctx.dev) { %>
  146. __whitelistWarning('readTextFile')
  147. <% } %>
  148. return __reject
  149. <% } %>
  150. },
  151. <% if (ctx.dev) { %>
  152. /**
  153. * @name readBinaryFile
  154. * @description Accesses a binary file on the user's filesystem
  155. * and returns the content. Permissions based on the app's PID owner
  156. * @param {String} path
  157. * @returns {*|Promise<any>|Promise}
  158. */
  159. <% } %>
  160. readBinaryFile (path) {
  161. <% if (tauri.whitelist.readBinaryFile === true || tauri.whitelist.all === true) { %>
  162. Object.freeze(path)
  163. return this.promisified({ cmd: 'readBinaryFile', path })
  164. <% } else { %>
  165. <% if (ctx.dev) { %>
  166. __whitelistWarning('readBinaryFile')
  167. <% } %>
  168. return __reject
  169. <% } %>
  170. },
  171. <% if (ctx.dev) { %>
  172. /**
  173. * @name writeFile
  174. * @description Write a file to the Local Filesystem.
  175. * Permissions based on the app's PID owner
  176. * @param {Object} cfg
  177. * @param {String} cfg.file
  178. * @param {String|Binary} cfg.contents
  179. */
  180. <% } %>
  181. writeFile (cfg) {
  182. <% if (tauri.whitelist.writeFile === true || tauri.whitelist.all === true) { %>
  183. Object.freeze(cfg)
  184. this.invoke({ cmd: 'writeFile', file: cfg.file, contents: cfg.contents })
  185. <% } else { %>
  186. <% if (ctx.dev) { %>
  187. __whitelistWarning('writeFile')
  188. <% } %>
  189. return __reject
  190. <% } %>
  191. },
  192. <% if (ctx.dev) { %>
  193. /**
  194. * @name listFiles
  195. * @description Get the files in a path.
  196. * Permissions based on the app's PID owner
  197. * @param {String} path
  198. * @returns {*|Promise<any>|Promise}
  199. */
  200. <% } %>
  201. listFiles (path) {
  202. <% if (tauri.whitelist.listFiles === true || tauri.whitelist.all === true) { %>
  203. Object.freeze(path)
  204. return this.promisified({ cmd: 'listFiles', path })
  205. <% } else { %>
  206. <% if (ctx.dev) { %>
  207. __whitelistWarning('listFiles')
  208. <% } %>
  209. return __reject
  210. <% } %>
  211. },
  212. <% if (ctx.dev) { %>
  213. /**
  214. * @name listDirs
  215. * @description Get the directories in a path.
  216. * Permissions based on the app's PID owner
  217. * @param {String} path
  218. * @returns {*|Promise<any>|Promise}
  219. */
  220. <% } %>
  221. listDirs (path) {
  222. <% if (tauri.whitelist.listDirs === true || tauri.whitelist.all === true) { %>
  223. Object.freeze(path)
  224. return this.promisified({ cmd: 'listDirs', path })
  225. <% } else { %>
  226. <% if (ctx.dev) { %>
  227. __whitelistWarning('listDirs')
  228. <% } %>
  229. return __reject
  230. <% } %>
  231. },
  232. <% if (ctx.dev) { %>
  233. /**
  234. * @name setTitle
  235. * @description Set the application's title
  236. * @param {String} title
  237. */
  238. <% } %>
  239. setTitle (title) {
  240. <% if (tauri.whitelist.setTitle === true || tauri.whitelist.all === true) { %>
  241. Object.freeze(title)
  242. this.invoke({ cmd: 'setTitle', title })
  243. <% } else { %>
  244. <% if (ctx.dev) { %>
  245. __whitelistWarning('setTitle')
  246. <% } %>
  247. return __reject
  248. <% } %>
  249. },
  250. <% if (ctx.dev) { %>
  251. /**
  252. * @name open
  253. * @description Open an URI
  254. * @param {String} uri
  255. */
  256. <% } %>
  257. open (uri) {
  258. <% if (tauri.whitelist.open === true || tauri.whitelist.all === true) { %>
  259. Object.freeze(uri)
  260. this.invoke({ cmd: 'open', uri })
  261. <% } else { %>
  262. <% if (ctx.dev) { %>
  263. __whitelistWarning('open')
  264. <% } %>
  265. return __reject
  266. <% } %>
  267. },
  268. <% if (ctx.dev) { %>
  269. /**
  270. * @name execute
  271. * @description Execute a program with arguments.
  272. * Permissions based on the app's PID owner
  273. * @param {String} command
  274. * @param {String|Array} args
  275. * @returns {*|Promise<any>|Promise}
  276. */
  277. <% } %>
  278. execute (command, args) {
  279. <% if (tauri.whitelist.execute === true || tauri.whitelist.all === true) { %>
  280. Object.freeze(command)
  281. if (typeof args === 'string' || typeof args === 'object') {
  282. Object.freeze(args)
  283. }
  284. return this.promisified({ cmd: 'execute', command, args: typeof (args) === 'string' ? [args] : args })
  285. <% } else { %>
  286. <% if (ctx.dev) { %>
  287. __whitelistWarning('execute')
  288. <% } %>
  289. return __reject
  290. <% } %>
  291. },
  292. <% if (ctx.dev) { %>
  293. /**
  294. * @name bridge
  295. * @description Securely pass a message to the backend.
  296. * @example
  297. * this.$q.tauri.bridge('QBP/1/ping/client-1', 'pingback')
  298. * @param {String} command - a compressed, slash-delimited and
  299. * versioned API call to the backend.
  300. * @param {String|Object}payload
  301. * @returns {*|Promise<any>|Promise}
  302. */
  303. <% } %>
  304. bridge (command, payload) {
  305. <% if (tauri.whitelist.bridge === true || tauri.whitelist.all === true) { %>
  306. Object.freeze(command)
  307. if (typeof payload === 'string' || typeof payload === 'object') {
  308. Object.freeze(payload)
  309. }
  310. return this.promisified({ cmd: 'bridge', command, payload: typeof (payload) === 'object' ? [payload] : payload })
  311. <% } else { %>
  312. <% if (ctx.dev) { %>
  313. __whitelistWarning('bridge')
  314. <% } %>
  315. return __reject
  316. <% } %>
  317. },
  318. <% if (ctx.dev) { %>
  319. /**
  320. * @name setup
  321. * @description Inform Rust that the webview has initialized and is
  322. * ready for communication
  323. */
  324. <% } %>
  325. setup () {
  326. document.querySelector('body').addEventListener('click', function (e) {
  327. let target = e.target
  328. while (target != null) {
  329. if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
  330. tauri.open(target.href)
  331. break
  332. }
  333. target = target.parentElement
  334. }
  335. }, true)
  336. tauri.invoke({
  337. cmd: 'init'
  338. })
  339. }
  340. }