Click to Search

Adds a clickable button to SE search

  1. // ==UserScript==
  2. // @name Click to Search
  3. // @author Cameron Bernhardt (AstroCB)
  4. // @version 3.0.1
  5. // @namespace http://github.com/AstroCB
  6. // @description Adds a clickable button to SE search
  7. // @include http://*.stackexchange.com/*
  8. // @include http://stackoverflow.com/*
  9. // @include http://meta.stackoverflow.com/*
  10. // @include http://serverfault.com/*
  11. // @include http://meta.serverfault.com/*
  12. // @include http://superuser.com/*
  13. // @include http://meta.superuser.com/*
  14. // @include http://askubuntu.com/*
  15. // @include http://meta.askubuntu.com/*
  16. // @include http://stackapps.com/*
  17. // ==/UserScript==
  18. if (localStorage) {
  19. if (!localStorage.hasAsked) {
  20. var button = prompt("Would you like the search button to be a link (like the review and help links) or a button (like the 'Post Your Answer' button)? (type \"button\" or \"link\" without the quotes)").toLowerCase();
  21. localStorage.hasAsked = "true";
  22. if (button === "button") {
  23. localStorage.preference = "button";
  24. } else {
  25. localStorage.preference = "link";
  26. }
  27. window.location.reload();
  28. } else {
  29. var either = localStorage.preference;
  30. var reset = document.createElement("a");
  31. reset.setAttribute("href", "#");
  32. reset.setAttribute("onclick", "localStorage.removeItem('hasAsked'); alert('Your search button preferences have been reset. Click OK to reset the page and enter your new preferences.'); window.location.reload();");
  33. reset.textContent = "reset";
  34. document.getElementsByClassName("top-footer-links")[0].insertBefore(reset, document.getElementsByClassName("top-footer-links")[0].children[0]);
  35.  
  36. document.getElementById("search").children[0].placeholder = "";
  37.  
  38. if (either === "button") {
  39. button();
  40. } else {
  41. link();
  42. }
  43. }
  44. } else {
  45. link();
  46. }
  47.  
  48. function link() {
  49. var div = document.createElement("div");
  50. var span = document.createElement("span");
  51. var link = document.createElement("a");
  52.  
  53. div.setAttribute("class", "links-container");
  54. span.setAttribute("class", "topbar-menu-links");
  55. link.setAttribute("href", "#");
  56. link.setAttribute("onclick", "document.getElementById('search').submit();");
  57. link.textContent = "search";
  58.  
  59. div.appendChild(span);
  60. span.appendChild(link);
  61.  
  62. document.getElementById("search").appendChild(div);
  63. }
  64.  
  65. function button() {
  66. var button = document.createElement("input");
  67. button.setAttribute("type", "submit");
  68. button.setAttribute("value", "search");
  69. document.getElementById("search").appendChild(button);
  70. }