Google Fixed Tab Order (2023-08)

Stops google from reordering the tabs like wtf are you doing you piece of shit, shamefully copied from https://greasyfork.org/en/scripts/18521-google-fixed-tab-order/code and updated for June 2023

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Google Fixed Tab Order (2023-08)
// @namespace   google.com
// @description Stops google from reordering the tabs like wtf are you doing you piece of shit, shamefully copied from https://greasyfork.org/en/scripts/18521-google-fixed-tab-order/code and updated for June 2023
// @include     https://www.google.*/search?*
// @include     https://www.google.*/webhp?*
// @version     3.3
// @grant       none
// @author      cabtv
// @license     MIT
// ==/UserScript==

function contains(selector, text) {
  var elements = document.querySelectorAll(selector);
  return [].filter.call(elements, function(element){
    return RegExp(text).test(element.textContent);
  });
}

function getTab(text) {

    let magicString;

    // yeah, that should work but it doesn't
    magicString = document.querySelector('[role="navigation"] [role="navigation"] > div > div > div');
    if ( !magicString ) return undefined;
    magicString = magicString.getAttribute('jscontroller')

    //magicString = 'fcDBE';

    let e = contains('[jscontroller="' + magicString + '"] > a', text);
    if (e != undefined && e[0] != undefined)
        return [e[0], e[0]];
    e = contains('[jscontroller="' + magicString + '"] div', text);
    if (e != undefined && e[0] != undefined)
        return [e[0].querySelector('a'), e[0]];
}

function moveToFront(text) {
    var tab = getTab(text);
    if (tab == undefined)
        return;
    var newtab = tab[0].cloneNode(true);

    var wrapper = tab[1].parentElement;
    wrapper.removeChild(tab[1]);
    wrapper.prepend(newtab);
}

function removeTab(text) {
    getTab(text).style.display = 'none';
}

var isDone = 0 // stopper flag. we unbind the event, but before we can do that, it already is fired 4 times, so this additional flag makes sure we only fix tabs once
function fixTabs()
{
    if (isDone) return isDone = 1;

    moveToFront('Maps');
    moveToFront('Videos');
    moveToFront('Images');
/*
    moveToFront('News');
    moveToFront('Books');
    moveToFront('Apps');
    moveToFront('Shopping');
    moveToFront('Flights');
    moveToFront('GitHub');
    moveToFront('Examples');

    moveToFront('More');
    */
}

function delegate(el, evt, sel, handler) {
    el.addEventListener(evt, function(event) {
        var t = event.target;
        while (t && t !== this) {
            if (t.matches(sel)) {
                handler.call(t, event);
            }
            t = t.parentNode;
        }
    });
}

(function() {
    delegate(document.getElementById('cnt'), "DOMNodeInserted", 'div > [role=navigation]', function(event) {
        var elem = document.getElementById('cnt');
        elem.replaceWith(elem.cloneNode(true)); // cheap way of removing all events. otherwise moveToFront would cause this event to trigger again
        setTimeout(fixTabs, 50); // sadly, when firing right away, the tabs elements have not been yet inserted fully
    });
})();