View Single Post
Old 08-05-2025, 09:55 PM   #4
ChairmanSaab
Junior Member
ChairmanSaab began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Aug 2025
Device: Generic
Post Fixed Non-dictionary results

Update: Fixes rendering issues with non-dictionary results. Tested on 8.7.0. {Mods: is it possible to delete the above post with code? I'll be including all the screenshots in this post.}

Code:
/* vim:fileencoding=utf-8
 * 
 * Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net>
 *
 * Distributed under terms of the GPLv3 license
 */

(function() {
    "use strict";

    var num_tries = 0;
    var styleAdded = false; // Track if we've added our CSS styles


    function fix_google_markup() {
        var cc = document.getElementById('center_col');
        if (!cc) {
            if (++num_tries <= 10) {
                return setTimeout(fix_google_markup, 100);
            }
            return;
        }

        // figure out if they actually got a dictionary card
        var isDict = !!document.querySelector('.lr_container, .lr_dct_ent');

        // grab the raw query
        var params = new URLSearchParams(location.search.slice(1));
        var q      = params.get('q') || '';

        // 1) DICTIONARY MODE
        if (isDict) {
            // Only add styles once to prevent duplication
            if (!styleAdded) {
                var style = document.createElement('style');
                style.textContent = `
                   * {
                     column-gap: 0!important;
                     -webkit-column-gap: 0!important;
                   }
                   #center_col {
                     position: absolute !important;
                     top: 1px !important; /* Using your preferred 1px value */
                     left: 0 !important;
                     z-index: 100;
                   }
                   #cnt {
                     position: relative;
                     min-height: 100vh;
                   }
                   /* Clear the space where search form was */
                   #searchform, #appbar, #before-appbar {
                     display: none !important;
                   }
                `;
                document.head.appendChild(style);
                styleAdded = true;
            }

            var maxW = 'calc(100vw - 25px)';
            cc.style.maxWidth   = maxW;
            cc.style.marginLeft = '0';

            ['rcnt','cnt','search']
              .forEach(function(id) {
                  var e = document.getElementById(id);
                  if (e) {
                      if (id==='search') e.style.maxWidth = maxW;
                      else if (id==='cnt')  e.style.paddingTop = '0';
                      else                  e.style.marginLeft = '0';
                  }
              });

            cc.style.paddingLeft  = '0';
            cc.style.paddingRight = '6px';

            // hide everything but the dictionary
            ['sfcnt','top_nav','easter-egg','topstuff']
            .forEach(function(id){
                var e = document.getElementById(id);
                if (e) e.style.display = 'none';
            });

            // Special handling for searchform area
            ['searchform', 'appbar', 'before-appbar'].forEach(function(id) {
                var e = document.getElementById(id);
                if (e) e.style.display = 'none';
            });

            // constrain define text
            document
              .querySelectorAll('[data-topic]')
              .forEach(e => e.style.maxWidth = maxW);

            // indent headings and watch for re-renders
            indentHeadings();
            new MutationObserver(indentHeadings)
              .observe(cc, { childList:true, subtree:true });
            
            // Ensure footer stays at bottom - with null check
            var cnt = document.getElementById('cnt');
            if (cnt) cnt.style.minHeight = '100vh';
        }

        // 2) Normal MODE (define: but no dict card)
        else if (q.startsWith('define:')) {
            // SAFER: Use try-catch for Normal mode operations
            try {
                ['sfcnt','top_nav','before-appbar','appbar',
                 'searchform','easter-egg','topstuff']
                .forEach(function(id){
                    var e = document.getElementById(id);
                    if (e) e.style.display = 'none';
                });
            } catch(e) {
                console.error("Error in Normal mode cleanup:", e);
            }
        }

        // 3) UNIVERSAL CLEAN-UP (with null checks)
        try {
            // remove that promo sidebar, wrap rest nicely
            var promo = document.getElementById('promos');
            if (promo) promo.remove();

            document
              .querySelectorAll('[data-ved]')
              .forEach(e => e.style.maxWidth = '100%');

            document
              .querySelectorAll('cite')
              .forEach(c => {
                  var wrap = c.closest('div');
                  if (wrap) wrap.style.position = 'static';
              });
        } catch(e) {
            console.error("Error in universal cleanup:", e);
        }
    }

    if (location.hostname === 'www.google.com') {
        window.addEventListener('DOMContentLoaded', fix_google_markup);
        
        // Re-run on resize to handle Google's dynamic layout changes
        window.addEventListener('resize', function() {
            // Reset try counter to handle DOM changes
            num_tries = 0;
            styleAdded = false;
            fix_google_markup();
        });
    }
})();
Click image for larger version

Name:	Lookup.png
Views:	25
Size:	109.6 KB
ID:	217322Click image for larger version

Name:	normalmode.jpg
Views:	23
Size:	347.7 KB
ID:	217323
ChairmanSaab is offline   Reply With Quote