Fast Search

Quickly search various sites using custom shortcuts.

当前为 2024-09-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fast Search
  3. // @namespace fast-search
  4. // @version 0.1.3
  5. // @description Quickly search various sites using custom shortcuts.
  6. // @match *://*/*
  7. // @icon https://th.bing.com/th/id/OIG4.Zgw8Ep_gbQoBnQO33DyS?pid=ImgGn
  8. // @grant GM_notification
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // Define search engine shortcuts and URLs
  16. const searchEngines = {
  17. "a": "https://www.amazon.com/s?k=",
  18. "b": "https://www.bing.com/search?q=",
  19. "d": "https://duckduckgo.com/?q=",
  20. "gf": "https://greasyfork.org/en/scripts?q=",
  21. "gh": "https://github.com/search?q=",
  22. "gi": "https://www.google.com/search?tbm=isch&q=",
  23. "g": "https://www.google.com/search?q=",
  24. "gs": "https://scholar.google.com/scholar?q=",
  25. "li": "https://www.linkedin.com/search/results/all/?keywords=",
  26. "r": "https://www.reddit.com/search/?q=",
  27. "so": "https://stackoverflow.com/search?q=",
  28. "t": "https://www.twitch.tv/search?term=",
  29. "w": "https://en.wikipedia.org/w/index.php?search=",
  30. "x": "https://twitter.com/search?q=",
  31. "y": "https://www.youtube.com/results?search_query=",
  32. "tk": "https://www.tiktok.com/search?q=",
  33. "f": "https://www.facebook.com/search/top/?q=",
  34. "i": "https://www.instagram.com/explore/tags/",
  35. "p": "https://www.pinterest.com/search/pins/?q=",
  36. "tu": "https://www.tumblr.com/search/",
  37. "q": "https://www.quora.com/search?q=",
  38. "fl": "https://www.flickr.com/search/?text=",
  39. "sc": "https://soundcloud.com/search?q="
  40. };
  41.  
  42. // Notify user with a message
  43. function notifyUser(message) {
  44. if (!('Notification' in window)) {
  45. console.log('Browser does not support notifications');
  46. return;
  47. }
  48. if (Notification.permission === 'granted') {
  49. new Notification(message);
  50. } else if (Notification.permission !== 'denied') {
  51. Notification.requestPermission().then(permission => {
  52. if (permission === 'granted') {
  53. new Notification(message);
  54. }
  55. });
  56. }
  57. }
  58.  
  59. // Check if current element is an input or editable
  60. function isFocusInEditable() {
  61. const el = document.activeElement;
  62. return el.tagName.toLowerCase() === 'input' ||
  63. el.tagName.toLowerCase() === 'textarea' ||
  64. (el.tagName.toLowerCase() === 'div' && el.contentEditable === 'true');
  65. }
  66.  
  67. // Perform the search
  68. function performSearch() {
  69. const userInput = prompt("Enter search command:");
  70. if (!userInput) {
  71. notifyUser("Search canceled.");
  72. return;
  73. }
  74. const [shortcut, ...queryParts] = userInput.trim().split(" ");
  75. const query = queryParts.join(" ");
  76.  
  77. // Default to Google if shortcut is not found
  78. const baseUrl = searchEngines[shortcut] || searchEngines["g"];
  79. const searchUrl = query ? `${baseUrl}${encodeURIComponent(query)}` : `${searchEngines["g"]}${encodeURIComponent(shortcut)}`;
  80.  
  81. notifyUser("Redirecting to your search...");
  82. window.location.href = searchUrl;
  83. }
  84.  
  85. // Initialize script
  86. function init() {
  87. console.log('Fast Search script initialized');
  88. document.addEventListener('keydown', event => {
  89. if (event.key === 'Insert' && !isFocusInEditable()) {
  90. performSearch();
  91. }
  92. }, true);
  93. }
  94.  
  95. init();
  96. })();