tauri.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. /**
  10. * @module tauri
  11. * @description This API interface makes powerful interactions available
  12. * to be run on client side applications. They are opt-in features, and
  13. * must be enabled in tauri.conf.js
  14. *
  15. * Each binding MUST provide these interfaces in order to be compliant,
  16. * and also whitelist them based upon the developer's settings.
  17. */
  18. function s4() {
  19. return Math.floor((1 + Math.random()) * 0x10000)
  20. .toString(16)
  21. .substring(1)
  22. }
  23. var uid = function () {
  24. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  25. s4() + '-' + s4() + s4() + s4()
  26. }
  27. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  28. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  29. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  30. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  31. <% if (ctx.dev) { %>
  32. /**
  33. * @name __reject
  34. * @description is a private promise used to deflect un-whitelisted tauri API calls
  35. * Its only purpose is to maintain thenable structure in client code without
  36. * breaking the application
  37. * * @type {Promise<any>}
  38. * @private
  39. */
  40. <% } %>
  41. var __reject = new Promise(function (reject) {
  42. reject;
  43. });
  44. window.tauri = {
  45. <% if (ctx.dev) { %>
  46. /**
  47. * @name invoke
  48. * @description Calls a Tauri Core feature, such as setTitle
  49. * @param {Object} args
  50. */
  51. <% } %>
  52. invoke: function invoke(args) {
  53. window.external.invoke(JSON.stringify(args));
  54. },
  55. <% if (ctx.dev) { %>
  56. /**
  57. * @name listen
  58. * @description Add an event listener to Tauri backend
  59. * @param {String} event
  60. * @param {Function} handler
  61. * @param {Boolean} once
  62. */
  63. <% } %>
  64. listen: function listen(event, handler) {
  65. var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  66. this.invoke({
  67. cmd: 'listen',
  68. event: event,
  69. handler: window.tauri.transformCallback(handler, once),
  70. once: once
  71. });
  72. },
  73. <% if (ctx.dev) { %>
  74. /**
  75. * @name emit
  76. * @description Emits an evt to the Tauri back end
  77. * @param {String} evt
  78. * @param {Object} payload
  79. */
  80. <% } %>
  81. emit: function emit(evt, payload) {
  82. this.invoke({
  83. cmd: 'emit',
  84. event: evt,
  85. payload: payload || ''
  86. });
  87. },
  88. <% if (ctx.dev) { %>
  89. /**
  90. * @name transformCallback
  91. * @description Registers a callback with a uid
  92. * @param {Function} callback
  93. * @param {Boolean} once
  94. * @returns {*}
  95. */
  96. <% } %>
  97. transformCallback: function transformCallback(callback) {
  98. var once = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  99. var identifier = Object.freeze(uid());
  100. window[identifier] = function (result) {
  101. if (once) {
  102. delete window[identifier];
  103. }
  104. return callback && callback(result);
  105. };
  106. return identifier;
  107. },
  108. <% if (ctx.dev) { %>
  109. /**
  110. * @name promisified
  111. * @description Turns a request into a chainable promise
  112. * @param {Object} args
  113. * @returns {Promise<any>}
  114. */
  115. <% } %>
  116. promisified: function promisified(args) {
  117. var _this = this;
  118. return new Promise(function (resolve, reject) {
  119. _this.invoke(_objectSpread({
  120. callback: _this.transformCallback(resolve),
  121. error: _this.transformCallback(reject)
  122. }, args));
  123. });
  124. },
  125. <% if (ctx.dev) { %>
  126. /**
  127. * @name readTextFile
  128. * @description Accesses a non-binary file on the user's filesystem
  129. * and returns the content. Permissions based on the app's PID owner
  130. * @param {String} path
  131. * @returns {*|Promise<any>|Promise}
  132. */
  133. <% } %>
  134. readTextFile: function readTextFile(path) {
  135. <% if (tauri.whitelist.readTextFile === true || tauri.whitelist.all === true) { %>
  136. Object.freeze(path);
  137. return this.promisified({
  138. cmd: 'readTextFile',
  139. path: path
  140. });
  141. <% } else { %>
  142. <% if (ctx.dev) { %>
  143. __whitelistWarning('readTextFile')
  144. <% } %>
  145. return __reject
  146. <% } %>
  147. },
  148. <% if (ctx.dev) { %>
  149. /**
  150. * @name readBinaryFile
  151. * @description Accesses a binary file on the user's filesystem
  152. * and returns the content. Permissions based on the app's PID owner
  153. * @param {String} path
  154. * @returns {*|Promise<any>|Promise}
  155. */
  156. <% } %>
  157. readBinaryFile: function readBinaryFile(path) {
  158. <% if (tauri.whitelist.readBinaryFile === true || tauri.whitelist.all === true) { %>
  159. Object.freeze(path);
  160. return this.promisified({
  161. cmd: 'readBinaryFile',
  162. path: path
  163. });
  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: function writeFile(cfg) {
  182. <% if (tauri.whitelist.writeFile === true || tauri.whitelist.all === true) { %>
  183. Object.freeze(cfg);
  184. this.invoke({
  185. cmd: 'writeFile',
  186. file: cfg.file,
  187. contents: cfg.contents
  188. });
  189. <% } else { %>
  190. <% if (ctx.dev) { %>
  191. __whitelistWarning('writeFile')
  192. <% } %>
  193. return __reject
  194. <% } %>
  195. },
  196. <% if (ctx.dev) { %>
  197. /**
  198. * @name listFiles
  199. * @description Get the files in a path.
  200. * Permissions based on the app's PID owner
  201. * @param {String} path
  202. * @returns {*|Promise<any>|Promise}
  203. */
  204. <% } %>
  205. listFiles: function listFiles(path) {
  206. <% if (tauri.whitelist.listFiles === true || tauri.whitelist.all === true) { %>
  207. Object.freeze(path);
  208. return this.promisified({
  209. cmd: 'listFiles',
  210. path: path
  211. });
  212. <% } else { %>
  213. <% if (ctx.dev) { %>
  214. __whitelistWarning('listDirs')
  215. <% } %>
  216. return __reject
  217. <% } %>
  218. },
  219. <% if (ctx.dev) { %>
  220. /**
  221. * @name listDirs
  222. * @description Get the directories in a path.
  223. * Permissions based on the app's PID owner
  224. * @param {String} path
  225. * @returns {*|Promise<any>|Promise}
  226. */
  227. <% } %>
  228. listDirs: function listDirs(path) {
  229. <% if (tauri.whitelist.listDirs === true || tauri.whitelist.all === true) { %>
  230. Object.freeze(path);
  231. return this.promisified({
  232. cmd: 'listDirs',
  233. path: path
  234. });
  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. setTitle: function setTitle(title) {
  250. <% if (tauri.whitelist.setTitle === true || tauri.whitelist.all === true) { %>
  251. Object.freeze(title);
  252. this.invoke({
  253. cmd: 'setTitle',
  254. title: title
  255. });
  256. <% } else { %>
  257. <% if (ctx.dev) { %>
  258. __whitelistWarning('setTitle')
  259. <% } %>
  260. return __reject
  261. <% } %>
  262. },
  263. <% if (ctx.dev) { %>
  264. /**
  265. * @name open
  266. * @description Open an URI
  267. * @param {String} uri
  268. */
  269. <% } %>
  270. open: function open(uri) {
  271. <% if (tauri.whitelist.open === true || tauri.whitelist.all === true) { %>
  272. Object.freeze(uri);
  273. this.invoke({
  274. cmd: 'open',
  275. uri: uri
  276. });
  277. <% } else { %>
  278. <% if (ctx.dev) { %>
  279. __whitelistWarning('open')
  280. <% } %>
  281. return __reject
  282. <% } %>
  283. },
  284. <% if (ctx.dev) { %>
  285. /**
  286. * @name execute
  287. * @description Execute a program with arguments.
  288. * Permissions based on the app's PID owner
  289. * @param {String} command
  290. * @param {String|Array} args
  291. * @returns {*|Promise<any>|Promise}
  292. */
  293. <% } %>
  294. execute: function execute(command, args) {
  295. <% if (tauri.whitelist.execute === true || tauri.whitelist.all === true) { %>
  296. Object.freeze(command);
  297. if (typeof args === 'string' || _typeof(args) === 'object') {
  298. Object.freeze(args);
  299. }
  300. return this.promisified({
  301. cmd: 'execute',
  302. command: command,
  303. args: typeof args === 'string' ? [args] : args
  304. });
  305. <% } else { %>
  306. <% if (ctx.dev) { %>
  307. __whitelistWarning('execute')
  308. <% } %>
  309. return __reject
  310. <% } %>
  311. },
  312. bridge: function bridge(command, payload) {
  313. <% if (tauri.whitelist.bridge === true || tauri.whitelist.all === true) { %>
  314. Object.freeze(command);
  315. if (typeof payload === 'string' || _typeof(payload) === 'object') {
  316. Object.freeze(payload);
  317. }
  318. return this.promisified({
  319. cmd: 'bridge',
  320. command: command,
  321. payload: _typeof(payload) === 'object' ? [payload] : payload
  322. });
  323. <% } else { %>
  324. <% if (ctx.dev) { %>
  325. __whitelistWarning('bridge')
  326. <% } %>
  327. return __reject
  328. <% } %>
  329. },
  330. loadAsset: function loadAsset(assetName, assetType) {
  331. return this.promisified({
  332. cmd: 'loadAsset',
  333. asset: assetName,
  334. asset_type: assetType || 'unknown'
  335. })
  336. }
  337. };
  338. // init tauri API
  339. try {
  340. window.tauri.invoke({
  341. cmd: 'init'
  342. })
  343. } catch (e) {
  344. window.addEventListener('DOMContentLoaded', function () {
  345. window.tauri.invoke({
  346. cmd: 'init'
  347. })
  348. }, true)
  349. }
  350. if (window.onTauriInit !== void 0) {
  351. window.onTauriInit()
  352. }
  353. document.addEventListener('error', function (e) {
  354. var target = e.target
  355. while (target != null) {
  356. if (target.matches ? target.matches('img') : target.msMatchesSelector('img')) {
  357. window.tauri.loadAsset(target.src, 'image')
  358. .then(img => {
  359. target.src = img
  360. })
  361. break
  362. }
  363. target = target.parentElement
  364. }
  365. }, true)
  366. window.addEventListener('DOMContentLoaded', function () {
  367. // open <a href="..."> links with the Tauri API
  368. document.querySelector('body').addEventListener('click', function (e) {
  369. var target = e.target
  370. while (target != null) {
  371. if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
  372. window.tauri.open(target.href)
  373. break
  374. }
  375. target = target.parentElement
  376. }
  377. }, true)
  378. }, true)