| 6 |
| * down or right gesture. Use `event.preventDefault()` at `touchstart` to prevent those gestures. |
| 7 | * It is recommended to do `event.preventDefault()` when dragging or swiping is expected to be |
| 8 | * handled by a website. |
| 9 | * |
| 10 | * Since some websites don't do that, the script also captures `style` and `class` changes to |
| 11 | * hierarchy of a touch element, and does equivalent of `preventDefault` if those changes happen |
| 12 | * while `touchstart` or `touchmove` events. |
| 13 | */ |
| 14 |
| 15 | if (!window.__tg__webview_set) { |
| 16 | window.__tg__webview_set = true; |
| 17 | (function () { |
| 18 | const DEBUG = $DEBUG$; |
| 19 |
| 20 | // Touch gestures hacks |
| 21 | let prevented = false; |
| 22 | let awaitingResponse = false; |
| 23 | let touchElement = null; |
| 24 | let mutatedWhileTouch = false; |
| 25 | let whiletouchstart = false, whiletouchmove = false; |
| 26 | document.addEventListener('touchstart', e => { |
| 27 | touchElement = e.target; |
| 28 | awaitingResponse = true; |
| 29 | whiletouchstart = true; |
| 30 | }, true); |
| 31 | document.addEventListener('touchstart', e => { |
| 32 | whiletouchstart = false; |
| 33 | }, false); |
| 34 | document.addEventListener('touchmove', e => { |
| 35 | whiletouchstart = false; |
| 36 | whiletouchmove = true; |
| 37 | if (awaitingResponse) { |
| 38 | setTimeout(() => { |
| 39 | if (awaitingResponse) { |
| 40 | if (window.TelegramWebviewProxy) { |
| 41 | const allowScrollX = !prevented && (!window.visualViewport || window.visualViewport.offsetLeft == 0) && !mutatedWhileTouch; |
| 42 | const allowScrollY = !prevented && (!window.visualViewport || window.visualViewport.offsetTop == 0) && !mutatedWhileTouch; |
| 43 | if (DEBUG) { |
| 44 | console.log('tgbrowser allowScroll sent after "touchmove": x=' + allowScrollX + ' y=' + allowScrollY, { prevented, mutatedWhileTouch }); |
| 45 | } |
| 46 | window.TelegramWebviewProxy.postEvent('web_app_allow_scroll', JSON.stringify([ allowScrollX, allowScrollY ])); |
| 47 | } |
| 48 | prevented = false; |
| 49 | awaitingResponse = false; |
| 50 | } |
| 51 | mutatedWhileTouch = false; |
| 52 | }, 16); |
| 53 | } |
| 54 | }, true); |
| 55 | document.addEventListener('touchmove', e => { |
| 56 | whiletouchmove = false; |
| 57 | }, false); |
| 58 | document.addEventListener('scroll', e => { |
| 59 | if (!e.target) return; |
| 60 | const allowScrollX = e.target.scrollLeft == 0 && (!window.visualViewport || window.visualViewport.offsetLeft == 0) && !prevented && !mutatedWhileTouch; |
| 61 | const allowScrollY = e.target.scrollTop == 0 && (!window.visualViewport || window.visualViewport.offsetTop == 0) && !prevented && !mutatedWhileTouch; |
| 62 | if (DEBUG) { |
| 63 | console.log('tgbrowser scroll on' + e.target + ' scrollLeft=' + e.target.scrollLeft + ' scrollTop=' + e.target.scrollTop); |
| 64 | } |
| 65 | if (awaitingResponse) { |
| 66 | if (window.TelegramWebviewProxy) { |
| 67 | if (DEBUG) { |
| 68 | console.log('tgbrowser allowScroll sent after "scroll": x=' + allowScrollX + ' y=' + allowScrollY, { prevented, mutatedWhileTouch, scrollLeft: e.target.scrollLeft, scrollTop: e.target.scrollTop }); |
| 69 | } |
| 70 | window.TelegramWebviewProxy.postEvent('web_app_allow_scroll', JSON.stringify([allowScrollX, allowScrollY])); |
| 71 | } |
| 72 | awaitingResponse = false; |
| 73 | } |
| 74 | prevented = false; |
| 75 | mutatedWhileTouch = false; |
| 76 | }, true); |
| 77 | if (TouchEvent) { |
| 78 | const originalPreventDefault = TouchEvent.prototype.preventDefault; |
| 79 | TouchEvent.prototype.preventDefault = function () { |
| 80 | prevented = true; |
| 81 | originalPreventDefault.call(this); |
| 82 | }; |
| 83 | const originalStopPropagation = TouchEvent.prototype.stopPropagation; |
| 84 | TouchEvent.prototype.stopPropagation = function () { |
| 85 | if (this.type === 'touchmove') { |
| 86 | whiletouchmove = false; |
| 87 | } else if (this.type === 'touchstart') { |
| 88 | whiletouchstart = false; |
| 89 | } |
| 90 | originalStopPropagation.call(this); |
| 91 | }; |
| 92 | } |
| 93 | const isParentOf = (e, p) => { |
| 94 | if (!e || !p) return false; |
| 95 | if (e == p) return true; |
| 96 | return isParentOf(e.parentElement, p); |
| 97 | } |
| 98 | new MutationObserver(mutationList => { |
| 99 | const isTouchElement = touchElement && !![...(mutationList||[])] |
| 100 | .filter(r => r && (r.attributeName === 'style' || r.attributeName === 'class')) |
| 101 | .map(r => r.target) |
| 102 | .filter(e => !!e && e != document.body && e != document.documentElement) |
| 103 | .find(e => isParentOf(touchElement, e)); |
| 104 | if (isTouchElement) { // && (whiletouchstart || whiletouchmove)) { |
| 105 | if (DEBUG) { |
| 106 | console.log('tgbrowser mutation detected', mutationList); |
| 107 | } |
| 108 | mutatedWhileTouch = true; |
| 109 | } |
| 110 | }).observe(document, { attributes: true, childList: true, subtree: true }); |
| 111 | })(); |
| 112 | }; |