tauri.js 9.0 KB

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