Greasy Fork 支持简体中文。

Add PDF Tab to Google Search

Adds a PDF tab to Google search results

// ==UserScript==
// @name         Add PDF Tab to Google Search
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a PDF tab to Google search results
// @author       Bui Quoc Dung
// @match        *://www.google.com/search*
// @icon         https://www.google.com/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Find the container for the search tabs (parent element containing the tab list)
    const tabContainer = document.querySelector('.crJ18e');

    if (!tabContainer) return;

    // Find the "Books" tab to insert the new tab after it
    const booksTab = [...tabContainer.querySelectorAll('div[role="listitem"]')]
        .find(tab => tab.textContent.trim() === 'Books');

    if (!booksTab) return;

    // Get the search query from the URL
    const urlParams = new URLSearchParams(window.location.search);
    const query = urlParams.get('q');

    if (!query) return;

    // Check if the query already contains "filetype:pdf"
    if (query.toLowerCase().includes("filetype:pdf")) {
        return; // If filetype:pdf is already in the query, do nothing
    }

    // Create the URL for the PDF search by appending "filetype:pdf"
    const pdfSearchUrl = `/search?q=${encodeURIComponent(query)}+filetype:pdf`;

    // Create a new PDF tab
    const pdfTab = document.createElement('div');
    pdfTab.setAttribute('role', 'listitem');
    pdfTab.innerHTML = `
        <a href="${pdfSearchUrl}" class="nPDzT T3FoJb">
            <div class="YmvwI">PDF</div>
        </a>
    `;

    // Insert the PDF tab after the "Books" tab
    booksTab.insertAdjacentElement('afterend', pdfTab);
})();