tauri.js 8.9 KB

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