Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Asqii/1d952354950e96681b2e915fe2e469cd to your computer and use it in GitHub Desktop.
Save Asqii/1d952354950e96681b2e915fe2e469cd to your computer and use it in GitHub Desktop.
Restored Pagination for Google (Disable Continuous / Infinite Scrolling)
// ==UserScript==
// @name Restored Pagination for Google
// @namespace google-pagination.sp00n.net
// @exclude https://www.google.com/search?*&tbm=isch*
// @match https://www.google.com/search*
// @grant none
// @version 0.1
// @author sp00n
// @description This script restores the paged navigation for the Google Search. It also requires that the "Continuous scrolling" in the Google Search settings is turned off (which infuriatingly does not restore the old Pagination!)
// @downloadURL https://gist.github.com/sp00n/e8b91d2f47c471bc0627f7b31d659291
// ==/UserScript==
let params = new URL(document.location).searchParams;
let searchQuery = params.get("q");
let currentStartEntries = parseInt(params.get("start")) || 0;
let previousStartEntries = Math.max(0, currentStartEntries - 10);
let nextStartEntries = currentStartEntries + 10; // Can we get the maximum amount of search results?
let currentPage = Math.floor(currentStartEntries / 10) + 1; // Page 1 has start=0, Page 2 start=10, etc
let firstPageToDisplay = 1;
let lastPageToDisplay = 10;
// Weak detection, but here we go
let isDarkMode = (getComputedStyle(document.body).backgroundColor != "rgb(255, 255, 255)");
// Date Filters
// Last Hour https://www.google.com/search?q=abc&tbs=qdr:h
// Last Month https://www.google.com/search?q=abc&tbs=qdr:m
// Last Year https://www.google.com/search?q=abc&tbs=qdr:y
// https://www.google.com/search?q=abc&tbs=cdr%3A1%2Ccd_min%3A01.01.2020%2Ccd_max%3A02.02.2022&tbm=
// Language
// German https://www.google.com/search?q=abc&tbs=lr:lang_1de&lr=lang_de
// Build the fixed query string
let fixedQueryParams = new URLSearchParams();
fixedQueryParams.set("q", searchQuery);
if ( params.has("filter") ) {
fixedQueryParams.set("filter", params.get("filter"));
}
if ( params.has("tbs") ) {
fixedQueryParams.set("tbs", params.get("tbs"));
}
if ( params.has("lr") ) {
fixedQueryParams.set("lr", params.get("lr"));
}
let fixedQueryString = fixedQueryParams.toString();
// Determine which pages to display
// Page 1-6: display 1-10
// Page 7: 2-11
// Page 8: 3-12
// etc
// 5 to the left visible, 4 to the right
if ( currentPage >= 7 ) {
firstPageToDisplay = currentPage - 5;
lastPageToDisplay = currentPage + 4;
}
// The entries HTML string
let entriesHTMLString = "";
let entryBgPosTop = ( isDarkMode ) ? -112 : 0;
// Generate the entries
for ( let entryPage = firstPageToDisplay; entryPage <= lastPageToDisplay; entryPage++ ) {
let entryStartEntries = (entryPage - 1) * 10;
let entryBgPosLeft = ( entryPage == 1 ) ? -53 : -74;
let entryTemplate = "";
// The template for an entry in the navigation
if ( entryPage == currentPage ) {
entryTemplate = `
<td class="YyVfkd">
<span class="SJajHc" style="background:url(/images/nav_logo321_hr.webp) no-repeat;background-position:${entryBgPosLeft}px ${entryBgPosTop}px;background-size:167px;width:20px"></span>
${entryPage}
</td>
`;
}
else {
entryTemplate = `
<td>
<a aria-label="Page ${entryPage}" class="fl" href="/search?${fixedQueryString}&amp;start=${entryStartEntries}">
<span class="SJajHc NVbCr" style="background:url(/images/nav_logo321_hr.webp) no-repeat;background-position:${entryBgPosLeft}px ${entryBgPosTop}px;background-size:167px;width:20px"></span>
${entryPage}
</a>
</td>
`;
}
entriesHTMLString += entryTemplate;
}
// The navigation table
let navigationHTMLString = `
<table class="AaVjTc" style="border-collapse:collapse;text-align:left" role="presentation" id="restored-pagination">
<tbody>
<tr jsname="TeSSVd" valign="top">
<td aria-level="3" class="d6cvqb BBwThe" role="heading">
<a href="/search?${fixedQueryString}&amp;start=${previousStartEntries}" id="pnprev">
<span class="SJajHc NVbCr" style="background:url(/images/nav_logo321_hr.webp) no-repeat;background-position:0 ${entryBgPosTop}px;background-size:167px;width:53px;float:right"></span>
<span style="display:block;margin-right:35px;clear:right">Previous</span>
</a>
</td>
`
+ entriesHTMLString +
`
<td aria-level="3" class="d6cvqb BBwThe" role="heading">
<a href="/search?${fixedQueryString}&amp;start=${nextStartEntries}" id="pnnext" style="text-align:left">
<span class="SJajHc NVbCr" style="background:url(/images/nav_logo321_hr.webp) no-repeat;background-position:-96px ${entryBgPosTop}px;background-size:167px;width:71px"></span>
<span style="display:block;margin-left:53px">Next</span>
</a>
</td>
</tr>
</tbody>
</table>
`;
// Insert the table
const navigationContainer = document.querySelector("div[role='navigation']:not([class])");
navigationContainer.innerHTML = navigationHTMLString;
// And some additional styles
let styles = `
<style type="text/css">
#restored-pagination.AaVjTc {
margin: 30px auto 30px;
}
#restored-pagination.AaVjTc td {
padding: 0;
text-align: center;
}
#restored-pagination.AaVjTc a:link {
color: ${isDarkMode ? "#8ab4f8" : "#4285f4"};
font-weight: normal;
}
#restored-pagination .SJajHc {
background: url(/images/nav_logo321_hr.webp) no-repeat;
background-size: 167px;
overflow: hidden;
background-position: 0 0;
height: 40px;
display: block;
}
#restored-pagination .NVbCr {
cursor: pointer;
}
#restored-pagination .YyVfkd {
color: ${isDarkMode ? "#bdc1c6" : "#202124"};
font-weight: normal;
}
</style>
`;
navigationContainer.innerHTML += styles;
@Asqii
Copy link
Author

Asqii commented Jul 19, 2023

Added exclusions for image search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment