Advertisement
Python253

set_default_search_engine_in_chrome_full_menu

Mar 3rd, 2024 (edited)
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.98 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: set_default_search_engine_in_chrome_full_menu.py
  3. # Author: Jeoi Reqi
  4.  
  5. """
  6. Set Default Search Engine in Google Chrome
  7.  
  8. This Python script allows users to easily set their default search engine in Google Chrome from a menu of popular options.
  9. The script provides a user-friendly interface, presenting a menu with a selection of search engines such as Google, DuckDuckGo,
  10. Bing, Yandex, Baidu, WolframAlpha, and Ecosia.
  11.  
  12. Requirements:
  13. - Python 3
  14.  
  15. Usage:
  16. 1: Ensure Python 3 is installed on your system.
  17. 2: Copy and paste the script into a Python file.
  18. 3: Customize the search engine settings within the script if desired.
  19. 4: Run the script, and a menu will appear.
  20. 5: Enter the ID of the preferred search engine from the menu.
  21.  
  22. The script will update your Chrome preferences, setting the chosen search engine as the default.
  23.  
  24. Note: Make sure to run the script with the appropriate permissions, and modify the Chrome preferences file path if your Chrome installation directory differs.
  25. """
  26.  
  27. import os
  28. import json
  29.  
  30. def set_search_engine(search_engine_settings, engine_name):
  31.     # Path to the Chrome preferences file
  32.     PREFS_FILE = os.path.join(os.getenv('LOCALAPPDATA'), 'Google', 'Chrome', 'User Data', 'Default', 'Preferences')
  33.  
  34.     # Check if the Chrome preferences file exists
  35.     if not os.path.isfile(PREFS_FILE):
  36.         print("Error: Chrome preferences file not found")
  37.         exit(1)
  38.  
  39.     # Read current preferences from the file
  40.     with open(PREFS_FILE, 'r', encoding='utf-8') as file:
  41.         preferences = json.load(file)
  42.  
  43.     # Update preferences with the chosen search engine
  44.     preferences.update(search_engine_settings)
  45.  
  46.     # Write the updated preferences back to the file
  47.     with open(PREFS_FILE, 'w', encoding='utf-8') as file:
  48.         json.dump(preferences, file, indent=2)
  49.  
  50.     print(f"Success: {engine_name} has been set as your default search engine in Google Chrome")
  51.  
  52. def menu():
  53.     print("::[Select a search engine to set as default]::\n")
  54.     print("-------------------------------------------")
  55.     print("0: Google -------- Global #1 Search Engine\n")
  56.     print("1: DuckDuckGo ---- Engine Owned By Google\n")
  57.     print("2: Bing ---------- Microsoft's Search Engine\n")
  58.     print("3: Yandex -------- Russia's #1 Search Engine\n")
  59.     print("4: Baidu --------- China's #1 Search Engine\n")
  60.     print("5: WolframAlpha -- Computational Search Engine\n")
  61.     print("6: Ecosia -------- Plants Trees For Searches 🌳\n")
  62.  
  63.     choice = input("\nEnter the ID of the search engine you want to set as default: ")
  64.  
  65.     if choice == "0":
  66.         set_search_engine(GOOGLE_SEARCH_ENGINE, "Google")
  67.     elif choice == "1":
  68.         set_search_engine(DUCKDUCKGO_SEARCH_ENGINE, "DuckDuckGo")
  69.     elif choice == "2":
  70.         set_search_engine(BING_SEARCH_ENGINE, "Bing")
  71.     elif choice == "3":
  72.         set_search_engine(YANDEX_SEARCH_ENGINE, "Yandex")
  73.     elif choice == "4":
  74.         set_search_engine(BAIDU_SEARCH_ENGINE, "Baidu")
  75.     elif choice == "5":
  76.         set_search_engine(WOLFRAMALPHA_SEARCH_ENGINE, "WolframAlpha")
  77.     elif choice == "6":
  78.         set_search_engine(ECOSIA_SETTINGS, "Ecosia")
  79.     else:
  80.         print("Invalid choice. Please enter a valid search engine ID.")
  81.  
  82. # Search engine settings
  83. GOOGLE_SEARCH_ENGINE = {
  84.     "default_search_provider": {
  85.         "enabled": True,
  86.         "encodings": {"query": "UTF-8", "search_url": "UTF-8"},
  87.         "favicon_url": "https://www.google.com/favicon.ico",
  88.         "id": "0",
  89.         "image_url": "",
  90.         "image_url_post_params": "",
  91.         "instant_url": "",
  92.         "keyword": "google.com",
  93.         "name": "Google",
  94.         "search_url": "https://www.google.com/search?q={searchTerms}",
  95.         "suggest_url": ""
  96.     }
  97. }
  98.  
  99. DUCKDUCKGO_SEARCH_ENGINE = {
  100.     "default_search_provider": {
  101.         "enabled": True,
  102.         "encodings": {"query": "UTF-8", "search_url": "UTF-8"},
  103.         "favicon_url": "https://duckduckgo.com/favicon.ico",
  104.         "id": "1",
  105.         "image_url": "https://duckduckgo.com/assets/logo.icon.svg",
  106.         "image_url_post_params": "",
  107.         "instant_url": "",
  108.         "keyword": "duckduckgo.com",
  109.         "name": "DuckDuckGo",
  110.         "search_url": "https://duckduckgo.com/?q={searchTerms}",
  111.         "suggest_url": ""
  112.     }
  113. }
  114.  
  115. BING_SEARCH_ENGINE = {
  116.     "default_search_provider": {
  117.         "enabled": True,
  118.         "encodings": {"query": "UTF-8", "search_url": "UTF-8"},
  119.         "favicon_url": "https://www.bing.com/favicon.ico",
  120.         "id": "2",
  121.         "image_url": "https://www.bing.com/sa/simg/favicon-2x.ico",
  122.         "image_url_post_params": "",
  123.         "instant_url": "",
  124.         "keyword": "bing.com",
  125.         "name": "Bing",
  126.         "search_url": "https://www.bing.com/search?q={searchTerms}",
  127.         "suggest_url": ""
  128.     }
  129. }
  130.  
  131. YANDEX_SEARCH_ENGINE = {
  132.     "default_search_provider": {
  133.         "enabled": True,
  134.         "encodings": {"query": "UTF-8", "search_url": "UTF-8"},
  135.         "favicon_url": "https://yandex.com/favicon.ico",
  136.         "id": "3",  
  137.         "image_url": "https://yandex.com/images/logo-share.png",
  138.         "image_url_post_params": "",
  139.         "instant_url": "",
  140.         "keyword": "yandex.com",
  141.         "name": "Yandex",
  142.         "search_url": "https://yandex.com/search/?text={searchTerms}",
  143.         "suggest_url": ""
  144.     }
  145. }
  146.  
  147. BAIDU_SEARCH_ENGINE = {
  148.     "default_search_provider": {
  149.         "enabled": True,
  150.         "encodings": {"query": "UTF-8", "search_url": "UTF-8"},
  151.         "favicon_url": "https://www.baidu.com/favicon.ico",
  152.         "id": "4",
  153.         "image_url": "https://www.baidu.com/img/baidu_jgylogo3.gif",
  154.         "image_url_post_params": "",
  155.         "instant_url": "",
  156.         "keyword": "baidu.com",
  157.         "name": "Baidu",
  158.         "search_url": "https://www.baidu.com/s?wd={searchTerms}",
  159.         "suggest_url": ""
  160.     }
  161. }
  162.  
  163. WOLFRAMALPHA_SEARCH_ENGINE = {
  164.     "default_search_provider": {
  165.         "enabled": True,
  166.         "encodings": {"query": "UTF-8", "search_url": "UTF-8"},
  167.         "favicon_url": "https://www.wolframalpha.com/favicon.ico",
  168.         "id": "5",
  169.         "image_url": "https://www.wolframalpha.com/images/wa-share.png",
  170.         "image_url_post_params": "",
  171.         "instant_url": "",
  172.         "keyword": "wolframalpha.com",
  173.         "name": "WolframAlpha",
  174.         "search_url": "https://www.wolframalpha.com/input/?i={searchTerms}",
  175.         "suggest_url": ""
  176.     }
  177. }
  178.  
  179. ECOSIA_SETTINGS = {
  180.     "default_search_provider": {
  181.         "enabled": True,
  182.         "encodings": {"query": "UTF-8", "search_url": "UTF-8"},
  183.         "favicon_url": "https://www.ecosia.org/favicon.png",
  184.         "id": "6",
  185.         "image_url": "https://www.ecosia.org/assets/images/png/logo_1024.png",
  186.         "image_url_post_params": "",
  187.         "instant_url": "",
  188.         "keyword": "ecosia.org",
  189.         "name": "Ecosia",
  190.         "search_url": "https://www.ecosia.org/search?q={searchTerms}",
  191.         "suggest_url": ""
  192.     }
  193. }
  194.  
  195. if __name__ == "__main__":
  196.     menu()
  197.  
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement