DuckDuckGo Middle Click Search v.2.1

Middle clicking the search Button on DuckDucjGo search icon opens the results in a new tab. Fork of "Youtube Middle Click Search"v. 2.0.5 Kufii (Adrien Pyke?) without using wait-for-elements/wait-for-elements.js

  1. // ==UserScript==
  2. // @name DuckDuckGo Middle Click Search v.2.1
  3. // @author Decembre
  4. // @namespace https://gf.qytechs.cn/users/8
  5. // @version 2.1.0
  6. // @description Middle clicking the search Button on DuckDucjGo search icon opens the results in a new tab. Fork of "Youtube Middle Click Search"v. 2.0.5 Kufii (Adrien Pyke?) without using wait-for-elements/wait-for-elements.js
  7. // @icon https://external-content.duckduckgo.com/ip3/duckduckgo.com.ico
  8. // @include https://duckduckgo.com/*
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. /* ===========
  13. If you want fork it for an other Site, watch in the code, the 3:
  14. // ConFig DuckDuckGo
  15.  
  16. This code completes the event listener attachment for the search suggestions.
  17. It checks if the target element is not the remove button,
  18. and if so, it constructs the search URL and navigates to the search results page
  19. or opens the results in a new tab depending on the mouse button clicked.
  20. If the target element is the remove button and the middle mouse button is clicked, i
  21. t prevents the default behavior and stops the event propagation.
  22. ========== */
  23.  
  24. // Define an anonymous function to encapsulate the script's functionality
  25. (function () {
  26. 'use strict'; // Enable strict mode for better error handling
  27.  
  28. // Define the script's name for logging purposes
  29. const SCRIPT_NAME = 'YMCS';
  30.  
  31. // Define a utility object with various helper functions
  32. const Util = {
  33. // Log a message to the console with the script's name
  34. log(...args) {
  35. args.unshift(`%c${SCRIPT_NAME}:`, 'font-weight: bold;color: #233c7b;');
  36. console.log(...args);
  37. },
  38. // Query the DOM for an element using a CSS selector
  39. q(query, context = document) {
  40. return context.querySelector(query);
  41. },
  42. // Query the DOM for multiple elements using a CSS selector
  43. qq(query, context = document) {
  44. return Array.from(context.querySelectorAll(query));
  45. },
  46. // Get a query parameter from the current URL
  47. getQueryParameter(name, url = window.location.href) {
  48. name = name.replace(/[[\]]/gu, '\\$&');
  49. const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`, 'u'),
  50. results = regex.exec(url);
  51. if (!results) return null;
  52. if (!results[2]) return '';
  53. return decodeURIComponent(results[2].replace(/\+/gu, ' '));
  54. },
  55. // Encode a URI with plus signs
  56. encodeURIWithPlus(string) {
  57. return encodeURIComponent(string).replace(/%20/gu, '+');
  58. }
  59. };
  60.  
  61. // Define a function to wait for an element to be available in the DOM
  62. function waitForElems(options) {
  63. // Extract the options object's properties
  64. const { sel, onmatch, stop } = options;
  65. // Create an interval to check for the element's availability
  66. const intervalId = setInterval(() => {
  67. // Query the DOM for the element using the provided selector
  68. const elem = document.querySelector(sel);
  69. // If the element is found, call the onmatch function and clear the interval if stop is true
  70. if (elem) {
  71. onmatch(elem);
  72. if (stop) {
  73. clearInterval(intervalId);
  74. }
  75. }
  76. }, 100); // Check every 100ms
  77. }
  78.  
  79. // ConFig DuckDuckGo Search Button = .is-link-style-exp .search__button
  80. // Wait for the search button to be available and attach event listeners
  81. waitForElems({
  82. // Selector for the search button
  83. sel: '.is-link-style-exp .search__button',
  84. // Stop checking for the element once it's found
  85. stop: true,
  86. // Function to call when the element is found
  87. onmatch(btn) {
  88. // Prevent default behavior on middle click
  89. btn.onmousedown = function (e) {
  90. if (e.button === 1) {
  91. e.preventDefault();
  92. }
  93. };
  94. // Attach an event listener for clicks
  95. btn.onclick = function (e) {
  96. // Prevent default behavior
  97. e.preventDefault();
  98. e.stopImmediatePropagation();
  99.  
  100.  
  101. // DuckDuckGo Search InPut = #search_form_input
  102. // Get the search input value
  103. const input = Util.q('#search_form_input').value.trim();
  104. // If the input is empty, do nothing
  105. if (!input) return false;
  106. // DuckDuckGo Search URL
  107. // Construct the search URL = ?q=
  108. const url = `${location.origin}?q=${Util.encodeURIWithPlus(input)}`;
  109. // If the middle mouse button was clicked, open the results in a new tab
  110. if (e.button === 1) {
  111. GM_openInTab(url, true);
  112. } else if (e.button === 0) {
  113. // Otherwise, navigate to the search results page
  114. window.location.href = url;
  115. }
  116.  
  117. return false;
  118. };
  119. // Attach an event listener for aux clicks (middle click)
  120. btn.onauxclick = btn.onclick;
  121. }
  122. });
  123.  
  124. // Wait for the search suggestions to be available and attach event listeners
  125. waitForElems({
  126. // Selector for the search suggestions
  127. sel: '.sbsb_c',
  128. // Function to call when the element is found
  129. onmatch(result) {
  130. // Attach an event listener for clicks
  131. result.onclick = function (e) {
  132. // If the target element is not the remove button
  133. if (!e.target.classList.contains('sbsb_i')) {
  134. // Get the search suggestion text
  135. const search = Util.q('.sbpqs_a, .sbqs_c', result).textContent;
  136.  
  137. // Construct the search URL
  138. const url = `${location.origin}/search/${Util.encodeURIWithPlus(search)}`;
  139. // If the middle mouse button was clicked, open the results in a new tab
  140. if (e.button === 1) {
  141. GM_openInTab(url, true);
  142. } else if (e.button === 0) {
  143. // Otherwise, navigate to the search results page
  144. window.location.href = url;
  145. }
  146. } else if (e.button === 1) {
  147. // Prevent opening in new tab if they middle click the remove button
  148. e.preventDefault();
  149. e.stopImmediatePropagation();
  150. }
  151. };
  152. // Attach an event listener for aux clicks (middle click)
  153. result.onauxclick = result.onclick;
  154. }
  155. });
  156. })();
  157.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址