| 4 |
| } |
| 5 | if (window.navigator && !window.navigator.share) { |
| 6 | window.navigator.share = data => { |
| 7 | if (window.navigator.__share__receive) { |
| 8 | return new Promise((_, reject) => reject(new DOMException("Other sharing operations are in progress", "InvalidStateError"))) |
| 9 | } |
| 10 | if (typeof data !== 'object' || !data.url && !data.title && !data.text && !data.files) { |
| 11 | return new Promise((_, reject) => reject(new DOMException("share(...) receives only object with either url, title, text or files", "TypeError"))) |
| 12 | } |
| 13 | if (!window.TelegramWebview) { |
| 14 | return new Promise((_, reject) => reject(new DOMException("Must be handling a user gesture to perform a share", "NotAllowedError"))) |
| 15 | } |
| 16 | const { url, title, text } = data |
| 17 | const file = (Array.isArray(data.file) ? data.file[0] : data.file) || data.files && data.files[0] |
| 18 | if (file && file.arrayBuffer && file.size < 1024 * 1024 * 3) { |
| 19 | file.arrayBuffer().then(buffer => { |
| 20 | const bytes = Array.from(new Uint8Array(buffer)) |
| 21 | const filename = file.name |
| 22 | const filetype = file.type |
| 23 | window.TelegramWebview.resolveShare(JSON.stringify({ url, title, text }), bytes, filename, filetype); |
| 24 | }) |
| 25 | } else { |
| 26 | window.TelegramWebview.resolveShare(JSON.stringify({ url, title, text }), null, null, null); |
| 27 | } |
| 28 | return new Promise((resolve, reject) => { |
| 29 | window.navigator.__share__receive = reason => { |
| 30 | window.navigator.__share__receive = undefined; |
| 31 | if (!reason) { |
| 32 | resolve(); |
| 33 | } else if (reason === 'security') { |
| 34 | reject(new DOMException("Must be handling a user gesture to perform a share", "NotAllowedError")); |
| 35 | } else if (reason === 'abort') { |
| 36 | reject(new DOMException("The operation was aborted.", "AbortError")); |
| 37 | } else { |
| 38 | reject(new DOMException("", "DataError")); |
| 39 | } |
| 40 | }; |
| 41 | }); |
| 42 | }; |
| 43 | } |