Google RHS Order

1/9/2024, 11:19:38 PM

目前为 2024-01-09 提交的版本。查看 最新版本

// ==UserScript==
// @name        Google RHS Order
// @namespace   Violentmonkey Scripts
// @match       https://www.google.*/*
// @grant       none
// @version     1.0
// @author      Benature
// @license     MIT
// @description 1/9/2024, 11:19:38 PM
// ==/UserScript==

(function() {
    'use strict';

    // 自定义排序函数
    function customSort(a, b) {
        const substrings = ['Obsidian', 'cubox', 'diigo', 'c4g', 'monica'];
        // 获取子元素的ID
        var aId = a.id;
        var bId = b.id;

        // 检查子元素的ID是否包含子字符串
        var aMatches = substrings.filter(sub => aId.includes(sub));
        var bMatches = substrings.filter(sub => bId.includes(sub));
        console.log(a, b)
        console.log(aMatches, bMatches)

        // 如果a和b都包含子字符串,比较子字符串的索引
        if (aMatches.length && bMatches.length) {
            var aIndex = substrings.indexOf(aMatches[0]);
            var bIndex = substrings.indexOf(bMatches[0]);

            // 如果索引相同,比较ID的自然顺序
            if (aIndex === bIndex) {
                return aId.localeCompare(bId);
            }
            return aIndex - bIndex;
        }

        // 如果a包含子字符串而b不包含,a在前
        if (aMatches.length && !bMatches.length) {
            return -1;
        }

        // 如果b包含子字符串而a不包含,b在前
        if (!aMatches.length && bMatches.length) {
            return 1;
        }

        // 如果都不包含子字符串,比较ID的自然顺序
        return aId.localeCompare(bId);
    }



    function sortChildrenById(containerId) {
        // 获取包含子元素的div
        var container = document.getElementById(containerId);

        // 检查div是否存在
        if (container) {
            // 获取所有具有id属性的子元素
            // var childrenWithId = Array.from(container.querySelectorAll('[id]'));
            var childrenWithId = Array.from(container.children).filter(child => child.id); // 只获取有id的子元素

            // 移除所有子元素
            container.innerHTML = '';

            // 根据ID对子元素进行排序
            childrenWithId.sort(customSort);

            // 重新插入排序后的子元素
            childrenWithId.forEach(function(child) {
                container.appendChild(child);
            });
        }
    }

    // 等待DOM完全加载后再运行排序函数
    function waitForDomReady() {
        // 检查DOM是否已经加载完成
        if (document.readyState === 'complete') {
            // 如果DOM已经加载完成,执行排序函数
            sortChildrenById('rhs');
        } else {
            // 如果DOM还未加载完成,等待100ms后再次检查
            setTimeout(waitForDomReady, 100);
        }
    }

    // 开始等待DOM加载
    waitForDomReady();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址