DuckDuckGoogle

Try to search DuckDuckGo, and if it fails, use Google instead.

  1. // ==UserScript==
  2. // @name DuckDuckGoogle
  3. // @description Try to search DuckDuckGo, and if it fails, use Google instead.
  4. // @author ForgottenUmbrella
  5. // @namespace https://gf.qytechs.cn/users/83187
  6. // @version 1.0.5
  7. // @match *://duckduckgo.com/*
  8. // ==/UserScript==
  9. // Return a URL for searching Google.
  10. function googleSearchUrl(query) {
  11. const base = "https://www.google.com";
  12. return `${base}/search?q=${query}`;
  13. }
  14. // Return the search query on DuckDuckGo.
  15. function ddgQuery(searchUrl) {
  16. const params = new URLSearchParams(searchUrl);
  17. return params.get("q");
  18. }
  19. // Return whether Firefox has encountered a proxy error.
  20. function firefoxProxyError() {
  21. const errors = document.getElementsByClassName("error-message");
  22. return errors.length !== 0;
  23. }
  24. // Return whether Chrome has encountered a privacy error.
  25. function chromePrivacyError() {
  26. const isNewTabPage = document.getElementById("one-google") !== null;
  27. // Chrome loads the new tab page when a privacy error occurs, so
  28. // detect whether the current page is the new tab page and return it.
  29. return isNewTabPage;
  30. }
  31. // Return whether the browser is Chrome.
  32. function isChrome() {
  33. return window.chrome !== undefined;
  34. }
  35. // Navigate to a URL.
  36. function goto(url) {
  37. location.href = url;
  38. }
  39. (() => {
  40. const failed = isChrome() ? chromePrivacyError() : firefoxProxyError();
  41. if (failed) {
  42. goto(googleSearchUrl(encodeURIComponent(ddgQuery(location.search))));
  43. }
  44. })();

QingJ © 2025

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