altsearchrefactored

performs search on other sites

  1. // ==UserScript==
  2. // @name altsearchrefactored
  3. // @namespace https://greasyfork.org/users/9631
  4. // @version 2020.05.11
  5. // @description performs search on other sites
  6. // @author jccalhoun
  7. // @match *://*.bing.com/*
  8. // @match *://*.duckduckgo.com/*
  9. // @match *://*.swagbucks.com/*
  10. // @match *://*.google.com/*
  11. // @run-at document-idle
  12. // @grant GM_addStyle
  13. // ==/UserScript==
  14. //for bing if the .dropdown is position:relative the menu hides behind the content. taking it out seems not to matter.
  15. //this is from https://www.w3schools.com/Css/css_dropdowns.asp
  16. //universal styles:
  17.  
  18. GM_addStyle(".dropbtn {border: none; cursor: pointer;}" +
  19. ".dropdown {display: inline-block;}" +
  20. ".dropdown-content {display: none; position: absolute; background-color: #fff !important; z-index: 1; box-shadow: 0 3px 5px rgba(0,0,0,0.19), 0 1px 1px rgba(0,0,0,0.23);}" +
  21. ".dropdown-content a {color: 666; padding: 12px 16px !important; text-decoration: none; display: block !important;}" +
  22. ".dropdown:hover .dropdown-content {display: block;}" +
  23. ".dropdown-content a:hover {background-color: #f2f2f2;}");
  24. // from http://greasemonkey.win-start.de/patterns/add-css.html
  25. function addSiteStyle(css) {
  26. var head, style;
  27. head = document.getElementsByTagName('head')[0];
  28. if (!head) {
  29. return;
  30. }
  31. style = document.createElement('style');
  32. style.type = 'text/css';
  33. style.innerHTML = css;
  34. head.appendChild(style);
  35. }
  36. var siteURL = window.location.hostname;
  37. //unique to bing:
  38. /*addSiteStyle(".dropbtn {background-color: #fff !important; font-family: Arial, Helvetica, sans-serif; color: #444444; font-size: 11px !important; line-height: 30px !important;}"+
  39. ".dropdown:hover .dropbtn {background-color: #3e8e41;}"); */
  40. var selectorGetter;
  41. switch (siteURL) {
  42. case "www.bing.com":
  43. addSiteStyle(".dropbtn {background-color: #fff !important; font-family: Arial, Helvetica, sans-serif; color: #444444; font-size: 11px !important; line-height: 30px !important; text-transform: uppercase;}" + ".dropdown:hover .dropbtn {background-color: #3e8e41;}");
  44. console.log("switch works");
  45. selectorGetter = document.querySelector('.b_scopebar li:nth-child(6)');
  46. //console.log(selectorGetter);
  47. break;
  48. case "duckduckgo.com":
  49. addSiteStyle(".zcm-wrap {position: static !important;}" + "li {list-style-type: none;}" + ".dropbtn {font-size: 14.4px; color: #666666; background-color: #fafafa; font-family: inherit; line-height: 37px; font-weight: 600; height: 37px;}" + ".dropbtn:hover {color:#333333;}" + ".dropdown {position:static !important;}");
  50. selectorGetter = document.querySelector('#duckbar_new');
  51. break;
  52. case "www.swagbucks.com":
  53. addSiteStyle(".dropbtn {background-color: #fff !important; color: #666666; font-family: 'Open Sans',sans-serif; font-size: 1.4em;}" + ".dropdown-content a {display: block !important;}" + ".dropdown:hover .dropbtn {color: #2d6cae;}");
  54. selectorGetter = document.querySelector("#tab_n");
  55. break;
  56. case "www.google.com":
  57. addSiteStyle(".dropbtn {background-color: #ffffff !important; color: #5f6368; font-family: 'Roboto'; line-height:16px; font-size: 13px;}" + ".dropbtn:hover {color: #1A73E8}" + ".dropdown {position: relative;}" + ".dropdown-content {background-color: #ffffff !important;}");
  58. selectorGetter = document.querySelector("#hdtb-msb-vis");
  59. break;
  60. }
  61.  
  62.  
  63. /* I will probably need to use something like the original alt search to get it to find the correct selector: //placeholder selector lists the different elements that the different sites have that i want to put stuff after. then the document.querySelector cycles through them until it fins the first match https://www.w3schools.com/jsref/met_document_queryselector.asp
  64.  
  65. var PLACEHOLDER_SELECTORS = '#resultStats, #b_results, .b-wordstat__text, .searchresults b, #ext_link, #sidebar, .b-global-wrapper, #gs_ab, #left, #zero_click_wrapper, bing-summary, .bing-summary, #bing-summary, #ucs';
  66. var results = document.querySelector(PLACEHOLDER_SELECTORS); */
  67.  
  68. //i'm not sure that addSearchElement needs to be a variable?
  69. var addSearchElement = 'searchListener';
  70.  
  71. var results = window.location.search.match(/(?:\?|&)q=([^&]*)/)[1];
  72.  
  73. //so all these are in a function currently but I think it should be fine to pull them out since that is how it is in the old alt search but the old one doesn't declare individual variables. Doing it individually could allow for not putting a link to the site you are already on (so a link to bing wouldn't show up if you were already on bing)
  74.  
  75.  
  76. var googleLink = '<a href =\"https://www.google.com/search?q=' + results + '\">Google</a>';
  77. var bingLink = "<a href =\"http://www.bing.com/search?q=" + results + "\">Bing</a>";
  78. var yahooLink = "<a href =\"http://search.yahoo.com/search?p=" + results + "\">Yahoo</a>";
  79. var swagLink = "<a href =\"http://www.swagbucks.com/?f=51&t=w&p=1&q=" + results + "\">Swagbucks</a>";
  80. var duckLink = "<a href =\"https://duckduckgo.com/?q=" + results + "\">DuckDuckGo</a>";
  81. var wolfLink = "<a href =\"http://www.wolframalpha.com/input/?i=" + results + "\">WolframAlpha</a>";
  82. var twitterLink = "<a href =\"http://twitter.com/search?q=" + results + "\">Twitter</a>";
  83. var scholarLink = "<a href =\"http://scholar.google.com/scholar?q=" + results + "\">Google Scholar</a>";
  84. var msAcademicLink = "<a href =\"https://academic.microsoft.com/search?q=" + results + "\">MS Academic</a>";
  85. var wikipedia = "<a href =\"https://www.bing.com/search?q=site%3Aen.wikipedia.org+" + results + "\">Wikipedia</a>";
  86. var reddit = "<a href =\"https://www.reddit.com/search?q=" + results + "\">Reddit</a>";
  87. var gitHub = "<a href =\"https://github.com/search?utf8=✓&q=" + results + "\">GitHub</a>";
  88.  
  89.  
  90. var altInsert = function () {
  91. //console.log(results);
  92. var newItem = document.createElement("li");
  93. newItem.id = addSearchElement;
  94. var links = '<div class="dropdown"> <button class="dropbtn">Alt Search</button> <div class="dropdown-content">' + googleLink + bingLink + yahooLink + swagLink + duckLink + wolfLink + twitterLink + scholarLink + msAcademicLink + wikipedia + reddit + gitHub + '</div></div>';
  95. newItem.innerHTML = links;
  96.  
  97. //var newItem2 = newItem.outerHTML;
  98. //bing updated to remove scopebar_pipe
  99. //var something = document.querySelector(".scopebar_pipe");
  100. // var something = document.querySelector('.b_scopebar li:nth-child(6)');
  101. //something.insertAdjacentHTML('beforebegin', newItem2);
  102. //so something.after(newItem2) results in the < being interpreted as less than &lt but it works with newItem. so do I need the outerHTML at all???
  103. //something.after(newItem2);
  104. //something.after(newItem);
  105.  
  106. selectorGetter.after(newItem);
  107. };
  108.  
  109. altInsert();
  110. // modified from sergio91pt script http://userscripts.org/users/122653
  111. var watchGoogleLInk = function () {
  112. // Whenever the query changes without changing the window href, our node
  113. // is removed, so use a MutationObserver to update and put us back.
  114. new MutationObserver(function (mutations) {
  115. var len = mutations.length;
  116. for (var i = 0; i < len; i++) {
  117. // Normally the link bar is removed then added, along
  118. // with search results, so just check additions.
  119. if (mutations[i].addedNodes) {
  120. if (!document.getElementById(addSearchElement)) {
  121. altInsert();
  122. }
  123. break;
  124. }
  125. }
  126. }).observe(document.body, {
  127. 'childList': true,
  128. 'subtree': true
  129. });
  130. };
  131.  
  132. if (window.location.host == "www.bing.com") {
  133. console.log("bing bottom baby!");
  134. var results2 = document.querySelector(".b_pag");
  135.  
  136. var other = document.createElement('div');
  137.  
  138. other.setAttribute("id", "altsearch");
  139. other.setAttribute("style", "width: 1000px; font-size: small; margin: 10px 10px 5px 159px;");
  140. var bottomLinks = "Try this search on " + googleLink + ", " + bingLink + ", " + yahooLink + ", " + swagLink + ", " + duckLink + ", " + wolfLink + ", " + twitterLink + ", " + scholarLink + ", " + msAcademicLink + ", " + wikipedia + ", " + reddit + ", " + gitHub;
  141. other.innerHTML = bottomLinks;
  142. //results2.parentNode.insertBefore(other, results2);
  143. results2.before(other);
  144. };
  145.  
  146. if (window.location.host == "www.google.com") {
  147. watchGoogleLInk();
  148. var icon = document.querySelector(".dropbtn");
  149. icon.insertAdjacentHTML("afterbegin", "<img alt='' width='16' height='16' src='data:image/svg+xml;base64,PHN2ZyBmb2N1c2FibGU9ImZhbHNlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZD0iTTE1LjUgMTRoLS43OWwtLjI4LS4yN0E2LjQ3MSA2LjQ3MSAwIDAgMCAxNiA5LjUgNi41IDYuNSAwIDEgMCA5LjUgMTZjMS42MSAwIDMuMDktLjU5IDQuMjMtMS41N2wuMjcuMjh2Ljc5bDUgNC45OUwyMC40OSAxOWwtNC45OS01em0tNiAwQzcuMDEgMTQgNSAxMS45OSA1IDkuNVM3LjAxIDUgOS41IDUgMTQgNy4wMSAxNCA5LjUgMTEuOTkgMTQgOS41IDE0eiIvPjwvc3ZnPg==' />");
  150. document.getElementById("searchListener").classList.add("hdtb-mitem");
  151. document.getElementById("searchListener").classList.add("hdtb-imb");
  152. };