tauri.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /* eslint-disable */
  2. /**
  3. * * THIS FILE IS GENERATED AUTOMATICALLY.
  4. * DO NOT EDIT.
  5. *
  6. * Please whitelist these API functions in tauri.conf.json
  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.json
  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. // polyfills
  19. if (!String.prototype.startsWith) {
  20. String.prototype.startsWith = function (searchString, position) {
  21. position = position || 0
  22. return this.substr(position, searchString.length) === searchString
  23. }
  24. }
  25. // makes the window.external.invoke API available after window.location.href changes
  26. switch (navigator.platform) {
  27. case "Macintosh":
  28. case "MacPPC":
  29. case "MacIntel":
  30. case "Mac68K":
  31. window.external = this
  32. invoke = function (x) {
  33. webkit.messageHandlers.invoke.postMessage(x);
  34. }
  35. break;
  36. case "Windows":
  37. case "WinCE":
  38. case "Win32":
  39. case "Win64":
  40. break;
  41. default:
  42. window.external = this
  43. invoke = function (x) {
  44. window.webkit.messageHandlers.external.postMessage(x);
  45. }
  46. break;
  47. }
  48. function s4() {
  49. return Math.floor((1 + Math.random()) * 0x10000)
  50. .toString(16)
  51. .substring(1)
  52. }
  53. var uid = function () {
  54. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  55. s4() + '-' + s4() + s4() + s4()
  56. }
  57. 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; }
  58. 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; }
  59. 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; }
  60. 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); }
  61. /**
  62. * @typedef {number} BaseDirectory
  63. */
  64. /**
  65. * @enum {BaseDirectory}
  66. */
  67. var Dir = {
  68. Audio: 1,
  69. Cache: 2,
  70. Config: 3,
  71. Data: 4,
  72. LocalData: 5,
  73. Desktop: 6,
  74. Document: 7,
  75. Download: 8,
  76. Executable: 9,
  77. Font: 10,
  78. Home: 11,
  79. Picture: 12,
  80. Public: 13,
  81. Runtime: 14,
  82. Template: 15,
  83. Video: 16,
  84. Resource: 17,
  85. App: 18
  86. }
  87. function camelToKebab (string) {
  88. return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase()
  89. }
  90. /**
  91. * @name return __whitelistWarning
  92. * @description Present a stylish warning to the developer that their API
  93. * call has not been whitelisted in tauri.conf.json
  94. * @param {String} func - function name to warn
  95. * @private
  96. */
  97. var __whitelistWarning = function (func) {
  98. console.warn('%c[Tauri] Danger \ntauri.' + func + ' not whitelisted 💣\n%c\nAdd to tauri.conf.json: \n\ntauri: \n whitelist: { \n ' + camelToKebab(func) + ': true \n\nReference: https://github.com/tauri-apps/tauri/wiki' + func, 'background: red; color: white; font-weight: 800; padding: 2px; font-size:1.5em', ' ')
  99. return __reject()
  100. }
  101. /**
  102. * @name __reject
  103. * @description generates a promise used to deflect un-whitelisted tauri API calls
  104. * Its only purpose is to maintain thenable structure in client code without
  105. * breaking the application
  106. * * @type {Promise<any>}
  107. * @private
  108. */
  109. var __reject = function () {
  110. return new Promise(function (_, reject) {
  111. reject();
  112. });
  113. }
  114. window.tauri = {
  115. Dir: Dir,
  116. /**
  117. * @name invoke
  118. * @description Calls a Tauri Core feature, such as setTitle
  119. * @param {Object} args
  120. */
  121. invoke: function invoke(args) {
  122. window.external.invoke(JSON.stringify(args));
  123. },
  124. /**
  125. * @name listen
  126. * @description Add an event listener to Tauri backend
  127. * @param {String} event
  128. * @param {Function} handler
  129. * @param {Boolean} once
  130. */
  131. listen: function listen(event, handler) {
  132. var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  133. this.invoke({
  134. cmd: 'listen',
  135. event: event,
  136. handler: window.tauri.transformCallback(handler, once),
  137. once: once
  138. });
  139. },
  140. /**
  141. * @name emit
  142. * @description Emits an evt to the Tauri back end
  143. * @param {String} evt
  144. * @param {Object} payload
  145. */
  146. emit: function emit(evt, payload) {
  147. this.invoke({
  148. cmd: 'emit',
  149. event: evt,
  150. payload: payload
  151. });
  152. },
  153. /**
  154. * @name transformCallback
  155. * @description Registers a callback with a uid
  156. * @param {Function} callback
  157. * @param {Boolean} once
  158. * @returns {*}
  159. */
  160. transformCallback: function transformCallback(callback) {
  161. var once = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  162. var identifier = uid();
  163. window[identifier] = function (result) {
  164. if (once) {
  165. delete window[identifier];
  166. }
  167. return callback && callback(result);
  168. };
  169. return identifier;
  170. },
  171. /**
  172. * @name promisified
  173. * @description Turns a request into a chainable promise
  174. * @param {Object} args
  175. * @returns {Promise<any>}
  176. */
  177. promisified: function promisified(args) {
  178. var _this = this;
  179. return new Promise(function (resolve, reject) {
  180. _this.invoke(_objectSpread({
  181. callback: _this.transformCallback(resolve),
  182. error: _this.transformCallback(reject)
  183. }, args));
  184. });
  185. },
  186. /**
  187. * @name readTextFile
  188. * @description Accesses a non-binary file on the user's filesystem
  189. * and returns the content. Permissions based on the app's PID owner
  190. * @param {String} path
  191. * @param {Object} [options]
  192. * @param {BaseDirectory} [options.dir]
  193. * @returns {*|Promise<any>|Promise}
  194. */
  195. readTextFile: function readTextFile(path, options) {
  196. return this.promisified({
  197. cmd: 'readTextFile',
  198. path: path,
  199. options: options
  200. });
  201. },
  202. /**
  203. * @name readBinaryFile
  204. * @description Accesses a binary file on the user's filesystem
  205. * and returns the content. Permissions based on the app's PID owner
  206. * @param {String} path
  207. * @param {Object} [options]
  208. * @param {BaseDirectory} [options.dir]
  209. * @returns {*|Promise<any>|Promise}
  210. */
  211. readBinaryFile: function readBinaryFile(path, options) {
  212. return this.promisified({
  213. cmd: 'readBinaryFile',
  214. path: path,
  215. options: options
  216. });
  217. },
  218. /**
  219. * @name writeFile
  220. * @description Write a file to the Local Filesystem.
  221. * Permissions based on the app's PID owner
  222. * @param {Object} cfg
  223. * @param {String} cfg.file
  224. * @param {String|Binary} cfg.contents
  225. * @param {Object} [options]
  226. * @param {BaseDirectory} [options.dir]
  227. */
  228. writeFile: function writeFile(cfg, options) {
  229. if (_typeof(cfg) === 'object') {
  230. Object.freeze(cfg);
  231. }
  232. return this.promisified({
  233. cmd: 'writeFile',
  234. file: cfg.file,
  235. contents: cfg.contents,
  236. options: options
  237. });
  238. },
  239. /**
  240. * @name readDir
  241. * @description Reads a directory
  242. * Permissions based on the app's PID owner
  243. * @param {String} path
  244. * @param {Object} [options]
  245. * @param {Boolean} [options.recursive]
  246. * @param {BaseDirectory} [options.dir]
  247. * @returns {*|Promise<any>|Promise}
  248. */
  249. readDir: function readDir(path, options) {
  250. return this.promisified({
  251. cmd: 'readDir',
  252. path: path,
  253. options: options
  254. });
  255. },
  256. /**
  257. * @name createDir
  258. * @description Creates a directory
  259. * Permissions based on the app's PID owner
  260. * @param {String} path
  261. * @param {Object} [options]
  262. * @param {Boolean} [options.recursive]
  263. * @param {BaseDirectory} [options.dir]
  264. * @returns {*|Promise<any>|Promise}
  265. */
  266. createDir: function createDir(path, options) {
  267. return this.promisified({
  268. cmd: 'createDir',
  269. path: path,
  270. options: options
  271. });
  272. },
  273. /**
  274. * @name removeDir
  275. * @description Removes a directory
  276. * Permissions based on the app's PID owner
  277. * @param {String} path
  278. * @param {Object} [options]
  279. * @param {Boolean} [options.recursive]
  280. * @param {BaseDirectory} [options.dir]
  281. * @returns {*|Promise<any>|Promise}
  282. */
  283. removeDir: function removeDir(path, options) {
  284. return this.promisified({
  285. cmd: 'removeDir',
  286. path: path,
  287. options: options
  288. });
  289. },
  290. /**
  291. * @name copyFile
  292. * @description Copy file
  293. * Permissions based on the app's PID owner
  294. * @param {String} source
  295. * @param {String} destination
  296. * @param {Object} [options]
  297. * @param {BaseDirectory} [options.dir]
  298. * @returns {*|Promise<any>|Promise}
  299. */
  300. copyFile: function copyFile(source, destination, options) {
  301. return this.promisified({
  302. cmd: 'copyFile',
  303. source: source,
  304. destination: destination,
  305. options: options
  306. });
  307. },
  308. /**
  309. * @name removeFile
  310. * @description Removes a file
  311. * Permissions based on the app's PID owner
  312. * @param {String} path
  313. * @param {Object} [options]
  314. * @param {BaseDirectory} [options.dir]
  315. * @returns {*|Promise<any>|Promise}
  316. */
  317. removeFile: function removeFile(path, options) {
  318. return this.promisified({
  319. cmd: 'removeFile',
  320. path: path,
  321. options: options
  322. });
  323. },
  324. /**
  325. * @name renameFile
  326. * @description Renames a file
  327. * Permissions based on the app's PID owner
  328. * @param {String} path
  329. * @param {Object} [options]
  330. * @param {BaseDirectory} [options.dir]
  331. * @returns {*|Promise<any>|Promise}
  332. */
  333. renameFile: function renameFile(oldPath, newPath, options) {
  334. return this.promisified({
  335. cmd: 'renameFile',
  336. oldPath: oldPath,
  337. newPath: newPath,
  338. options: options
  339. });
  340. },
  341. /**
  342. * @name setTitle
  343. * @description Set the application's title
  344. * @param {String} title
  345. */
  346. setTitle: function setTitle(title) {
  347. this.invoke({
  348. cmd: 'setTitle',
  349. title: title
  350. });
  351. },
  352. /**
  353. * @name open
  354. * @description Open an URI
  355. * @param {String} uri
  356. */
  357. open: function open(uri) {
  358. this.invoke({
  359. cmd: 'open',
  360. uri: uri
  361. });
  362. },
  363. /**
  364. * @name execute
  365. * @description Execute a program with arguments.
  366. * Permissions based on the app's PID owner
  367. * @param {String} command
  368. * @param {String|Array} args
  369. * @returns {*|Promise<any>|Promise}
  370. */
  371. execute: function execute(command, args) {
  372. if (_typeof(args) === 'object') {
  373. Object.freeze(args);
  374. }
  375. return this.promisified({
  376. cmd: 'execute',
  377. command: command,
  378. args: typeof args === 'string' ? [args] : args
  379. });
  380. },
  381. /**
  382. * @name openDialog
  383. * @description Open a file/directory selection dialog
  384. * @param {String} [options]
  385. * @param {String} [options.filter]
  386. * @param {String} [options.defaultPath]
  387. * @param {Boolean} [options.multiple=false]
  388. * @param {Boolean} [options.directory=false]
  389. * @returns {Promise<String|String[]>} promise resolving to the select path(s)
  390. */
  391. openDialog: function openDialog(options) {
  392. var opts = options || {}
  393. if (_typeof(options) === 'object') {
  394. opts.default_path = opts.defaultPath
  395. Object.freeze(options);
  396. }
  397. return this.promisified({
  398. cmd: 'openDialog',
  399. options: opts
  400. });
  401. },
  402. /**
  403. * @name saveDialog
  404. * @description Open a file/directory save dialog
  405. * @param {String} [options]
  406. * @param {String} [options.filter]
  407. * @param {String} [options.defaultPath]
  408. * @returns {Promise<String>} promise resolving to the select path
  409. */
  410. saveDialog: function saveDialog(options) {
  411. var opts = options || {}
  412. if (_typeof(options) === 'object') {
  413. opts.default_path = opts.defaultPath
  414. Object.freeze(options);
  415. }
  416. return this.promisified({
  417. cmd: 'saveDialog',
  418. options: opts
  419. });
  420. },
  421. /**
  422. * @name httpRequest
  423. * @description Makes an HTTP request
  424. * @param {Object} options
  425. * @param {String} options.method GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, CONNECT or TRACE
  426. * @param {String} options.url the request URL
  427. * @param {Object} [options.headers] the request headers
  428. * @param {Object} [options.params] the request query params
  429. * @param {Object|String|Binary} [options.body] the request body
  430. * @param {Boolean} followRedirects whether to follow redirects or not
  431. * @param {Number} maxRedirections max number of redirections
  432. * @param {Number} connectTimeout request connect timeout
  433. * @param {Number} readTimeout request read timeout
  434. * @param {Number} timeout request timeout
  435. * @param {Boolean} allowCompression
  436. * @param {Number} [responseType=1] 1 - JSON, 2 - Text, 3 - Binary
  437. * @param {Number} [bodyType=3] 1 - Form, 2 - File, 3 - Auto
  438. * @returns {Promise<any>}
  439. */
  440. httpRequest: function httpRequest(options) {
  441. return this.promisified({
  442. cmd: 'httpRequest',
  443. options: options
  444. });
  445. },
  446. loadAsset: function loadAsset(assetName, assetType) {
  447. return this.promisified({
  448. cmd: 'loadAsset',
  449. asset: assetName,
  450. assetType: assetType || 'unknown'
  451. })
  452. },
  453. cliMatches: function () {
  454. return this.promisified({
  455. cmd: 'cliMatches'
  456. })
  457. }
  458. };
  459. // init tauri API
  460. try {
  461. window.tauri.invoke({
  462. cmd: 'init'
  463. })
  464. } catch (e) {
  465. window.addEventListener('DOMContentLoaded', function () {
  466. window.tauri.invoke({
  467. cmd: 'init'
  468. })
  469. }, true)
  470. }
  471. document.addEventListener('error', function (e) {
  472. var target = e.target
  473. while (target != null) {
  474. if (target.matches ? target.matches('img') : target.msMatchesSelector('img')) {
  475. window.tauri.loadAsset(target.src, 'image')
  476. .then(function (img) {
  477. target.src = img
  478. })
  479. break
  480. }
  481. target = target.parentElement
  482. }
  483. }, true)
  484. // open <a href="..."> links with the Tauri API
  485. function __openLinks() {
  486. document.querySelector('body').addEventListener('click', function (e) {
  487. var target = e.target
  488. while (target != null) {
  489. if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
  490. if (target.href && target.href.startsWith('http') && target.target === '_blank') {
  491. window.tauri.open(target.href)
  492. e.preventDefault()
  493. }
  494. break
  495. }
  496. target = target.parentElement
  497. }
  498. }, true)
  499. }
  500. if (document.readyState === 'complete' || document.readyState === 'interactive') {
  501. __openLinks()
  502. } else {
  503. window.addEventListener('DOMContentLoaded', function () {
  504. __openLinks()
  505. }, true)
  506. }