tauri.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 () {
  49. function s4() {
  50. return Math.floor((1 + Math.random()) * 0x10000)
  51. .toString(16)
  52. .substring(1)
  53. }
  54. var uid = function () {
  55. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  56. s4() + '-' + s4() + s4() + s4()
  57. }
  58. 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; }
  59. 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; }
  60. 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; }
  61. 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); }
  62. /**
  63. * @typedef {number} BaseDirectory
  64. */
  65. /**
  66. * @enum {BaseDirectory}
  67. */
  68. var Dir = {
  69. Audio: 1,
  70. Cache: 2,
  71. Config: 3,
  72. Data: 4,
  73. LocalData: 5,
  74. Desktop: 6,
  75. Document: 7,
  76. Download: 8,
  77. Executable: 9,
  78. Font: 10,
  79. Home: 11,
  80. Picture: 12,
  81. Public: 13,
  82. Runtime: 14,
  83. Template: 15,
  84. Video: 16,
  85. Resource: 17,
  86. App: 18
  87. }
  88. function camelToKebab (string) {
  89. return string.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase()
  90. }
  91. /**
  92. * @name return __whitelistWarning
  93. * @description Present a stylish warning to the developer that their API
  94. * call has not been whitelisted in tauri.conf.json
  95. * @param {String} func - function name to warn
  96. * @private
  97. */
  98. var __whitelistWarning = function (func) {
  99. 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', ' ')
  100. return __reject()
  101. }
  102. /**
  103. * @name __reject
  104. * @description generates a promise used to deflect un-whitelisted tauri API calls
  105. * Its only purpose is to maintain thenable structure in client code without
  106. * breaking the application
  107. * * @type {Promise<any>}
  108. * @private
  109. */
  110. var __reject = function () {
  111. return new Promise(function (_, reject) {
  112. reject();
  113. });
  114. }
  115. window.tauri = {
  116. Dir: Dir,
  117. /**
  118. * @name invoke
  119. * @description Calls a Tauri Core feature, such as setTitle
  120. * @param {Object} args
  121. */
  122. invoke: function invoke(args) {
  123. window.external.invoke(JSON.stringify(args));
  124. },
  125. /**
  126. * @name listen
  127. * @description Add an event listener to Tauri backend
  128. * @param {String} event
  129. * @param {Function} handler
  130. * @param {Boolean} once
  131. */
  132. listen: function listen(event, handler) {
  133. var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  134. this.invoke({
  135. cmd: 'listen',
  136. event: event,
  137. handler: window.tauri.transformCallback(handler, once),
  138. once: once
  139. });
  140. },
  141. /**
  142. * @name emit
  143. * @description Emits an evt to the Tauri back end
  144. * @param {String} evt
  145. * @param {Object} payload
  146. */
  147. emit: function emit(evt, payload) {
  148. this.invoke({
  149. cmd: 'emit',
  150. event: evt,
  151. payload: payload
  152. });
  153. },
  154. /**
  155. * @name transformCallback
  156. * @description Registers a callback with a uid
  157. * @param {Function} callback
  158. * @param {Boolean} once
  159. * @returns {*}
  160. */
  161. transformCallback: function transformCallback(callback) {
  162. var once = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  163. var identifier = uid();
  164. window[identifier] = function (result) {
  165. if (once) {
  166. delete window[identifier];
  167. }
  168. return callback && callback(result);
  169. };
  170. return identifier;
  171. },
  172. /**
  173. * @name promisified
  174. * @description Turns a request into a chainable promise
  175. * @param {Object} args
  176. * @returns {Promise<any>}
  177. */
  178. promisified: function promisified(args) {
  179. var _this = this;
  180. return new Promise(function (resolve, reject) {
  181. _this.invoke(_objectSpread({
  182. callback: _this.transformCallback(resolve),
  183. error: _this.transformCallback(reject)
  184. }, args));
  185. });
  186. },
  187. /**
  188. * @name readTextFile
  189. * @description Accesses a non-binary file on the user's filesystem
  190. * and returns the content. Permissions based on the app's PID owner
  191. * @param {String} path
  192. * @param {Object} [options]
  193. * @param {BaseDirectory} [options.dir]
  194. * @returns {*|Promise<any>|Promise}
  195. */
  196. readTextFile: function readTextFile(path, options) {
  197. return this.promisified({
  198. cmd: 'readTextFile',
  199. path: path,
  200. options: options
  201. });
  202. },
  203. /**
  204. * @name readBinaryFile
  205. * @description Accesses a binary file on the user's filesystem
  206. * and returns the content. Permissions based on the app's PID owner
  207. * @param {String} path
  208. * @param {Object} [options]
  209. * @param {BaseDirectory} [options.dir]
  210. * @returns {*|Promise<any>|Promise}
  211. */
  212. readBinaryFile: function readBinaryFile(path, options) {
  213. return this.promisified({
  214. cmd: 'readBinaryFile',
  215. path: path,
  216. options: options
  217. });
  218. },
  219. /**
  220. * @name writeFile
  221. * @description Write a file to the Local Filesystem.
  222. * Permissions based on the app's PID owner
  223. * @param {Object} cfg
  224. * @param {String} cfg.file
  225. * @param {String|Binary} cfg.contents
  226. * @param {Object} [options]
  227. * @param {BaseDirectory} [options.dir]
  228. */
  229. writeFile: function writeFile(cfg, options) {
  230. if (_typeof(cfg) === 'object') {
  231. Object.freeze(cfg);
  232. }
  233. return this.promisified({
  234. cmd: 'writeFile',
  235. file: cfg.file,
  236. contents: cfg.contents,
  237. options: options
  238. });
  239. },
  240. /**
  241. * @name readDir
  242. * @description Reads a directory
  243. * Permissions based on the app's PID owner
  244. * @param {String} path
  245. * @param {Object} [options]
  246. * @param {Boolean} [options.recursive]
  247. * @param {BaseDirectory} [options.dir]
  248. * @returns {*|Promise<any>|Promise}
  249. */
  250. readDir: function readDir(path, options) {
  251. return this.promisified({
  252. cmd: 'readDir',
  253. path: path,
  254. options: options
  255. });
  256. },
  257. /**
  258. * @name createDir
  259. * @description Creates a directory
  260. * Permissions based on the app's PID owner
  261. * @param {String} path
  262. * @param {Object} [options]
  263. * @param {Boolean} [options.recursive]
  264. * @param {BaseDirectory} [options.dir]
  265. * @returns {*|Promise<any>|Promise}
  266. */
  267. createDir: function createDir(path, options) {
  268. return this.promisified({
  269. cmd: 'createDir',
  270. path: path,
  271. options: options
  272. });
  273. },
  274. /**
  275. * @name removeDir
  276. * @description Removes a directory
  277. * Permissions based on the app's PID owner
  278. * @param {String} path
  279. * @param {Object} [options]
  280. * @param {Boolean} [options.recursive]
  281. * @param {BaseDirectory} [options.dir]
  282. * @returns {*|Promise<any>|Promise}
  283. */
  284. removeDir: function removeDir(path, options) {
  285. return this.promisified({
  286. cmd: 'removeDir',
  287. path: path,
  288. options: options
  289. });
  290. },
  291. /**
  292. * @name copyFile
  293. * @description Copy file
  294. * Permissions based on the app's PID owner
  295. * @param {String} source
  296. * @param {String} destination
  297. * @param {Object} [options]
  298. * @param {BaseDirectory} [options.dir]
  299. * @returns {*|Promise<any>|Promise}
  300. */
  301. copyFile: function copyFile(source, destination, options) {
  302. return this.promisified({
  303. cmd: 'copyFile',
  304. source: source,
  305. destination: destination,
  306. options: options
  307. });
  308. },
  309. /**
  310. * @name removeFile
  311. * @description Removes a file
  312. * Permissions based on the app's PID owner
  313. * @param {String} path
  314. * @param {Object} [options]
  315. * @param {BaseDirectory} [options.dir]
  316. * @returns {*|Promise<any>|Promise}
  317. */
  318. removeFile: function removeFile(path, options) {
  319. return this.promisified({
  320. cmd: 'removeFile',
  321. path: path,
  322. options: options
  323. });
  324. },
  325. /**
  326. * @name renameFile
  327. * @description Renames a file
  328. * Permissions based on the app's PID owner
  329. * @param {String} path
  330. * @param {Object} [options]
  331. * @param {BaseDirectory} [options.dir]
  332. * @returns {*|Promise<any>|Promise}
  333. */
  334. renameFile: function renameFile(oldPath, newPath, options) {
  335. return this.promisified({
  336. cmd: 'renameFile',
  337. oldPath: oldPath,
  338. newPath: newPath,
  339. options: options
  340. });
  341. },
  342. /**
  343. * @name setTitle
  344. * @description Set the application's title
  345. * @param {String} title
  346. */
  347. setTitle: function setTitle(title) {
  348. this.invoke({
  349. cmd: 'setTitle',
  350. title: title
  351. });
  352. },
  353. /**
  354. * @name open
  355. * @description Open an URI
  356. * @param {String} uri
  357. */
  358. open: function open(uri) {
  359. this.invoke({
  360. cmd: 'open',
  361. uri: uri
  362. });
  363. },
  364. /**
  365. * @name execute
  366. * @description Execute a program with arguments.
  367. * Permissions based on the app's PID owner
  368. * @param {String} command
  369. * @param {String|Array} args
  370. * @returns {*|Promise<any>|Promise}
  371. */
  372. execute: function execute(command, args) {
  373. if (_typeof(args) === 'object') {
  374. Object.freeze(args);
  375. }
  376. return this.promisified({
  377. cmd: 'execute',
  378. command: command,
  379. args: typeof args === 'string' ? [args] : args
  380. });
  381. },
  382. /**
  383. * @name openDialog
  384. * @description Open a file/directory selection dialog
  385. * @param {String} [options]
  386. * @param {String} [options.filter]
  387. * @param {String} [options.defaultPath]
  388. * @param {Boolean} [options.multiple=false]
  389. * @param {Boolean} [options.directory=false]
  390. * @returns {Promise<String|String[]>} promise resolving to the select path(s)
  391. */
  392. openDialog: function openDialog(options) {
  393. var opts = options || {}
  394. if (_typeof(options) === 'object') {
  395. opts.default_path = opts.defaultPath
  396. Object.freeze(options);
  397. }
  398. return this.promisified({
  399. cmd: 'openDialog',
  400. options: opts
  401. });
  402. },
  403. /**
  404. * @name saveDialog
  405. * @description Open a file/directory save dialog
  406. * @param {String} [options]
  407. * @param {String} [options.filter]
  408. * @param {String} [options.defaultPath]
  409. * @returns {Promise<String>} promise resolving to the select path
  410. */
  411. saveDialog: function saveDialog(options) {
  412. var opts = options || {}
  413. if (_typeof(options) === 'object') {
  414. opts.default_path = opts.defaultPath
  415. Object.freeze(options);
  416. }
  417. return this.promisified({
  418. cmd: 'saveDialog',
  419. options: opts
  420. });
  421. },
  422. /**
  423. * @name httpRequest
  424. * @description Makes an HTTP request
  425. * @param {Object} options
  426. * @param {String} options.method GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, CONNECT or TRACE
  427. * @param {String} options.url the request URL
  428. * @param {Object} [options.headers] the request headers
  429. * @param {Object} [options.params] the request query params
  430. * @param {Object|String|Binary} [options.body] the request body
  431. * @param {Boolean} followRedirects whether to follow redirects or not
  432. * @param {Number} maxRedirections max number of redirections
  433. * @param {Number} connectTimeout request connect timeout
  434. * @param {Number} readTimeout request read timeout
  435. * @param {Number} timeout request timeout
  436. * @param {Boolean} allowCompression
  437. * @param {Number} [responseType=1] 1 - JSON, 2 - Text, 3 - Binary
  438. * @param {Number} [bodyType=3] 1 - Form, 2 - File, 3 - Auto
  439. * @returns {Promise<any>}
  440. */
  441. httpRequest: function httpRequest(options) {
  442. return this.promisified({
  443. cmd: 'httpRequest',
  444. options: options
  445. });
  446. },
  447. /**
  448. * @name notification
  449. * @description Display a desktop notification
  450. * @param {Object|String} options the notifications options if an object, otherwise its body
  451. * @param {String} [options.summary] the notification's summary
  452. * @param {String} options.body the notification's body
  453. * @param {String} [options.icon] the notifications's icon
  454. * @returns {*|Promise<any>|Promise}
  455. */
  456. notification: function notification(options) {
  457. if (_typeof(options) === 'object') {
  458. Object.freeze(options);
  459. }
  460. return window.tauri.isNotificationPermissionGranted()
  461. .then(function (permission) {
  462. if (permission) {
  463. return window.tauri.promisified({
  464. cmd: 'notification',
  465. options: typeof options === 'string' ? {
  466. body: options
  467. } : options
  468. });
  469. }
  470. })
  471. },
  472. isNotificationPermissionGranted: function isNotificationPermissionGranted() {
  473. if (window.Notification.permission !== 'default' && window.Notification.permission !== 'loading') {
  474. return Promise.resolve(window.Notification.permission === 'granted')
  475. }
  476. return window.tauri.promisified({
  477. cmd: 'isNotificationPermissionGranted'
  478. })
  479. },
  480. requestNotificationPermission: function requestNotificationPermission() {
  481. return window.tauri.promisified({
  482. cmd: 'requestNotificationPermission'
  483. }).then(function (state) {
  484. setNotificationPermission(state)
  485. return state
  486. })
  487. },
  488. loadAsset: function loadAsset(assetName, assetType) {
  489. return this.promisified({
  490. cmd: 'loadAsset',
  491. asset: assetName,
  492. assetType: assetType || 'unknown'
  493. })
  494. },
  495. cliMatches: function () {
  496. return this.promisified({
  497. cmd: 'cliMatches'
  498. })
  499. }
  500. };
  501. var notificationPermissionSettable = false
  502. var notificationPermission = 'default'
  503. function setNotificationPermission(value) {
  504. notificationPermissionSettable = true
  505. window.Notification.permission = value
  506. notificationPermissionSettable = false
  507. }
  508. window.Notification = function (title, options) {
  509. if (options === void 0) {
  510. options = {}
  511. }
  512. options.title = title
  513. window.tauri.notification(options)
  514. }
  515. window.Notification.requestPermission = window.tauri.requestNotificationPermission
  516. Object.defineProperty(window.Notification, 'permission', {
  517. enumerable: true,
  518. get: function () {
  519. return notificationPermission
  520. },
  521. set: function(v) {
  522. if (!notificationPermissionSettable) {
  523. throw new Error("Readonly property")
  524. }
  525. notificationPermission = v
  526. }
  527. });
  528. setNotificationPermission('loading')
  529. window.tauri.isNotificationPermissionGranted()
  530. .then(function (response) {
  531. if (response === null) {
  532. setNotificationPermission('default')
  533. } else {
  534. setNotificationPermission(response ? 'granted' : 'denied')
  535. }
  536. })
  537. // init tauri API
  538. try {
  539. window.tauri.invoke({
  540. cmd: 'init'
  541. })
  542. } catch (e) {
  543. window.addEventListener('DOMContentLoaded', function () {
  544. window.tauri.invoke({
  545. cmd: 'init'
  546. })
  547. }, true)
  548. }
  549. document.addEventListener('error', function (e) {
  550. var target = e.target
  551. while (target != null) {
  552. if (target.matches ? target.matches('img') : target.msMatchesSelector('img')) {
  553. window.tauri.loadAsset(target.src, 'image')
  554. .then(function (img) {
  555. target.src = img
  556. })
  557. break
  558. }
  559. target = target.parentElement
  560. }
  561. }, true)
  562. // open <a href="..."> links with the Tauri API
  563. function __openLinks() {
  564. document.querySelector('body').addEventListener('click', function (e) {
  565. var target = e.target
  566. while (target != null) {
  567. if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
  568. if (target.href && target.href.startsWith('http') && target.target === '_blank') {
  569. window.tauri.open(target.href)
  570. e.preventDefault()
  571. }
  572. break
  573. }
  574. target = target.parentElement
  575. }
  576. }, true)
  577. }
  578. if (document.readyState === 'complete' || document.readyState === 'interactive') {
  579. __openLinks()
  580. } else {
  581. window.addEventListener('DOMContentLoaded', function () {
  582. __openLinks()
  583. }, true)
  584. }
  585. })()