Google RHS Order

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

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

  1. // ==UserScript==
  2. // @name Google RHS Order
  3. // @namespace Violentmonkey Scripts
  4. // @match https://www.google.*/*
  5. // @grant none
  6. // @version 1.0
  7. // @author Benature
  8. // @license MIT
  9. // @description 1/9/2024, 11:19:38 PM
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 自定义排序函数
  16. function customSort(a, b) {
  17. const substrings = ['Obsidian', 'cubox', 'diigo', 'c4g', 'monica'];
  18. // 获取子元素的ID
  19. var aId = a.id;
  20. var bId = b.id;
  21.  
  22. // 检查子元素的ID是否包含子字符串
  23. var aMatches = substrings.filter(sub => aId.includes(sub));
  24. var bMatches = substrings.filter(sub => bId.includes(sub));
  25. console.log(a, b)
  26. console.log(aMatches, bMatches)
  27.  
  28. // 如果a和b都包含子字符串,比较子字符串的索引
  29. if (aMatches.length && bMatches.length) {
  30. var aIndex = substrings.indexOf(aMatches[0]);
  31. var bIndex = substrings.indexOf(bMatches[0]);
  32.  
  33. // 如果索引相同,比较ID的自然顺序
  34. if (aIndex === bIndex) {
  35. return aId.localeCompare(bId);
  36. }
  37. return aIndex - bIndex;
  38. }
  39.  
  40. // 如果a包含子字符串而b不包含,a在前
  41. if (aMatches.length && !bMatches.length) {
  42. return -1;
  43. }
  44.  
  45. // 如果b包含子字符串而a不包含,b在前
  46. if (!aMatches.length && bMatches.length) {
  47. return 1;
  48. }
  49.  
  50. // 如果都不包含子字符串,比较ID的自然顺序
  51. return aId.localeCompare(bId);
  52. }
  53.  
  54.  
  55.  
  56. function sortChildrenById(containerId) {
  57. // 获取包含子元素的div
  58. var container = document.getElementById(containerId);
  59.  
  60. // 检查div是否存在
  61. if (container) {
  62. // 获取所有具有id属性的子元素
  63. // var childrenWithId = Array.from(container.querySelectorAll('[id]'));
  64. var childrenWithId = Array.from(container.children).filter(child => child.id); // 只获取有id的子元素
  65.  
  66. // 移除所有子元素
  67. container.innerHTML = '';
  68.  
  69. // 根据ID对子元素进行排序
  70. childrenWithId.sort(customSort);
  71.  
  72. // 重新插入排序后的子元素
  73. childrenWithId.forEach(function(child) {
  74. container.appendChild(child);
  75. });
  76. }
  77. }
  78.  
  79. // 等待DOM完全加载后再运行排序函数
  80. function waitForDomReady() {
  81. // 检查DOM是否已经加载完成
  82. if (document.readyState === 'complete') {
  83. // 如果DOM已经加载完成,执行排序函数
  84. sortChildrenById('rhs');
  85. } else {
  86. // 如果DOM还未加载完成,等待100ms后再次检查
  87. setTimeout(waitForDomReady, 100);
  88. }
  89. }
  90.  
  91. // 开始等待DOM加载
  92. waitForDomReady();
  93. })();

QingJ © 2025

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