Stack Overflow on Google Search

Adds a button to search stackoverflow via Google Search

当前为 2023-08-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Stack Overflow on Google Search
  3. // @version 2.0.5
  4. // @description Adds a button to search stackoverflow via Google Search
  5. // @author Alexyoe
  6. // @namespace https://github.com/Alexyoe/stackoverflow-search-on-google.git
  7. // @include http*://www.google.*/search*
  8. // @include http*://google.*/search*
  9. // @run-at document-end
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. // Settings
  14. const iconVisible = true; // Toggle icon visibility
  15. const nameVisible = true; // Toggle name visibility
  16. const btnPosition = "end"; // Start or End
  17. const fixSize = false; // Expands the search buttons bar
  18.  
  19. // Start Code
  20. const queryRegex = /q=[^&]+/g;
  21. const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g;
  22. const stackoverflowUrl = "+site%3Astackoverflow.com";
  23. let stackoverflowIcon =
  24. '<svg class="DCxYpf" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 384 512"><path d="M290.7 311L95 269.7 86.8 309l195.7 41zm51-87L188.2 95.7l-25.5 30.8 153.5 128.3zm-31.2 39.7L129.2 179l-16.7 36.5L293.7 300zM262 32l-32 24 119.3 160.3 32-24zm20.5 328h-200v39.7h200zm39.7 80H42.7V320h-40v160h359.5V320h-40z"/></svg>';
  25. const isImageSearch = /[?&]tbm=isch/.test(location.search);
  26.  
  27. // Allow importing SVGs
  28. if (typeof trustedTypes !== "undefined") {
  29. const policy = trustedTypes.createPolicy("html", {
  30. createHTML: (input) => input,
  31. });
  32. stackoverflowIcon = policy.createHTML(stackoverflowIcon);
  33. }
  34.  
  35. // Main Function: Runs on load
  36. (function () {
  37. // Create the main link element
  38. const el = document.createElement("a");
  39. el.className = isImageSearch ? "NZmxZe" : "nPDzT T3FoJb";
  40.  
  41. // Create the div element for the text
  42. const mainDiv = document.createElement("div");
  43. mainDiv.className = "GKS7s";
  44.  
  45. // Create the span to wrap the icon and title
  46. const span = document.createElement("span");
  47. span.style.cssText = "display:inline-flex;gap:5px;";
  48. span.className = isImageSearch ? "m3kSL" : "FMKtTb UqcIvb";
  49.  
  50. // create the div to hold our SVG
  51. const iconDiv = document.createElement("div");
  52. iconDiv.style.cssText = nameVisible
  53. ? "height:16px;width:16px;display:block;fill:white;"
  54. : "height:16px;width:16px;display:block;margin:auto;fill:white;";
  55. iconDiv.innerHTML = stackoverflowIcon;
  56.  
  57. // Create the text node to hold the button title
  58. const textNode = document.createTextNode("Stack Overflow");
  59. // Add iconDiv to the span element
  60. if (iconVisible) {
  61. span.appendChild(iconDiv);
  62. }
  63. // Add textNode to the span element
  64. if (nameVisible) {
  65. if (!isImageSearch) {
  66. span.appendChild(textNode);
  67. }
  68. }
  69.  
  70. // Add span to the mainDiv
  71. mainDiv.appendChild(span);
  72. // Add mainDiv to the main link element
  73. el.appendChild(isImageSearch ? span : mainDiv);
  74. // Add text node last if isImageSearch is true
  75. if (isImageSearch) {
  76. el.appendChild(textNode);
  77. }
  78.  
  79. // Add site:stackoverflow.com to the query
  80. el.href = window.location.href.replace(queryRegex, (match) =>
  81. match.search(siteRegex) >= 0
  82. ? match.replace(siteRegex, stackoverflowUrl)
  83. : match + stackoverflowUrl
  84. );
  85.  
  86. // Insert the link into Google search
  87. if (isImageSearch) {
  88. let menuBar = document.querySelector(".T47uwc");
  89. menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]);
  90. } else {
  91. let menuBar = document.querySelectorAll(".IUOThf")[0];
  92. switch (btnPosition) {
  93. case "start":
  94. menuBar.insertBefore(el, menuBar.children[0]);
  95. break;
  96. case "end":
  97. menuBar.appendChild(el);
  98. break;
  99. default:
  100. menuBar.appendChild(el);
  101. break;
  102. }
  103. }
  104.  
  105. // Fix Sizing
  106. if (fixSize) {
  107. const buttonBox = document.querySelector(".xhjkHe");
  108. buttonBox.style.maxWidth = "inherit";
  109. }
  110. })();