tauri.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. var __reject = function () {
  88. return new Promise(function (_, reject) {
  89. reject();
  90. });
  91. }
  92. window.tauri = {
  93. Dir: Dir,
  94. invoke: function invoke(args) {
  95. window.external.invoke(JSON.stringify(args));
  96. },
  97. listen: function listen(event, handler) {
  98. var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  99. this.invoke({
  100. cmd: 'listen',
  101. event: event,
  102. handler: window.tauri.transformCallback(handler, once),
  103. once: once
  104. });
  105. },
  106. emit: function emit(evt, payload) {
  107. this.invoke({
  108. cmd: 'emit',
  109. event: evt,
  110. payload: payload
  111. });
  112. },
  113. transformCallback: function transformCallback(callback) {
  114. var once = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  115. var identifier = uid();
  116. window[identifier] = function (result) {
  117. if (once) {
  118. delete window[identifier];
  119. }
  120. return callback && callback(result);
  121. };
  122. return identifier;
  123. },
  124. promisified: function promisified(args) {
  125. var _this = this;
  126. return new Promise(function (resolve, reject) {
  127. _this.invoke(_objectSpread({
  128. callback: _this.transformCallback(resolve),
  129. error: _this.transformCallback(reject)
  130. }, args));
  131. });
  132. },
  133. readTextFile: function readTextFile(path, options) {
  134. return this.promisified({
  135. cmd: 'readTextFile',
  136. path: path,
  137. options: options
  138. });
  139. },
  140. readBinaryFile: function readBinaryFile(path, options) {
  141. return this.promisified({
  142. cmd: 'readBinaryFile',
  143. path: path,
  144. options: options
  145. });
  146. },
  147. writeFile: function writeFile(cfg, options) {
  148. if (_typeof(cfg) === 'object') {
  149. Object.freeze(cfg);
  150. }
  151. return this.promisified({
  152. cmd: 'writeFile',
  153. file: cfg.file,
  154. contents: cfg.contents,
  155. options: options
  156. });
  157. },
  158. readDir: function readDir(path, options) {
  159. return this.promisified({
  160. cmd: 'readDir',
  161. path: path,
  162. options: options
  163. });
  164. },
  165. createDir: function createDir(path, options) {
  166. return this.promisified({
  167. cmd: 'createDir',
  168. path: path,
  169. options: options
  170. });
  171. },
  172. removeDir: function removeDir(path, options) {
  173. return this.promisified({
  174. cmd: 'removeDir',
  175. path: path,
  176. options: options
  177. });
  178. },
  179. copyFile: function copyFile(source, destination, options) {
  180. return this.promisified({
  181. cmd: 'copyFile',
  182. source: source,
  183. destination: destination,
  184. options: options
  185. });
  186. },
  187. removeFile: function removeFile(path, options) {
  188. return this.promisified({
  189. cmd: 'removeFile',
  190. path: path,
  191. options: options
  192. });
  193. },
  194. renameFile: function renameFile(oldPath, newPath, options) {
  195. return this.promisified({
  196. cmd: 'renameFile',
  197. old_path: oldPath,
  198. new_path: newPath,
  199. options: options
  200. });
  201. },
  202. setTitle: function setTitle(title) {
  203. this.invoke({
  204. cmd: 'setTitle',
  205. title: title
  206. });
  207. },
  208. open: function open(uri) {
  209. this.invoke({
  210. cmd: 'open',
  211. uri: uri
  212. });
  213. },
  214. execute: function execute(command, args) {
  215. if (_typeof(args) === 'object') {
  216. Object.freeze(args);
  217. }
  218. return this.promisified({
  219. cmd: 'execute',
  220. command: command,
  221. args: typeof args === 'string' ? [args] : args
  222. });
  223. },
  224. openDialog: function openDialog(options) {
  225. var opts = options || {}
  226. if (_typeof(options) === 'object') {
  227. opts.default_path = opts.defaultPath
  228. Object.freeze(options);
  229. }
  230. return this.promisified({
  231. cmd: 'openDialog',
  232. options: opts
  233. });
  234. },
  235. saveDialog: function saveDialog(options) {
  236. var opts = options || {}
  237. if (_typeof(options) === 'object') {
  238. opts.default_path = opts.defaultPath
  239. Object.freeze(options);
  240. }
  241. return this.promisified({
  242. cmd: 'saveDialog',
  243. options: opts
  244. });
  245. },
  246. loadAsset: function loadAsset(assetName, assetType) {
  247. return this.promisified({
  248. cmd: 'loadAsset',
  249. asset: assetName,
  250. asset_type: assetType || 'unknown'
  251. })
  252. }
  253. };
  254. // init tauri API
  255. try {
  256. window.tauri.invoke({
  257. cmd: 'init'
  258. })
  259. } catch (e) {
  260. window.addEventListener('DOMContentLoaded', function () {
  261. window.tauri.invoke({
  262. cmd: 'init'
  263. })
  264. }, true)
  265. }
  266. document.addEventListener('error', function (e) {
  267. var target = e.target
  268. while (target != null) {
  269. if (target.matches ? target.matches('img') : target.msMatchesSelector('img')) {
  270. window.tauri.loadAsset(target.src, 'image')
  271. .then(function (img) {
  272. target.src = img
  273. })
  274. break
  275. }
  276. target = target.parentElement
  277. }
  278. }, true)
  279. // open <a href="..."> links with the Tauri API
  280. function __openLinks() {
  281. document.querySelector('body').addEventListener('click', function (e) {
  282. var target = e.target
  283. while (target != null) {
  284. if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
  285. if (target.href && target.href.startsWith('http') && target.target === '_blank') {
  286. window.tauri.open(target.href)
  287. e.preventDefault()
  288. }
  289. break
  290. }
  291. target = target.parentElement
  292. }
  293. }, true)
  294. }
  295. if (document.readyState === 'complete' || document.readyState === 'interactive') {
  296. __openLinks()
  297. } else {
  298. window.addEventListener('DOMContentLoaded', function () {
  299. __openLinks()
  300. }, true)
  301. }