Github search on Google

Adds a button to search github.com with Google

目前為 2023-08-02 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Github search on Google
  3. // @version 2.0.4
  4. // @description Adds a button to search github.com with Google
  5. // @author Alexyoe
  6. // @namespace https://github.com/Alexyoe/Github-on-Google-Search
  7. // @license MIT
  8. // @include http*://www.google.*/search*
  9. // @include http*://google.*/search*
  10. // @run-at document-end
  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 githubUrl = "+site%3Agithub.com";
  23. let githubIcon =
  24. '<svg class="DCxYpf" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"/></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. githubIcon = policy.createHTML(githubIcon);
  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 = githubIcon;
  56.  
  57. // Create the text node to hold the button title
  58. const textNode = document.createTextNode("Github");
  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:github.com to the query
  80. el.href = window.location.href.replace(queryRegex, (match) =>
  81. match.search(siteRegex) >= 0
  82. ? match.replace(siteRegex, githubUrl)
  83. : match + githubUrl
  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. })();