Wikipedia AI + Google Button

Adds "AI" and "Google" buttons next to Wikipedia article titles for quick searches.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Wikipedia AI + Google Button
// @namespace    http://tampermonkey.net/
// @version      1.31
// @description  Adds "AI" and "Google" buttons next to Wikipedia article titles for quick searches.
// @author       DoctorEye
// @match        https://*.wikipedia.org/wiki/*
// @license MIT
// @grant        none
// ==/UserScript==
 
(function() {
    'use strict';
 
    // Βρίσκουμε το container του τίτλου
    const firstHeading = document.querySelector('#firstHeading');
    if (!firstHeading) return;
 
    // Προσπαθούμε πρώτα το span (νέος τρόπος)
    let titleElement = firstHeading.querySelector('.mw-page-title-main');
 
    // Αν δεν βρεθεί → παίρνουμε όλο το κείμενο του h1 και το καθαρίζουμε
    let pageTitle;
    if (titleElement) {
        pageTitle = titleElement.textContent.trim();
    } else {
        // Αφαίρεση edit link text αν υπάρχει (συχνά "[edit]" ή κενά)
        pageTitle = firstHeading.textContent
            .replace(/\s*\[edit\]\s*/gi, '')   // αφαιρεί [edit] links
            .replace(/\s+/g, ' ')              // πολλαπλά κενά → ένα
            .trim();
    }
 
    if (!pageTitle) return; // safety check
 
    // Προαιρετικό: αφαίρεση (parenthetical disambiguation) από το query
    // π.χ. "The Sixth Sense (film)" → "The Sixth Sense"
    // Αφαίρεσε τα // μπροστά από τις γραμμές αν το θες
    // const cleanTitle = pageTitle.replace(/\s*\([^)]+\)$/, '').trim();
    // const query = encodeURIComponent(cleanTitle);
 
    const query = encodeURIComponent(pageTitle);
 
    // Δημιουργία κουμπιού AI
    const aiButton = document.createElement('button');
    aiButton.textContent = 'AI';
    aiButton.style.backgroundColor = '#4CAF50';
    aiButton.style.color = 'white';
    aiButton.style.border = 'none';
    aiButton.style.padding = '2px 6px';
    aiButton.style.marginLeft = '10px';
    aiButton.style.fontSize = '14px';
    aiButton.style.cursor = 'pointer';
    aiButton.style.borderRadius = '3px';
    aiButton.addEventListener('click', () => {
        window.open(`https://google.com/ai?q=${query}`, '_blank');
    });
 
    // Δημιουργία κουμπιού Google (με hl=el όπως είχες)
    const googleButton = document.createElement('button');
    googleButton.textContent = 'G';
    googleButton.style.backgroundColor = '#4285F4';
    googleButton.style.color = 'white';
    googleButton.style.border = 'none';
    googleButton.style.padding = '2px 6px';
    googleButton.style.marginLeft = '5px';
    googleButton.style.fontSize = '14px';
    googleButton.style.cursor = 'pointer';
    googleButton.style.borderRadius = '3px';
    googleButton.addEventListener('click', () => {
        window.open(`http://www.google.com/search?hl=el&safe=off&q=${query}`, '_blank');
    });
 
    // Εισαγωγή κουμπιών **μετά** τον τίτλο (στο τέλος του firstHeading)
    firstHeading.appendChild(aiButton);
    firstHeading.appendChild(googleButton);
 
    // Προαιρετικό: μικρή βελτίωση εμφάνισης (τα κουμπιά να είναι inline με τον τίτλο)
    firstHeading.style.display = 'flex';
    firstHeading.style.alignItems = 'center';
    firstHeading.style.flexWrap = 'wrap';
})();