tauri.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // polyfills
  2. if (!String.prototype.startsWith) {
  3. String.prototype.startsWith = function (searchString, position) {
  4. position = position || 0
  5. return this.substr(position, searchString.length) === searchString
  6. }
  7. }
  8. (function () {
  9. function s4() {
  10. return Math.floor((1 + Math.random()) * 0x10000)
  11. .toString(16)
  12. .substring(1)
  13. }
  14. var uid = function () {
  15. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  16. s4() + '-' + s4() + s4() + s4()
  17. }
  18. function ownKeys(object, enumerableOnly) {
  19. var keys = Object.keys(object);
  20. if (Object.getOwnPropertySymbols) {
  21. var symbols = Object.getOwnPropertySymbols(object);
  22. if (enumerableOnly) symbols = symbols.filter(function (sym) {
  23. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  24. });
  25. keys.push.apply(keys, symbols);
  26. }
  27. return keys;
  28. }
  29. function _objectSpread(target) {
  30. for (var i = 1; i < arguments.length; i++) {
  31. var source = arguments[i] != null ? arguments[i] : {};
  32. if (i % 2) {
  33. ownKeys(source, true).forEach(function (key) {
  34. _defineProperty(target, key, source[key]);
  35. });
  36. } else if (Object.getOwnPropertyDescriptors) {
  37. Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
  38. } else {
  39. ownKeys(source).forEach(function (key) {
  40. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  41. });
  42. }
  43. }
  44. return target;
  45. }
  46. function _defineProperty(obj, key, value) {
  47. if (key in obj) {
  48. Object.defineProperty(obj, key, {
  49. value: value,
  50. enumerable: true,
  51. configurable: true,
  52. writable: true
  53. });
  54. } else {
  55. obj[key] = value;
  56. }
  57. return obj;
  58. }
  59. if (!window.__TAURI__) {
  60. window.__TAURI__ = {}
  61. }
  62. window.__TAURI__.transformCallback = function transformCallback(callback, once) {
  63. var identifier = uid()
  64. window[identifier] = function (result) {
  65. if (once) {
  66. delete window[identifier]
  67. }
  68. return callback && callback(result)
  69. }
  70. return identifier;
  71. }
  72. window.__TAURI__.promisified = function promisified(args) {
  73. var _this = this
  74. return new Promise(function (resolve, reject) {
  75. var callback = _this.transformCallback(function (r) {
  76. resolve(r)
  77. delete window[error]
  78. }, true)
  79. var error = _this.transformCallback(function (e) {
  80. reject(e)
  81. delete window[callback]
  82. }, true)
  83. window.__TAURI_INVOKE_HANDLER__(_objectSpread({
  84. callback: callback,
  85. error: error
  86. }, args))
  87. })
  88. }
  89. window.__TAURI__.loadAsset = function loadAsset(assetName, assetType) {
  90. return this.promisified({
  91. cmd: 'loadAsset',
  92. asset: assetName,
  93. assetType: assetType || 'unknown'
  94. })
  95. }
  96. document.addEventListener('error', function (e) {
  97. var target = e.target
  98. while (target != null) {
  99. if (target.matches ? target.matches('img') : target.msMatchesSelector('img')) {
  100. window.__TAURI__.loadAsset(target.src, 'image')
  101. .then(function (img) {
  102. target.src = img
  103. })
  104. break
  105. }
  106. target = target.parentElement
  107. }
  108. }, true)
  109. // open <a href="..."> links with the Tauri API
  110. function __openLinks() {
  111. document.querySelector('body').addEventListener('click', function (e) {
  112. var target = e.target
  113. while (target != null) {
  114. if (target.matches ? target.matches('a') : target.msMatchesSelector('a')) {
  115. if (target.href && target.href.startsWith('http') && target.target === '_blank') {
  116. window.__TAURI_INVOKE_HANDLER__({
  117. cmd: 'open',
  118. uri: target.href
  119. })
  120. e.preventDefault()
  121. }
  122. break
  123. }
  124. target = target.parentElement
  125. }
  126. }, true)
  127. }
  128. if (document.readyState === 'complete' || document.readyState === 'interactive') {
  129. __openLinks()
  130. } else {
  131. window.addEventListener('DOMContentLoaded', function () {
  132. __openLinks()
  133. }, true)
  134. }
  135. let permissionSettable = false
  136. let permissionValue = 'default'
  137. function isPermissionGranted() {
  138. if (window.Notification.permission !== 'default') {
  139. return Promise.resolve(window.Notification.permission === 'granted')
  140. }
  141. return window.__TAURI__.promisified({
  142. cmd: 'isNotificationPermissionGranted'
  143. })
  144. }
  145. function setNotificationPermission(value) {
  146. permissionSettable = true
  147. window.Notification.permission = value
  148. permissionSettable = false
  149. }
  150. function requestPermission() {
  151. return window.__TAURI__.promisified({
  152. cmd: 'requestNotificationPermission'
  153. }).then(function (permission) {
  154. setNotificationPermission(permission)
  155. return permission
  156. })
  157. }
  158. function sendNotification(options) {
  159. if (typeof options === 'object') {
  160. Object.freeze(options)
  161. }
  162. isPermissionGranted()
  163. .then(function (permission) {
  164. if (permission) {
  165. return window.__TAURI__.promisified({
  166. cmd: 'notification',
  167. options: typeof options === 'string' ? {
  168. title: options
  169. } : options
  170. })
  171. }
  172. })
  173. }
  174. window.Notification = function (title, options) {
  175. var opts = options || {}
  176. sendNotification(Object.assign(opts, {
  177. title: title
  178. }))
  179. }
  180. window.Notification.requestPermission = requestPermission
  181. Object.defineProperty(window.Notification, 'permission', {
  182. enumerable: true,
  183. get: function () {
  184. return permissionValue
  185. },
  186. set: function (v) {
  187. if (!permissionSettable) {
  188. throw new Error('Readonly property')
  189. }
  190. permissionValue = v
  191. }
  192. })
  193. isPermissionGranted()
  194. .then(function (response) {
  195. if (response === null) {
  196. setNotificationPermission('default')
  197. } else {
  198. setNotificationPermission(response ? 'granted' : 'denied')
  199. }
  200. })
  201. window.alert = function (message) {
  202. window.__TAURI_INVOKE_HANDLER__({
  203. cmd: 'messageDialog',
  204. message: message
  205. })
  206. }
  207. window.confirm = function (message) {
  208. return window.__TAURI__.promisified({
  209. cmd: 'askDialog',
  210. message: message
  211. })
  212. }
  213. })()