Display Google Search Options

Display Google Search Options on search results page

  1. // ==UserScript==
  2. // @name Display Google Search Options
  3. // @description Display Google Search Options on search results page
  4. // @author Betty
  5. // @namespace https://github.com/BettyJJ
  6. // @version 0.1
  7. // @include http://*.google.*/search*
  8. // @include https://*.google.*/search*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // Create the toolbar container
  16. const toolbar = document.createElement('div');
  17. toolbar.style.display = 'flex';
  18. toolbar.style.flexDirection = 'column';
  19. toolbar.style.margin = '20px 0px 0px 20px';
  20. toolbar.style.gap = '10px';
  21. toolbar.style.fontSize = '14px';
  22.  
  23. // Add the toolbar below the search box
  24. const header = document.querySelector('#before-appbar');
  25. if (header) {
  26. header.insertAdjacentElement('afterend', toolbar);
  27. }
  28.  
  29. // Helper to update URL
  30. function updateURL(newParams) {
  31. const url = new URL(window.location.href);
  32. for (const key in newParams) {
  33. if (newParams[key] === null) {
  34. url.searchParams.delete(key);
  35. } else {
  36. url.searchParams.set(key, newParams[key]);
  37. }
  38. }
  39. window.location.href = url.toString();
  40. }
  41.  
  42. // Highlight active button
  43. function isActiveButton(param, value) {
  44. const url = new URL(window.location.href);
  45. return url.searchParams.get(param) === value;
  46. }
  47.  
  48. // Create buttons
  49. function createButton(text, param, value) {
  50. const button = document.createElement('button');
  51. button.textContent = text;
  52. button.style.padding = '5px 10px';
  53. button.style.border = '1px solid #ccc';
  54. button.style.borderRadius = '4px';
  55. button.style.backgroundColor = isActiveButton(param, value) ? '#333' : '#f8f9fa';
  56. button.style.color = isActiveButton(param, value) ? '#fff' : '#000';
  57. button.style.cursor = 'pointer';
  58. button.addEventListener('click', () => {
  59. updateURL({ [param]: value });
  60. });
  61. return button;
  62. }
  63.  
  64. // Create two divs to hold the two groups of buttons
  65. const [lang_buttons, time_buttons] = ['div', 'div'].map(tag => {
  66. const element = document.createElement(tag);
  67. element.style.display = 'flex';
  68. element.style.flexWrap = 'nowrap';
  69. element.style.gap = '10px';
  70. element.style.alignItems = 'center';
  71. return element;
  72. });
  73. toolbar.append(lang_buttons, time_buttons);
  74.  
  75. // Language buttons
  76. lang_buttons.appendChild(document.createTextNode('语言:'));
  77. const languages = {
  78. '全部': null,
  79. '简中': 'lang_zh-CN',
  80. '英语': 'lang_en',
  81. };
  82. for (const [text, value] of Object.entries(languages)) {
  83. lang_buttons.appendChild(
  84. createButton(text, 'lr', value)
  85. );
  86. }
  87.  
  88. // Time buttons
  89. time_buttons.appendChild(document.createTextNode('时间:'));
  90. const times = {
  91. '全部': null,
  92. '一小时': 'qdr:h',
  93. '24 小时': 'qdr:d',
  94. '一周': 'qdr:w',
  95. '一个月': 'qdr:m',
  96. '三个月': 'qdr:m3',
  97. '六个月': 'qdr:m6',
  98. '一年': 'qdr:y',
  99. '三年': 'qdr:y3',
  100. };
  101. for (const [text, value] of Object.entries(times)) {
  102. time_buttons.appendChild(
  103. createButton(text, 'tbs', value)
  104. );
  105. }
  106. })();

QingJ © 2025

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