tauri.js 9.0 KB

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