Reorder Google Categories

Reorganizes the google search categories to the wanted order

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reorder Google Categories
// @version      0.5
// @description  Reorganizes the google search categories to the wanted order
// @author       CoilBlimp
// @grant        none
// @include      http://*.google.com/search*
// @include      https://*.google.com/search*
// @include      https://*.google.*/search*
// @namespace https://greasyfork.org/users/392376
// ==/UserScript==
window.addEventListener('load', function() {
    let properOrganized = ["All", "Images", "Videos", "News", "Maps", "Books", "Shopping", "Flights", "Finance"],
        categoriesVisible = document.getElementById("hdtb-msb"),
        categoriesMore = document.getElementsByClassName("cF4V5c zriOQb UU8UAb gLSAk rShyOb")[0],
        categories = [];

    if(categoriesVisible === null || categoriesMore === null ){
        console.log("404 - Google categories not found");
        return;
    }

    let categoriesVisibleChildren = categoriesVisible.childNodes[0].childNodes[0].childNodes,
        categoriesMoreChildren = categoriesMore.childNodes,
		categoriesTools = categoriesVisible.childNodes[1];
	console.log(categoriesTools);

    categoriesVisibleChildren.forEach(function(item) {
        if(item.classList.contains("hdtb-msel")){
          	categories.push([item.innerText, item]);
        }
        else{
          	categories.push([item.firstChild.innerText, item]);
        }
    });
    categoriesMoreChildren.forEach(function(item) {
      	let inner = item.firstChild.firstChild.cloneNode(true);
      	inner.classList.remove("znKVS");
      	inner.classList.remove("tnhqA");
      	let outerDiv = document.createElement("div");
      	outerDiv.className = "hdtb-mitem hdtb-imb";
      	outerDiv.appendChild(inner);
        categories.push([item.innerText, outerDiv]);
    });
	console.log(categories)

    let result = [];

    properOrganized.forEach(function(key) {
        let found = false;
        categories.filter(function(item) {
			console.log(item[0]);
			console.log(key);
            if(!found && item[0] == key) {
                result.push(item);
                found = true;
				return false;
            }
			else{
				return true;
			}
        });
    });

	console.log(result);

    while (categoriesVisible.firstChild) {
        categoriesVisible.removeChild(categoriesVisible.firstChild);
    };

    result.forEach(function(item) {
      categoriesVisible.appendChild(item[1]);
    });

	categoriesVisible.appendChild(categoriesTools);
}, false);