| 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 | * You can also globally disable swipes for X and/or Y with <meta> tags: |
| 11 | * - <meta name="tg:swipes:x" content="none"> |
| 12 | * - <meta name="tg:swipes:y" content="allow"> |
| 13 | * Please, use these <meta> tags as the last resort, as it disables convenient back and close |
| 14 | * gestures, degrading user experience. |
| 15 | * |
| 16 | * Since some websites don't do that, the script also captures `style` and `class` changes to |
| 17 | * hierarchy of a touch element, and does equivalent of `preventDefault` if those changes happen |
| 18 | * while `touchstart` or `touchmove` events. |
| 19 | * |
| 20 | * === feature is hidden under debug button === |
| 21 | * # Action Bar and Navigation Bar colors |
| 22 | * Top action bar and bottom navigation bar colors are defined with: |
| 23 | * - <meta name="tg:theme-accent" content="#FFFFFF" /> — action bar, usually an accent color |
| 24 | * - <meta name="theme-color" content="#FFFFFF" /> — action bar, usually an accent color |
| 25 | * - <meta name="tg:theme-background" content="#FFFFFF" /> — navigation bar |
| 26 | * - <meta name="theme-background-color" content="#FFFFFF" /> — navigation bar |
| 27 | * - <body> `background-color` css style — fallback |
| 28 | * `media` attribute on <meta> is also supported, feel free to use `prefers-color-scheme` |
| 29 | */ |
| 30 |
| 31 | if (!window.__tg__webview_set) { |
| 32 | window.__tg__webview_set = true; |
| 33 | (function () { |
| 34 | const DEBUG = $DEBUG$; |
| 35 |
| 36 | // Touch gestures hacks |
| 37 | const isImageViewer = () => { |
| 38 | if (!document.body.children || document.body.children.length != 1) return false; |
| 39 | const img = document.querySelector('body > img'); |
| 40 | return img && img.tagName && img.tagName.toLowerCase() === 'img' && img.src === window.location.href; |
| 41 | } |
| 42 | const swipesDisabled = axis => |
| 43 | (document.querySelector(`meta[name="tg:swipes:${axis}"]`)||{}).content === 'none'; |
| 44 | let prevented = false; |
| 45 | let awaitingResponse = false; |
| 46 | let touchElement = null; |
| 47 | let mutatedWhileTouch = false; |
| 48 | let whiletouchstart = false, whiletouchmove = false; |
| 49 | document.addEventListener('touchstart', e => { |
| 50 | touchElement = e.target; |
| 51 | awaitingResponse = true; |
| 52 | whiletouchstart = true; |
| 53 | if (isImageViewer()) { |
| 54 | if (window.TelegramWebviewProxy) { |
| 55 | const allowScrollX = window.visualViewport && window.visualViewport.offsetLeft == 0 && !swipesDisabled('x'); |
| 56 | const allowScrollY = window.visualViewport && window.visualViewport.offsetTop == 0 && !swipesDisabled('y'); |
| 57 | if (DEBUG) { |
| 58 | console.log('tgbrowser allowScroll sent after "touchstart": x=' + allowScrollX + ' y=' + allowScrollY + ' inside image viewer'); |
| 59 | } |
| 60 | window.TelegramWebviewProxy.postEvent('allowScroll', JSON.stringify([ allowScrollX, allowScrollY ])); |
| 61 | } |
| 62 | awaitingResponse = false; |
| 63 | } |
| 64 | }, true); |
| 65 | document.addEventListener('touchstart', e => { |
| 66 | whiletouchstart = false; |
| 67 | }, false); |
| 68 | document.addEventListener('touchmove', e => { |
| 69 | whiletouchstart = false; |
| 70 | whiletouchmove = true; |
| 71 | if (awaitingResponse) { |
| 72 | setTimeout(() => { |
| 73 | if (awaitingResponse) { |
| 74 | if (window.TelegramWebviewProxy) { |
| 75 | const allowScrollX = !prevented && (!window.visualViewport || window.visualViewport.offsetLeft == 0) && !mutatedWhileTouch && !swipesDisabled('x'); |
| 76 | const allowScrollY = !prevented && (!window.visualViewport || window.visualViewport.offsetTop == 0) && !mutatedWhileTouch && !swipesDisabled('y'); |
| 77 | if (DEBUG) { |
| 78 | console.log('tgbrowser allowScroll sent after "touchmove": x=' + allowScrollX + ' y=' + allowScrollY, { prevented, mutatedWhileTouch }); |
| 79 | } |
| 80 | window.TelegramWebviewProxy.postEvent('allowScroll', JSON.stringify([ allowScrollX, allowScrollY ])); |
| 81 | } |
| 82 | prevented = false; |
| 83 | awaitingResponse = false; |
| 84 | } |
| 85 | mutatedWhileTouch = false; |
| 86 | }, 16); |
| 87 | } |
| 88 | }, true); |
| 89 | document.addEventListener('touchmove', e => { |
| 90 | whiletouchmove = false; |
| 91 | }, false); |
| 92 | document.addEventListener('scroll', e => { |
| 93 | if (!e.target) return; |
| 94 | const allowScrollX = e.target.scrollLeft == 0 && (!window.visualViewport || window.visualViewport.offsetLeft == 0) && !prevented && !mutatedWhileTouch && !swipesDisabled('x'); |
| 95 | const allowScrollY = e.target.scrollTop == 0 && (!window.visualViewport || window.visualViewport.offsetTop == 0) && !prevented && !mutatedWhileTouch && !swipesDisabled('y'); |
| 96 | if (DEBUG) { |
| 97 | console.log('tgbrowser scroll on' + e.target + ' scrollLeft=' + e.target.scrollLeft + ' scrollTop=' + e.target.scrollTop); |
| 98 | } |
| 99 | if (awaitingResponse) { |
| 100 | if (window.TelegramWebviewProxy) { |
| 101 | if (DEBUG) { |
| 102 | console.log('tgbrowser allowScroll sent after "scroll": x=' + allowScrollX + ' y=' + allowScrollY, { prevented, mutatedWhileTouch, scrollLeft: e.target.scrollLeft, scrollTop: e.target.scrollTop }); |
| 103 | } |
| 104 | window.TelegramWebviewProxy.postEvent('allowScroll', JSON.stringify([allowScrollX, allowScrollY])); |
| 105 | } |
| 106 | awaitingResponse = false; |
| 107 | } |
| 108 | prevented = false; |
| 109 | mutatedWhileTouch = false; |
| 110 | }, true); |
| 111 | if (TouchEvent) { |
| 112 | const originalPreventDefault = TouchEvent.prototype.preventDefault; |
| 113 | TouchEvent.prototype.preventDefault = function () { |
| 114 | prevented = true; |
| 115 | originalPreventDefault.call(this); |
| 116 | }; |
| 117 | const originalStopPropagation = TouchEvent.prototype.stopPropagation; |
| 118 | TouchEvent.prototype.stopPropagation = function () { |
| 119 | if (this.type === 'touchmove') { |
| 120 | whiletouchmove = false; |
| 121 | } else if (this.type === 'touchstart') { |
| 122 | whiletouchstart = false; |
| 123 | } |
| 124 | originalStopPropagation.call(this); |
| 125 | }; |
| 126 | } |
| 127 | const isParentOf = (e, p) => { |
| 128 | if (!e || !p) return false; |
| 129 | if (e == p) return true; |
| 130 | return isParentOf(e.parentElement, p); |
| 131 | } |
| 132 | new MutationObserver(mutationList => { |
| 133 | const isTouchElement = touchElement && !![...(mutationList||[])] |
| 134 | .filter(r => r && (r.attributeName === 'style' || r.attributeName === 'class')) |
| 135 | .map(r => r.target) |
| 136 | .filter(e => !!e && e != document.body && e != document.documentElement) |
| 137 | .find(e => isParentOf(touchElement, e)); |
| 138 | if (isTouchElement) { // && (whiletouchstart || whiletouchmove)) { |
| 139 | if (DEBUG) { |
| 140 | console.log('tgbrowser mutation detected', mutationList); |
| 141 | } |
| 142 | mutatedWhileTouch = true; |
| 143 | } |
| 144 | }).observe(document, { attributes: true, childList: true, subtree: true }); |
| 145 |
| 146 | // Retrieving colors |
| 147 | const __tg__backgroundColor = () => { |
| 148 | try { |
| 149 | return window.getComputedStyle(document.body, null).getPropertyValue('background-color'); |
| 150 | } catch (e) { |
| 151 | return null; |
| 152 | } |
| 153 | } |
| 154 | const __tg__metaColor = name => |
| 155 | [...document.querySelectorAll(`meta[name="${name}"]`)] |
| 156 | .filter(meta => !meta.media || window.matchMedia && window.matchMedia(meta.media).matches) |
| 157 | .map(meta => meta.content)[0]; |
| 158 | const __tg__cssColorToRGBA = color => { |
| 159 | if (!color) return null; |
| 160 | if (color[0] === '#') { |
| 161 | let hex = color.slice(1); |
| 162 | if (hex.length === 3 || hex.length === 4) { |
| 163 | hex = hex.split('').map(char => char + char).join(''); |
| 164 | } |
| 165 | return [parseInt(hex.slice(0,2), 16), parseInt(hex.slice(2,4), 16), parseInt(hex.slice(4,6), 16), hex.length <= 6 ? 1 : parseInt(hex.slice(6,8), 16) / 255]; |
| 166 | } |
| 167 | const colorMatch = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/); |
| 168 | if (colorMatch) { |
| 169 | return [parseInt(colorMatch[1]), parseInt(colorMatch[2]), parseInt(colorMatch[3]), colorMatch[4] ? parseFloat(colorMatch[4]) : 1]; |
| 170 | } |
| 171 | return null; |
| 172 | }; |
| 173 | let __tg__lastActionBarColor, __tg__lastNavigationBarColor; |
| 174 | window.__tg__postColorsChange = () => { |
| 175 | const actionBarColor = JSON.stringify(__tg__cssColorToRGBA( |
| 176 | __tg__metaColor("tg:theme-accent") || |
| 177 | __tg__metaColor("theme-color") || |
| 178 | __tg__backgroundColor() |
| 179 | )); |
| 180 | const navigationBarColor = JSON.stringify(__tg__cssColorToRGBA( |
| 181 | __tg__metaColor("tg:theme-background") || |
| 182 | __tg__metaColor("theme-background-color") || |
| 183 | __tg__backgroundColor() |
| 184 | )); |
| 185 | if (window.TelegramWebviewProxy) { |
| 186 | if (actionBarColor != __tg__lastActionBarColor) { |
| 187 | if (DEBUG) { |
| 188 | console.log('tgbrowser actionbar color', actionBarColor); |
| 189 | } |
| 190 | window.TelegramWebviewProxy.postEvent("actionBarColor", __tg__lastActionBarColor = actionBarColor); |
| 191 | } |
| 192 | if (navigationBarColor != __tg__lastNavigationBarColor) { |
| 193 | if (DEBUG) { |
| 194 | console.log('tgbrowser navbar color', navigationBarColor); |
| 195 | } |
| 196 | window.TelegramWebviewProxy.postEvent("navigationBarColor", __tg__lastNavigationBarColor = navigationBarColor); |
| 197 | } |
| 198 | } |
| 199 | }; |
| 200 | const __tg__colorsObserver = new MutationObserver(() => { |
| 201 | window.__tg__postColorsChange(); |
| 202 | setTimeout(window.__tg__postColorsChange, 500); |
| 203 | }); |
| 204 | window.__tg__listenColors = () => { |
| 205 | [ |
| 206 | document, |
| 207 | document.body, |
| 208 | ...document.querySelectorAll('meta[name="tg:theme-accent"]'), |
| 209 | ...document.querySelectorAll('meta[name="tg:theme-background"]'), |
| 210 | ...document.querySelectorAll('meta[name="theme-color"]'), |
| 211 | ...document.querySelectorAll('meta[name="theme-background-color"]') |
| 212 | ].filter(e => !!e).map(e => __tg__colorsObserver.observe(e, { attributes: true })); |
| 213 | if (window.matchMedia) { |
| 214 | window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', () => window.__tg__postColorsChange()); |
| 215 | window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => window.__tg__postColorsChange()); |
| 216 | } |
| 217 | }; |
| 218 | window.__tg__listenColors(); |
| 219 | window.addEventListener('ready', __tg__listenColors, true); |
| 220 | window.__tg__postColorsChange(); |
| 221 | })(); |
| 222 | }; |
| 223 |
| 224 | setTimeout(function () { |
| 225 | const site_name = ( |
| 226 | (document.querySelector('meta[property="og:site_name"]') || {}).content || |
| 227 | (document.querySelector('meta[property="og:title"]') || {}).content |
| 228 | ); |
| 229 | if (window.TelegramWebviewProxy && window.TelegramWebviewProxy.postEvent) { |
| 230 | if (site_name) { |
| 231 | window.TelegramWebviewProxy.postEvent('siteName', site_name); |
| 232 | } else { |
| 233 | window.TelegramWebviewProxy.postEvent('siteNameEmpty'); |
| 234 | } |
| 235 | } |
| 236 | if (window.__tg__listenColors) { |
| 237 | window.__tg__listenColors(); |
| 238 | } |
| 239 | if (window.__tg__postColorsChange) { |
| 240 | window.__tg__postColorsChange(); |
| 241 | } |
| 242 | }, 10); |