|
// ==UserScript== |
|
// @name Restore Products Tab on Google |
|
// @namespace http://tampermonkey.net/ |
|
// @version 2025-06-21 |
|
// @description Adds a "Products" tab to Google Search results that links to Google Shopping. Works across major Google domains and reappears dynamically after navigation changes (SPA support). Positioned after "Videos" if present, or at the end otherwise. |
|
// @author ChatGPT |
|
// @match *://*.google.com/search* |
|
// @match *://*.google.co.uk/search* |
|
// @match *://*.google.ca/search* |
|
// @match *://*.google.com.au/search* |
|
// @match *://*.google.co.in/search* |
|
// @match *://*.google.co.jp/search* |
|
// @match *://*.google.de/search* |
|
// @match *://*.google.fr/search* |
|
// @match *://*.google.nl/search* |
|
// @match *://*.google.it/search* |
|
// @match *://*.google.es/search* |
|
// @match *://*.google.com.br/search* |
|
// @match *://*.google.com.mx/search* |
|
// @match *://*.google.ru/search* |
|
// @match *://*.google.co.kr/search* |
|
// @match *://*.google.com.tr/search* |
|
// @match *://*.google.pl/search* |
|
// @match *://*.google.se/search* |
|
// @match *://*.google.no/search* |
|
// @match *://*.google.fi/search* |
|
// @match *://*.google.dk/search* |
|
// @match *://*.google.be/search* |
|
// @match *://*.google.pt/search* |
|
// @match *://*.google.gr/search* |
|
// @match *://*.google.cz/search* |
|
// @match *://*.google.hu/search* |
|
// @match *://*.google.sk/search* |
|
// @match *://*.google.ro/search* |
|
// @match *://*.google.bg/search* |
|
// @match *://*.google.com.eg/search* |
|
// @match *://*.google.co.za/search* |
|
// @match *://*.google.com.sg/search* |
|
// @match *://*.google.com.hk/search* |
|
// @match *://*.google.co.id/search* |
|
// @match *://*.google.co.th/search* |
|
// @match *://*.google.com.vn/search* |
|
// @match *://*.google.com.ph/search* |
|
// @match *://*.google.com.my/search* |
|
// @match *://*.google.tld/search* |
|
// @run-at document-idle |
|
// @grant none |
|
// ==/UserScript== |
|
|
|
(function () { |
|
'use strict'; |
|
|
|
const DEBUG = true; |
|
function log(...args) { |
|
if (DEBUG) console.log('[Restore Products Tab]', ...args); |
|
} |
|
|
|
function restoreProductsTab() { |
|
const possibleContainers = [ |
|
document.querySelector('div[role="navigation"] div[role="list"]'), |
|
document.querySelector('.MUFPAc'), |
|
document.querySelector('.nfdoRb'), |
|
document.querySelector('.T47uwc'), |
|
document.querySelector('.NZmxZe'), |
|
document.querySelector('.crJ18e') |
|
]; |
|
|
|
const tabsContainer = possibleContainers.find(el => el !== null); |
|
if (!tabsContainer) { |
|
log('No tab container found.'); |
|
return; |
|
} |
|
|
|
if (tabsContainer.querySelector('.products-button-custom')) { |
|
return; // Already added |
|
} |
|
|
|
const searchQuery = new URLSearchParams(window.location.search).get('q'); |
|
if (!searchQuery) { |
|
log('No search query found.'); |
|
return; |
|
} |
|
|
|
// Create the Products tab |
|
const listItem = document.createElement('div'); |
|
listItem.setAttribute('role', 'listitem'); |
|
listItem.classList.add('products-button-custom'); |
|
|
|
const link = document.createElement('a'); |
|
link.classList.add('nPDzT', 'T3FoJb'); |
|
link.href = `https://www.google.com/search?tbm=shop&q=${encodeURIComponent(searchQuery)}`; |
|
|
|
const text = document.createElement('div'); |
|
text.classList.add('YmvwI'); |
|
text.textContent = 'Products'; |
|
|
|
link.appendChild(text); |
|
listItem.appendChild(link); |
|
|
|
// Insert after "Videos" if found, otherwise at end |
|
const tabNodes = tabsContainer.querySelectorAll('[role="listitem"]'); |
|
let inserted = false; |
|
|
|
for (const tab of tabNodes) { |
|
const label = tab.textContent?.trim().toLowerCase(); |
|
if (label === 'videos') { |
|
log('Inserting Products tab after Videos'); |
|
if (tab.nextSibling) { |
|
tabsContainer.insertBefore(listItem, tab.nextSibling); |
|
} else { |
|
tabsContainer.appendChild(listItem); |
|
} |
|
inserted = true; |
|
break; |
|
} |
|
} |
|
|
|
if (!inserted) { |
|
log('Videos tab not found, appending Products tab at end'); |
|
tabsContainer.appendChild(listItem); |
|
} |
|
} |
|
|
|
if (document.readyState === 'loading') { |
|
document.addEventListener('DOMContentLoaded', restoreProductsTab); |
|
} else { |
|
restoreProductsTab(); |
|
} |
|
|
|
let lastUrl = location.href; |
|
const observer = new MutationObserver(() => { |
|
const currentUrl = location.href; |
|
if (currentUrl !== lastUrl) { |
|
lastUrl = currentUrl; |
|
setTimeout(restoreProductsTab, 100); |
|
} |
|
}); |
|
|
|
observer.observe(document, { subtree: true, childList: true }); |
|
|
|
setInterval(restoreProductsTab, 2000); |
|
})(); |