🏠 返回首頁 

Greasy Fork is available in English.

Google & YouTube - Universal Exact Search Hotkey

Surround selected text with quotation marks using NumpadAdd key


安装此脚本?
  1. // ==UserScript==
  2. // @name Google & YouTube - Universal Exact Search Hotkey
  3. // @namespace GYESH
  4. // @version 6.0
  5. // @description Surround selected text with quotation marks using NumpadAdd key
  6. // @match *://*/*
  7. // @exclude https://www.google.ca/maps/*
  8. // @run-at document-start
  9. // @grant none
  10. // @author drhouse
  11. // @license CC-BY-NC-SA-4.0
  12. // @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
  13. // ==/UserScript==
  14. (function() {
  15. 'use strict';
  16. function surroundWithQuotes(element) {
  17. if (element.isContentEditable) {
  18. const selection = window.getSelection();
  19. const range = selection.getRangeAt(0);
  20. const selectedText = range.toString();
  21. if (selectedText) {
  22. const quotedText = `"${selectedText}"`;
  23. range.deleteContents();
  24. range.insertNode(document.createTextNode(quotedText));
  25. range.setStart(range.startContainer, range.startOffset + 1);
  26. range.setEnd(range.endContainer, range.endOffset - 1);
  27. selection.removeAllRanges();
  28. selection.addRange(range);
  29. }
  30. } else {
  31. const start = element.selectionStart;
  32. const end = element.selectionEnd;
  33. const selectedText = element.value.substring(start, end);
  34. if (selectedText) {
  35. const quotedText = `"${selectedText}"`;
  36. element.value = element.value.substring(0, start) + quotedText + element.value.substring(end);
  37. element.selectionStart = start + 1;
  38. element.selectionEnd = start + quotedText.length - 1;
  39. }
  40. }
  41. }
  42. function handleKeyPress(event) {
  43. if ((event.key === '+' && event.location === KeyboardEvent.DOM_KEY_LOCATION_NUMPAD) ||
  44. (event.key === 'Add' && event.location === KeyboardEvent.DOM_KEY_LOCATION_NUMPAD)) {
  45. let activeElement = document.activeElement;
  46. // Special handling for Google search
  47. if (window.location.hostname.includes('google.com')) {
  48. activeElement = document.querySelector('input[name="q"]') || activeElement;
  49. }
  50. if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA' || activeElement.isContentEditable) {
  51. surroundWithQuotes(activeElement);
  52. event.preventDefault();
  53. event.stopPropagation();
  54. }
  55. }
  56. }
  57. function initScript() {
  58. document.addEventListener('keydown', handleKeyPress, true);
  59. // Mutation observer to handle dynamically added elements
  60. const observer = new MutationObserver((mutations) => {
  61. mutations.forEach((mutation) => {
  62. if (mutation.type === 'childList') {
  63. mutation.addedNodes.forEach((node) => {
  64. if (node.nodeType === Node.ELEMENT_NODE) {
  65. if (node.tagName === 'INPUT' || node.tagName === 'TEXTAREA' || node.isContentEditable) {
  66. node.addEventListener('keydown', handleKeyPress, true);
  67. }
  68. }
  69. });
  70. }
  71. });
  72. });
  73. observer.observe(document.body, { childList: true, subtree: true });
  74. }
  75. if (document.readyState === 'loading') {
  76. document.addEventListener('DOMContentLoaded', initScript);
  77. } else {
  78. initScript();
  79. }
  80. })();