{"id":19035243,"url":"https://github.com/omkarcloud/chrome-extension-python","last_synced_at":"2025-04-14T21:04:16.403Z","repository":{"id":219253059,"uuid":"748610229","full_name":"omkarcloud/chrome-extension-python","owner":"omkarcloud","description":"Chrome Extension Python allows you to easily integrate Chrome extensions in web automation frameworks like Botasaurus, Selenium, and Playwright.","archived":false,"fork":false,"pushed_at":"2024-05-17T05:20:55.000Z","size":15,"stargazers_count":23,"open_issues_count":1,"forks_count":7,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-11-08T21:49:44.109Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.omkar.cloud/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/omkarcloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["Chetan11-Dev"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2024-01-26T11:18:51.000Z","updated_at":"2024-11-05T17:36:57.000Z","dependencies_parsed_at":"2024-05-17T06:29:39.372Z","dependency_job_id":"f4ed7d75-163f-42da-94a7-3f93ac9b9c36","html_url":"https://github.com/omkarcloud/chrome-extension-python","commit_stats":null,"previous_names":["omkarcloud/chrome-extension-python"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omkarcloud%2Fchrome-extension-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omkarcloud%2Fchrome-extension-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omkarcloud%2Fchrome-extension-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omkarcloud%2Fchrome-extension-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omkarcloud","download_url":"https://codeload.github.com/omkarcloud/chrome-extension-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224885693,"owners_count":17386202,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T21:49:45.859Z","updated_at":"2025-04-14T21:04:16.391Z","avatar_url":"https://github.com/omkarcloud.png","language":"Python","funding_links":["https://github.com/sponsors/Chetan11-Dev"],"categories":[],"sub_categories":[],"readme":"# Chrome Extension Python\r\n\r\n`Chrome Extension Python` that allows you to easily integrate Chrome extensions in web automation frameworks like Botasaurus, Selenium, and Playwright. \r\n\r\nThis tool simplifies the process of downloading, configuring, and using any Chrome extension.\r\n\r\n## Installation\r\n\r\nInstall the package using pip:\r\n\r\n```bash\r\npython -m pip install chrome_extension_python\r\n```\r\n\r\n## Usage\r\n\r\nThis package allows the use of Chrome extensions in Botasaurus, Selenium, and Playwright frameworks.\r\n\r\nBelow are examples demonstrating the integration of the Adblock extension in each framework.\r\n\r\n### Usage with Botasaurus\r\n\r\n[Botasaurus](https://github.com/omkarcloud/botasaurus), a web scraping framework, integrates easily with `Chrome Extension Python`. Pass the Chrome Webstore link of the extension to use it.\r\n\r\nExample with Adblock Extension:\r\n\r\n```python\r\nfrom botasaurus.browser import browser, Driver\r\nfrom chrome_extension_python import Extension\r\n\r\n@browser(\r\n    extensions=[\r\n        # Simply pass the Chrome Extension Link\r\n        Extension(\r\n            \"https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom\"\r\n        )\r\n    ],\r\n)\r\ndef scrape_while_blocking_ads(driver: Driver, data):\r\n    driver.prompt()\r\n\r\nscrape_while_blocking_ads()\r\n```\r\n\r\n### Usage with Selenium\r\n\r\nIntegration with Selenium involves setting up Chrome options and adding the extension.\r\n\r\nExample:\r\n\r\n```python\r\nfrom selenium import webdriver\r\nfrom selenium.webdriver.chrome.options import Options\r\nfrom chromedriver_autoinstaller import install\r\nfrom chrome_extension_python import Extension\r\n\r\n# Set Chrome options\r\noptions = Options()\r\noptions.add_argument(Extension(\"https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom\").load())\r\n\r\n# Install and set up the driver\r\ndriver_path = install()\r\ndriver = webdriver.Chrome(driver_path, options=options)\r\n\r\n# Prompt for user input\r\ninput(\"Press Enter to exit...\")\r\n\r\n# Clean up\r\ndriver.quit()\r\n```\r\n\r\n### Usage with Playwright\r\n\r\nPlaywright integration includes specifying the extension path and launching the browser context with the extension.\r\n\r\nExample:\r\n\r\n```python\r\nfrom playwright.sync_api import sync_playwright\r\nfrom chrome_extension_python import Extension\r\nimport random\r\n\r\ndef generate_random_profile():\r\n    return str(random.randint(1, 1000))\r\n\r\nwith sync_playwright() as p:\r\n    extension_path = Extension(\"https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom\").load(with_command_line_option=False)\r\n    browser = p.chromium.launch_persistent_context(\r\n        user_data_dir=generate_random_profile(),\r\n        headless=False,\r\n        args=[\r\n            '--disable-extensions-except='+ extension_path,\r\n            '--load-extension=' + extension_path,\r\n        ],\r\n    )\r\n    page = browser.new_page()\r\n    input(\"Press Enter to exit...\")\r\n    browser.close()\r\n```\r\n\r\n## Configuring an Extension\r\n\r\nExtensions can be configured either\r\n\r\n- by editing their JavaScript files\r\n- or by interacting with their UI using Selenium.\r\n\r\n### Editing JavaScript Files\r\n\r\nThe JavaScript file editing method is faster and more robust. The following example demonstrates creating a configurable CapSolver Extension with an API key:\r\n\r\n```python\r\nfrom chrome_extension_python import Extension\r\n\r\nclass Capsolver(Extension):\r\n    def __init__(self, api_key):\r\n        # Initialize the Capsolver extension with given parameters\r\n        super().__init__(\r\n            extension_id=\"pgojnojmmhpofjgdmaebadhbocahppod\",  # Unique identifier for the Chrome Extension, found in the Chrome Webstore link\r\n            extension_name=\"capsolver\",  # The name assigned to the extension\r\n            api_key=api_key,  # An important custom parameter (API key) required for the extension's functionality\r\n        )\r\n\r\n    # This method is called to update the necessary JavaScript files within the extension\r\n    def update_files(self, api_key):\r\n        # Retrieve a list of all JavaScript files in the extension\r\n        js_files = self.get_js_files()\r\n\r\n        def update_js_contents(content):\r\n            # A string in the JavaScript file that needs to be replaced\r\n            to_replace = \"return e.defaultConfig\"\r\n            # The new content to insert, which includes the API key\r\n            replacement = (\r\n                f\"return {{ ...e.defaultConfig, apiKey: '{api_key}' }}\"\r\n            )\r\n            # Replace the old string with the new one in the file's content\r\n            return content.replace(to_replace, replacement)\r\n\r\n        # Loop through each JavaScript file and update its contents\r\n        for file in js_files:\r\n            file.update_contents(update_js_contents)\r\n\r\n        def update_config_contents(content):\r\n            # Replace the empty apiKey value with the new API key in the config file\r\n            key_replaced = content.replace(\"apiKey: '',\", f\"apiKey: '{api_key}',\")\r\n\r\n            return key_replaced\r\n\r\n        # Retrieve the specific configuration JavaScript file\r\n        config_file = self.get_file(\"/assets/config.js\")\r\n\r\n        # Update the config file with the new API key\r\n        config_file.update_contents(update_config_contents)\r\n```\r\n\r\nUsage Example:\r\n\r\n```python\r\nfrom botasaurus.browser import browser, Driver\r\nfrom chrome_extension_python import Extension\r\n\r\n@browser(\r\n    extensions=[\r\n        # Simply pass the Chrome Extension Link\r\n        Capsolver(api_key=\"CAP-MY_KEY\")\r\n    ],\r\n)\r\ndef open_chrome(driver: Driver, data):\r\n    driver.get(\"https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php\")\r\n    driver.prompt()\r\n\r\nopen_chrome()\r\n```\r\n\r\n## API Reference\r\n\r\nFor custom extension development, the following methods are available:\r\n\r\n- `get_js_files`, `get_json_files`, `get_html_files`, `get_css_files`: Recursively retrieves specified file types.\r\n- `get_file`: Retrieves a specific file as a `File` object.\r\n- `File.update_contents`: Updates the content of a file.\r\n- `force_update` (defaults to False): Redownload the extension and call `update_files` when extension data changes. It is recommended to set it to `True` during active development.\r\n\r\nExample of a custom extension with `force_update`:\r\n\r\n```python\r\nfrom chrome_extension_python import Extension\r\n\r\nclass CustomExtension(Extension):\r\n    def __init__(self, api_key):\r\n        # Initialize the CustomExtension with specific parameters\r\n        super().__init__(\r\n            extension_id=\"pgojnojmmhpofjgdmaebadhbocahppod\", # Unique identifier for the Chrome Extension, obtained from the Chrome Webstore link\r\n            extension_name=\"capsolver\", # The name assigned to the extension\r\n            force_update=True, # This flag, when set to True, forces the redownload of the extension and calls the `update_files` method. This is useful during development to ensure updates are applied.\r\n        )\r\n```\r\n\r\n## Examples of Custom Extensions\r\nHere are some code snippets of Custom Extensions to provide you with an idea of how to develop your own.\r\n\r\n- [Capsolver Extension](https://github.com/omkarcloud/capsolver-extension-python/blob/master/capsolver_extension_python/__init__.py)\r\n- [2captcha Extension](https://github.com/omkarcloud/twocaptcha-extension-python/blob/master/twocaptcha_extension_python/__init__.py)\r\n\r\n## Publishing Your Extension\r\n\r\nIf you wish to share your extension with other developers via PyPI, follow these steps:\r\n\r\n1. Clone the template repository: [capsolver-extension-python](https://github.com/omkarcloud/capsolver-extension-python).\r\n2. Replace references to \"capsolver\" with your extension's name.\r\n3. Rename the `capsolver_extension_python` folder to match your extension name.\r\n4. Insert your extension code in `__init__.py`.\r\n5. Update README.md\r\n6. Use `npm run upload` to publish the extension on PyPI.\r\n\r\nContact us at `chetan@omkar.cloud` with details about your extension. If it's beneficial for web scrapers, we'll promote it within the Botasaurus community.\r\n\r\n## Love It? [Star It ⭐!](https://github.com/omkarcloud/chrome-extension-python)\r\n\r\nBecome one of our amazing stargazers by giving us a star ⭐ on GitHub!\r\n\r\nIt's just one click, but it means the world to me.\r\n\r\n[![Stargazers for @omkarcloud/chrome-extension-python](https://bytecrank.com/nastyox/reporoster/php/stargazersSVG.php?user=omkarcloud\u0026repo=chrome-extension-python)](https://github.com/omkarcloud/chrome-extension-python/stargazers)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomkarcloud%2Fchrome-extension-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomkarcloud%2Fchrome-extension-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomkarcloud%2Fchrome-extension-python/lists"}