tauri.js 9.0 KB

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