Fast Search

Quickly search various sites using custom shortcuts.

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

  1. // ==UserScript==
  2. // @name Fast Search
  3. // @namespace fast-search
  4. // @version 0.1.1
  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 in alphabetical order
  16. const SEARCH_ENGINES = {
  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. };
  33.  
  34. // Notify user with a message
  35. function notifyUser(message) {
  36. if (!('Notification' in window)) {
  37. console.log('Browser does not support notifications');
  38. return;
  39. }
  40. if (Notification.permission === 'granted') {
  41. new Notification(message);
  42. } else if (Notification.permission !== 'denied') {
  43. Notification.requestPermission().then(permission => {
  44. if (permission === 'granted') {
  45. new Notification(message);
  46. }
  47. });
  48. }
  49. }
  50.  
  51. // Check if current element is an input or editable
  52. function isFocusInEditable() {
  53. const el = document.activeElement;
  54. return el.tagName.toLowerCase() === 'input' ||
  55. el.tagName.toLowerCase() === 'textarea' ||
  56. (el.tagName.toLowerCase() === 'div' && el.contentEditable === 'true');
  57. }
  58.  
  59. // Perform the search
  60. function performSearch() {
  61. const userInput = prompt("Enter search command:");
  62. if (!userInput) {
  63. notifyUser("Search canceled.");
  64. return;
  65. }
  66. const [shortcut, ...queryParts] = userInput.trim().split(" ");
  67. const query = queryParts.join(" ");
  68.  
  69. // Default to Google if shortcut is not found
  70. const baseUrl = SEARCH_ENGINES[shortcut] || SEARCH_ENGINES["g"];
  71. const searchUrl = query ? `${baseUrl}${encodeURIComponent(query)}` : `${SEARCH_ENGINES["g"]}${encodeURIComponent(shortcut)}`;
  72.  
  73. notifyUser("Redirecting to your search...");
  74. window.location.href = searchUrl;
  75. }
  76.  
  77. // Initialize script
  78. function init() {
  79. console.log('Fast Search script initialized');
  80. document.addEventListener('keydown', event => {
  81. if (event.key === 'Insert' && !isFocusInEditable()) {
  82. performSearch();
  83. }
  84. }, true);
  85. }
  86.  
  87. init();
  88. })();