E-book viewer: Improve display of Lookup results from Google

This commit is contained in:
Kovid Goyal 2025-08-06 08:57:19 +05:30
parent 5690f37477
commit 956ef2a662
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,5 +1,5 @@
/* vim:fileencoding=utf-8 /* vim:fileencoding=utf-8
* *
* Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net> * Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net>
* *
* Distributed under terms of the GPLv3 license * Distributed under terms of the GPLv3 license
@ -9,54 +9,107 @@
"use strict"; "use strict";
var num_tries = 0; var num_tries = 0;
var style_id = 'a' + Math.random().toString(36).slice(2);
function fix_google_markup() { function fix_google_markup() {
var cc = document.getElementById('center_col'); try {
var max_width = 'calc(100vw - 25px)'; var cc = document.getElementById('center_col');
if (!cc) { if (!cc) {
if (++num_tries > 10) return; if (++num_tries > 10) return;
setTimeout(fix_google_markup, 100); return setTimeout(fix_google_markup, 100);
return; }
}
cc.style.maxWidth = max_width; // figure out if they actually got a dictionary card
cc.style.marginLeft = '0'; var is_dictionary_result = !!document.querySelector('.lr_container, .lr_dct_ent');
var rcnt = document.getElementById('rcnt'); // grab the raw query
if (rcnt) rcnt.style.marginLeft = '0'; var q = new URLSearchParams(location.search.slice(1)).get('q') || ''
cc = document.getElementById('cnt');
if (cc) cc.style.paddingTop = '0'; if (is_dictionary_result) {
var s = document.getElementById('search'); // Only add styles once to prevent duplication
if (s) s.style.maxWidth = max_width; if (!document.getElementById(style_id)) {
var params = new URLSearchParams(document.location.search.substring(1)); var style = document.createElement('style');
var q = params.get('q'); style.id = style_id;
if (q && q.startsWith('define:')) { style.textContent = `
cc.style.position = 'absolute'; * {
cc.style.top = '0'; column-gap: 0!important;
cc.style.left = '0'; -webkit-column-gap: 0!important;
cc.style.paddingLeft = '6px'; }
cc.style.paddingRight = '6px'; #center_col {
var remove = ['sfcnt', 'top_nav', 'before-appbar', 'appbar', 'searchform', 'easter-egg', 'topstuff']; position: absolute !important;
remove.forEach(function(id) { top: 1px !important; /* Using your preferred 1px value */
var elem = document.getElementById(id); left: 0 !important;
if (elem) elem.style.display = 'none'; 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);
}
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';
// constrain define text
document.querySelectorAll('[data-topic]')
.forEach(e => e.style.maxWidth = maxW);
// Ensure footer stays at bottom - with null check
var cnt = document.getElementById('cnt');
if (cnt) cnt.style.minHeight = '100vh';
}
// hide bunch of useful UI elements
['sfcnt', 'top_nav', 'easter-egg', 'topstuff', 'searchform', 'appbar', 'before-appbar']
.forEach(function(id){
var e = document.getElementById(id);
if (e && e.style) e.style.display = 'none';
}); });
// make definitions text wrap // remove that promo sidebar, wrap rest nicely
document.querySelectorAll('[data-topic]').forEach(function(elem) { elem.style.maxWidth = max_width; }); 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("fix_google_markup() failed with error:");
console.error(e);
} }
var promo = document.getElementById('promos'); }
if (promo) promo.parentNode.removeChild(promo);
// make search results wrap if (location.hostname === 'www.google.com') {
document.querySelectorAll('[data-ved]').forEach(function(elem) { elem.style.maxWidth = max_width; }); window.addEventListener('DOMContentLoaded', fix_google_markup);
// the above wrapping causing overlapping text of the // Re-run on resize to handle Google's dynamic layout changes
// search results and the citation, fix that window.addEventListener('resize', function() {
document.querySelectorAll('cite').forEach(function(elem) { // Reset try counter to handle DOM changes
var d = elem.closest('div'); num_tries = 0;
if (d) d.style.position = 'static'; fix_google_markup();
}); });
} }
if (window.location.hostname === 'www.google.com') {
window.addEventListener('DOMContentLoaded', fix_google_markup);
}
})(); })();