| | // ==UserScript== | | | // @name Gemini Adaptive Tables, Clean UI & Scroll-Lock | | | // @name:pt-BR Gemini: Tabelas Adaptáveis, UI Limpa e Trava de Rolagem | | | // @namespace leitonico3000 | | | // @version 1.1 | | | // @author leitonico | | | // @description Native chat & prompt bar, symmetric adaptive wide tables with no layout shift, fixes broken inline-code auto-links, blocks autoscroll while generating. Non-intrusive: @grant none. | | | // @description:pt-BR Chat e barra de prompt nativos, tabelas largas adaptáveis e simétricas sem pulos de layout, corrige auto-links quebrados em código inline, bloqueia autoscroll na geração. Não intrusivo: @grant none. | | | // @homepageURL https://gist.github.com/leitonico/ce43f464613e47041f662a5f69efc623 | | | // @updateURL https://gist.githubusercontent.com/leitonico/ce43f464613e47041f662a5f69efc623/raw/gemini-adaptive-tables.user.js | | | // @downloadURL https://gist.githubusercontent.com/leitonico/ce43f464613e47041f662a5f69efc623/raw/gemini-adaptive-tables.user.js | | | // @match https://gemini.google.com/* | | | // @grant none |
| | // @run-at document-start |
| | // ==/UserScript== |
| | (function () { |
| | 'use strict'; |
| | const STYLE_ID = 'gemini-adaptive-tables-css'; |
| | const SPECIFICITY = ':not(#_):not(#_):not(#_)'; |
| | const SAFE_WIDTH_VAR = '--gemini-max-safe-w'; |
| | const TABLE_WIDTH_VAR = '--gemini-wide-table-w'; |
| | const TABLE_SELECTOR = 'table-block, .table-block'; |
| | const INLINE_CODE_SELECTOR = '.markdown code, .model-response-text code, message-content code'; |
| | const CODE_BLOCK_SELECTOR = 'pre, code-block, .code-block'; |
| | const AUTO_LINK_RE = /\[([^\]]+)\]$(https?:\/\/[^)\s]+)$/g; |
| | const HOOK_MARK = Symbol.for('gemini-scroll-lock'); |
| | function rule(selectors, declarations) { |
| | return `${selectors.map((selector) => selector + SPECIFICITY).join(',\n')} {${declarations}}`; |
| | } |
| | const CSS = [ |
| | rule([ |
| | '.conversation-container', | | | 'response-container', | | | '.response-container', | | | '.response-container-content', | | | '.response-content', | | | 'model-response', | | | '.model-response-text', | | | 'message-content', | | | '.markdown', |
| | '.markdown-main-panel', |
| | 'structured-content-container' |
| | ], | | | overflow-x: visible !important; | | |), |
| | rule(['.markdown', '.model-response-text'], ` |
| | font-size: 14.5px !important; |
| | line-height: 1.52 !important; |
| | `), |
| | rule(['table-block', '.table-block'], ` |
| | display: block !important; |
| | margin: 14px 0 !important; |
| | background: rgba(255, 255, 255, 0.025) !important; |
| | border: 1px solid rgba(255, 255, 255, 0.11) !important; |
| | border-radius: 12px !important; |
| | box-sizing: border-box !important; |
| | overflow: hidden !important; |
| | `), |
| | rule([ |
| | 'table-block > div', |
| | 'table-block .table-content', |
| | 'table-block .horizontal-scroll-wrapper', |
| | '.table-block > div', |
| | '.table-content', | | | '.horizontal-scroll-wrapper' | | | ], ` |
| | width: 100% !important; |
| | max-width: 100% !important; |
| | min-width: 0 !important; |
| | margin-left: 0 !important; |
| | margin-right: 0 !important; |
| | padding-left: 0 !important; |
| | padding-right: 0 !important; |
| | left: auto !important; |
| | transform: none !important; |
| | box-sizing: border-box !important; |
| | overflow-x: auto !important; |
| | `), |
| | rule(['table-block[data-table-size="small"]', '.table-block[data-table-size="small"]'], ` |
| | position: static !important; |
| | left: auto !important; |
| | transform: none !important; |
| | width: 100% !important; |
| | max-width: 100% !important; |
| | `), |
| | rule(['table-block[data-table-size="wide"]', '.table-block[data-table-size="wide"]'], ` |
| | position: relative !important; |
| | left: 50% !important; |
| | transform: translateX(-50%) !important; |
| | width: var(${TABLE_WIDTH_VAR}, 88vw) !important; |
| | max-width: var(${SAFE_WIDTH_VAR}, 92vw) !important; |
| | `), |
| | rule(['table-block table', '.markdown table'], ` |
| | width: 100% !important; |
| | max-width: 100% !important; |
| | min-width: 0 !important; |
| | margin: 0 !important; |
| | border-collapse: collapse !important; |
| | border-spacing: 0 !important; |
| | display: table !important; |
| | table-layout: auto !important; |
| | font-size: 13.5px !important; |
| | line-height: 1.38 !important; |
| | `), |
| | rule(['table-block[data-table-size="wide"] table'], ` |
| | font-size: 12.5px !important; |
| | line-height: 1.35 !important; |
| | `), |
| | rule(['table-block thead tr', '.markdown thead tr'], ` |
| | background: rgba(255, 255, 255, 0.055) !important; |
| | border-bottom: 1px solid rgba(255, 255, 255, 0.16) !important; |
| | `), |
| | rule(['table-block tbody tr', '.markdown tbody tr'], ` |
| | border-bottom: 1px solid rgba(255, 255, 255, 0.08) !important; |
| | transition: background-color 0.12s ease !important; |
| | `), |
| | rule(['table-block tbody tr:last-child', '.markdown tbody tr:last-child'], ` |
| | border-bottom: none !important; |
| | `), |
| | rule(['table-block tbody tr:hover', '.markdown tbody tr:hover'], ` |
| | background: rgba(255, 255, 255, 0.04) !important; |
| | `), |
| | rule(['table-block th', '.markdown th'], ` |
| | padding: 8px 11px !important; |
| | font-weight: 600 !important; |
| | text-align: left !important; |
| | vertical-align: bottom !important; |
| | border: none !important; |
| | min-width: 0 !important; |
| | white-space: normal !important; |
| | word-break: normal !important; |
| | overflow-wrap: break-word !important; |
| | `), |
| | rule(['table-block td', '.markdown td'], ` |
| | padding: 7px 11px !important; |
| | text-align: left !important; |
| | vertical-align: top !important; |
| | border: none !important; |
| | min-width: 0 !important; |
| | white-space: normal !important; |
| | word-break: normal !important; |
| | overflow-wrap: break-word !important; |
| | `), |
| | rule(['table-block[data-table-size="wide"] th'], ` |
| | padding: 7px 8px !important; |
| | `), |
| | rule(['table-block[data-table-size="wide"] td'], ` |
| | padding: 6px 8px !important; |
| | `), |
| | rule(['table-block td *', '.markdown td *', 'source-footnote', '.citation-chip'], ` |
| | white-space: normal !important; |
| | word-break: normal !important; |
| | overflow-wrap: break-word !important; |
| | max-width: 100% !important; |
| | `), |
| | rule(['.table-footer'], ` |
| | padding: 5px 12px !important; |
| | border-top: 1px solid rgba(255, 255, 255, 0.08) !important; |
| | background: rgba(255, 255, 255, 0.015) !important; |
| | `) |
| | ].join('\n\n'); |
| | function injectCSS() { |
| | if (document.getElementById(STYLE_ID)) return; |
| | const style = document.createElement('style'); |
| | style.id = STYLE_ID; |
| | style.textContent = CSS; |
| | (document.head \|\| document.documentElement).appendChild(style); |
| | } |
| | injectCSS(); |
| | function setStyleProperty(element, name, value) { |
| | if (element.style.getPropertyValue(name) !== value) { |
| | element.style.setProperty(name, value); |
| | } | | | } |
| | function classifyAndSizeTables() { |
| | const blocks = document.querySelectorAll(TABLE_SELECTOR); |
| | if (blocks.length === 0) return; |
| | const scroller = document.querySelector('infinite-scroller') \|\| document.querySelector('main'); |
| | const safeMaxW = scroller && scroller.clientWidth > 320 |
| | ? Math.max(680, scroller.clientWidth - 40) |
| | : Math.floor(window.innerWidth * 0.92); |
| | setStyleProperty(document.documentElement, SAFE_WIDTH_VAR, `${safeMaxW}px`); |
| | blocks.forEach((block) => { |
| | const firstRow = block.querySelector('tr'); |
| | if (!firstRow) return; |
| | const colCount = firstRow.querySelectorAll('th, td').length; |
| | if (colCount <= 4) { |
| | if (block.dataset.tableSize !== 'small') { |
| | block.dataset.tableSize = 'small'; | | | } |
| | } else { |
| | if (block.dataset.tableSize !== 'wide') { |
| | block.dataset.tableSize = 'wide'; | | | } |
| | const idealW = Math.min(safeMaxW, Math.max(860, colCount * 128)); |
| | setStyleProperty(block, TABLE_WIDTH_VAR, `${idealW}px`); |
| | } | | | }); | | | } |
| | function stripRedundantAutoLink(full, label, url) { |
| | const withoutProto = url.replace(/^https?:\/\//i, ''); |
| | if (url === label \|\| withoutProto === label \|\| withoutProto === `${label}/`) { |
| | return label; | | | } | | | return full; | | | } |
| | function cleanBrokenInlineCodeLinks() { |
| | const codes = document.querySelectorAll(INLINE_CODE_SELECTOR); |
| | for (let i = 0; i < codes.length; i++) { |
| | const code = codes[i]; |
| | if (code.childElementCount > 0 \|\| code.closest(CODE_BLOCK_SELECTOR)) continue; |
| | const raw = code.textContent; |
| | if (!raw \|\| !raw.includes('](')) continue; |
| | const cleaned = raw.replace(AUTO_LINK_RE, stripRedundantAutoLink); |
| | if (cleaned !== raw) { |
| | code.textContent = cleaned; |
| | } | | | } | | | } |
| | let frameId = 0; |
| | function processDOM() { |
| | frameId = 0; |
| | injectCSS(); |
| | classifyAndSizeTables(); |
| | cleanBrokenInlineCodeLinks(); |
| | } |
| | function scheduleProcessing() { |
| | if (!frameId) frameId = requestAnimationFrame(processDOM); |
| | } |
| | window.addEventListener('resize', scheduleProcessing, { passive: true }); |
| | const observer = new MutationObserver(scheduleProcessing); |
| | observer.observe(document.documentElement, { childList: true, subtree: true }); |
| | let scrollCallCount = 0; |
| | let scrollUrl = location.href; |
| | function hookScroll(target, name) { |
| | const original = target[name]; |
| | if (!original) return false; |
| | if (original[HOOK_MARK]) return true; |
| | const wrapped = function (...args) { |
| | if (location.href !== scrollUrl) { |
| | scrollUrl = location.href; |
| | scrollCallCount = 0; |
| | } |
| | if (++scrollCallCount === 1) return original.apply(this, args); |
| | }; |
| | Object.defineProperty(wrapped, HOOK_MARK, { value: true }); |
| | try { | | | Object.defineProperty(target, name, { value: wrapped, writable: false, configurable: false }); | | | return true; | | | } catch { | | | return false; | | | } | | | } |
| | function applyScrollLocks() { |
| | const results = [ |
| | hookScroll(Element.prototype, 'scrollIntoView'), | | | hookScroll(Element.prototype, 'scrollTo'), | | | hookScroll(Element.prototype, 'scrollBy'), | | | hookScroll(Element.prototype, 'scroll'), |
| | hookScroll(window, 'scrollTo'), |
| | hookScroll(window, 'scrollBy'), |
| | hookScroll(window, 'scroll') |
| | ]; |
| | return results.every(Boolean); |
| | } |
| | if (!applyScrollLocks()) { |
| | const retryTimer = setInterval(() => { |
| | if (applyScrollLocks()) clearInterval(retryTimer); |
| | }, 1000); |
| | } | | | })(); |