### Usage Example: Open Chrome with Capsolver Extension Source: https://github.com/omkarcloud/chrome-extension-python/blob/master/README.md Shows how to use the Capsolver extension by initializing it with an API key and then opening a Chrome browser instance with this extension loaded. The example navigates to a demo page and prompts the user. ```python from botasaurus.browser import browser, Driver from chrome_extension_python import Extension @browser( extensions=[ # Simply pass the Chrome Extension Link Capsolver(api_key="CAP-MY_KEY") ], ) def open_chrome(driver: Driver, data): driver.get("https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php") driver.prompt() open_chrome() ``` -------------------------------- ### Create Custom Extension with Configuration (Capsolver Example) Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Illustrates how to create a custom Chrome extension by subclassing the `Extension` class. This example shows how to override the `update_files` method to inject an API key into the Capsolver extension's configuration files. ```python from chrome_extension_python import Extension class Capsolver(Extension): def __init__(self, api_key): super().__init__( extension_id="pgojnojmmhpofjgdmaebadhbocahppod", extension_name="capsolver", api_key=api_key, # Custom parameter passed to update_files ) def update_files(self, api_key): # Modify all JavaScript files js_files = self.get_js_files() def inject_api_key(content): return content.replace( "return e.defaultConfig", f"return {{ ...e.defaultConfig, apiKey: '{api_key}' }}" ) for file in js_files: file.update_contents(inject_api_key) # Modify specific config file config_file = self.get_file("/assets/config.js") config_file.update_contents( lambda content: content.replace("apiKey: '',", f"apiKey: '{api_key}',") ) # Usage with Botasaurus from botasaurus.browser import browser, Driver @browser(extensions=[Capsolver(api_key="CAP-YOUR-API-KEY")]) def solve_captcha(driver: Driver, data): driver.get("https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php") # Capsolver extension auto-solves CAPTCHA return driver.page_html solve_captcha() ``` -------------------------------- ### Install chrome_extension_python Package Source: https://github.com/omkarcloud/chrome-extension-python/blob/master/README.md Installs the chrome_extension_python package using pip. This is the initial step to use the library for integrating Chrome extensions. ```bash python -m pip install chrome_extension_python ``` -------------------------------- ### Use Chrome Extension with Playwright Source: https://github.com/omkarcloud/chrome-extension-python/blob/master/README.md Illustrates integrating a Chrome extension (Adblock) with Playwright. The Extension.load() method is used to get the extension path, which is then passed to launch_persistent_context with specific arguments. ```python from playwright.sync_api import sync_playwright from chrome_extension_python import Extension import random def generate_random_profile(): return str(random.randint(1, 1000)) with sync_playwright() as p: extension_path = Extension("https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom").load(with_command_line_option=False) browser = p.chromium.launch_persistent_context( user_data_dir=generate_random_profile(), headless=False, args=[ '--disable-extensions-except='+ extension_path, '--load-extension=' + extension_path, ], ) page = browser.new_page() input("Press Enter to exit...") browser.close() ``` -------------------------------- ### Use Extension with Botasaurus (Python) Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Integrates a Chrome extension into a Botasaurus browser instance using the `extensions` parameter in the `@browser` decorator. This allows the extension to be active when the browser session starts. ```python from botasaurus.browser import browser, Driver from chrome_extension_python import Extension @browser( extensions=[ Extension( "https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom" ) ], ) def scrape_with_adblock(driver: Driver, data): driver.get("https://example.com") # Ad blocker is now active page_content = driver.page_html return page_content # Execute the scraping function result = scrape_with_adblock() ``` -------------------------------- ### Use Extension with Playwright (Python) Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Explains how to load Chrome extensions with Playwright by passing the extension path as a launch argument within a persistent browser context. This requires manual setup of the browser context. ```python from playwright.sync_api import sync_playwright from chrome_extension_python import Extension import tempfile # Placeholder for Playwright integration example # The actual implementation would involve creating a context with the extension path # Example structure: # with sync_playwright() as p: # extension = Extension("extension_id_or_url") # extension_path = extension.load(with_command_line_option=False) # browser = p.chromium.launch(args=[ # f"--disable-extensions-except={extension_path}", # f"--load-extension={extension_path}" # ]) # context = browser.new_context() # page = context.new_page() # page.goto("https://example.com") # # ... interact with page ... # browser.close() ``` -------------------------------- ### Initialize Extension from Chrome Web Store URL (Python) Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Demonstrates how to initialize the Extension class using a Chrome Web Store URL. This method automatically downloads and prepares the extension. It returns a command-line argument for Chrome or just the extension path. ```python from chrome_extension_python import Extension # Initialize extension from Chrome Web Store URL extension = Extension( "https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom" ) # Load extension - returns command line argument for Chrome chrome_arg = extension.load() # Output: "--load-extension=/path/to/extensions/adblock-%E2%80%94-best-ad-blocker" # Load extension path only (for Playwright) extension_path = extension.load(with_command_line_option=False) # Output: "/path/to/extensions/adblock-%E2%80%94-best-ad-blocker" ``` -------------------------------- ### Create Configurable Capsolver Chrome Extension with API Key Source: https://github.com/omkarcloud/chrome-extension-python/blob/master/README.md Demonstrates how to create a configurable Capsolver Chrome Extension by initializing it with an API key. It updates JavaScript files to include the API key and modifies the configuration file. ```python from chrome_extension_python import Extension class Capsolver(Extension): def __init__(self, api_key): # Initialize the Capsolver extension with given parameters super().__init__( extension_id="pgojnojmmhpofjhdmaebadhbocahppod", # Unique identifier for the Chrome Extension, found in the Chrome Webstore link extension_name="capsolver", # The name assigned to the extension api_key=api_key, # An important custom parameter (API key) required for the extension's functionality ) # This method is called to update the necessary JavaScript files within the extension def update_files(self, api_key): # Retrieve a list of all JavaScript files in the extension js_files = self.get_js_files() def update_js_contents(content): # A string in the JavaScript file that needs to be replaced to_replace = "return e.defaultConfig" # The new content to insert, which includes the API key replacement = ( f"return {{ ...e.defaultConfig, apiKey: '{api_key}' }}" ) # Replace the old string with the new one in the file's content return content.replace(to_replace, replacement) # Loop through each JavaScript file and update its contents for file in js_files: file.update_contents(update_js_contents) def update_config_contents(content): # Replace the empty apiKey value with the new API key in the config file key_replaced = content.replace("apiKey: '',", f"apiKey: '{api_key}',") return key_replaced # Retrieve the specific configuration JavaScript file config_file = self.get_file("/assets/config.js") # Update the config file with the new API key config_file.update_contents(update_config_contents) ``` -------------------------------- ### Load and Launch Browser with Extension Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Demonstrates how to load a Chrome extension using its URL and launch a browser instance with the extension enabled. It utilizes `sync_playwright` to manage the browser context and specifies arguments to load the extension. ```python from chrome_extension_python import Extension from playwright.sync_api import sync_playwright import tempfile # Create extension instance adblock = Extension( "https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom" ) with sync_playwright() as p: # Get extension path without command line prefix extension_path = adblock.load(with_command_line_option=False) # Launch browser with extension browser = p.chromium.launch_persistent_context( user_data_dir=tempfile.mkdtemp(), headless=False, args=[ f'--disable-extensions-except={extension_path}', f'--load-extension={extension_path}', ], ) page = browser.new_page() page.goto("https://example.com") print(page.title()) browser.close() ``` -------------------------------- ### Initialize Extension with ID and Name (Python) Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Shows how to initialize the Extension class using a specific extension ID and a custom name. This is useful for creating reusable custom extension configurations and allows for options like forcing an update. ```python from chrome_extension_python import Extension # Initialize extension with ID and custom name extension = Extension( extension_id="gighmmpiobklfepjocnamgkkbiglidom", # From Chrome Web Store URL extension_name="adblock", # Custom folder name force_update=False # Set True during development to redownload ) # Extension properties print(extension.extension_id) # "gighmmpiobklfepjocnamgkkbiglidom" print(extension.extension_name) # "adblock" print(extension.extension_path) # "extensions/adblock" print(extension.extension_absolute_path) # Full absolute path ``` -------------------------------- ### Force Extension Update During Development with Python Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Illustrates how to use the `force_update=True` parameter during extension initialization to ensure the extension is re-downloaded and reconfigured on every load. This is beneficial for rapid development and testing of file update logic. The `force_update=False` (default) is recommended for production to leverage caching. ```python from chrome_extension_python import Extension class DevExtension(Extension): def __init__(self, api_key): super().__init__( extension_id="pgojnojmmhpofjgdmaebadhbocahppod", extension_name="dev-extension", force_update=True, # Always redownload during development api_key=api_key, ) def update_files(self, api_key): # This will run on every load when force_update=True js_files = self.get_js_files() for file in js_files: file.update_contents( lambda content: content.replace("PLACEHOLDER_KEY", api_key) ) print("Extension files updated with new API key") # In production, set force_update=False (default) for caching class ProductionExtension(Extension): def __init__(self, api_key): super().__init__( extension_id="pgojnojmmhpofjgdmaebadhbocahppod", extension_name="prod-extension", force_update=False, # Default - only update when config changes api_key=api_key, ) ``` -------------------------------- ### Use Chrome Extension with Selenium Source: https://github.com/omkarcloud/chrome-extension-python/blob/master/README.md Shows how to integrate a Chrome extension (Adblock) with Selenium. It involves setting Chrome options, adding the extension using Extension.load(), and then launching the Chrome driver. ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options from chromedriver_autoinstaller import install from chrome_extension_python import Extension # Set Chrome options options = Options() options.add_argument(Extension("https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom").load()) # Install and set up the driver driver_path = install() driver = webdriver.Chrome(driver_path, options=options) # Prompt for user input input("Press Enter to exit...") # Clean up driver.quit() ``` -------------------------------- ### Use Chrome Extension with Botasaurus Source: https://github.com/omkarcloud/chrome-extension-python/blob/master/README.md Demonstrates integrating a Chrome extension (Adblock) with the Botasaurus framework. The Extension class is used within the @browser decorator to specify the extension's Chrome Webstore link. ```python from botasaurus.browser import browser, Driver from chrome_extension_python import Extension @browser( extensions=[ # Simply pass the Chrome Extension Link Extension( "https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom" ) ], ) def scrape_while_blocking_ads(driver: Driver, data): driver.prompt() scrape_while_blocking_ads() ``` -------------------------------- ### Use Extension with Selenium (Python) Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Demonstrates how to add a Chrome extension to a Selenium WebDriver instance. The extension's command-line argument is added to Chrome options before initializing the driver. ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options from chromedriver_autoinstaller import install from chrome_extension_python import Extension # Create extension instance adblock = Extension( "https://chromewebstore.google.com/detail/adblock-%E2%80%94-best-ad-blocker/gighmmpiobklfepjocnamgkkbiglidom" ) # Configure Chrome options with extension options = Options() options.add_argument(adblock.load()) # Returns "--load-extension=..." # Initialize driver driver_path = install() driver = webdriver.Chrome(driver_path, options=options) # Use the browser with extension loaded driver.get("https://example.com") print(driver.title) # Cleanup driver.quit() ``` -------------------------------- ### Custom Extension Development with force_update Flag Source: https://github.com/omkarcloud/chrome-extension-python/blob/master/README.md Illustrates how to develop a custom Chrome extension using the Extension class and enable the `force_update` flag. Setting `force_update=True` ensures the extension is redownloaded and `update_files` is called when extension data changes, which is useful during development. ```python from chrome_extension_python import Extension class CustomExtension(Extension): def __init__(self, api_key): # Initialize the CustomExtension with specific parameters super().__init__( extension_id="pgojnojmmhpofjgdmaebadhbocahppod", # Unique identifier for the Chrome Extension, obtained from the Chrome Webstore link extension_name="capsolver", # The name assigned to the extension 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. ) ``` -------------------------------- ### Update Extension File Contents with Python Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Demonstrates how to modify the content of a specific extension file using a callback function. This method is useful for dynamically updating configuration or script content based on external parameters. It requires an instance of the `Extension` class and a file path. ```python from chrome_extension_python import Extension class ConfigurableExtension(Extension): def __init__(self, api_endpoint, debug_mode=False): super().__init__( extension_id="abcdefghijklmnopqrstuvwxyzabcdef", extension_name="configurable-ext", api_endpoint=api_endpoint, debug_mode=debug_mode, ) def update_files(self, api_endpoint, debug_mode): # Get specific file by path relative to extension root config_file = self.get_file("/config.js") def update_config(content): # Replace API endpoint content = content.replace( 'API_URL: "https://default-api.com"', f'API_URL: "{api_endpoint}"' ) # Set debug mode content = content.replace( 'DEBUG: false', f'DEBUG: {"true" if debug_mode else "false"}' ) return content config_file.update_contents(update_config) # Read file contents without modifying current_content = config_file.get_contents() print(f"Config file length: {len(current_content)} bytes") # Direct write (alternative to update_contents) # config_file.write_contents("new content here") ``` -------------------------------- ### File Access Methods for Extension Files Source: https://context7.com/omkarcloud/chrome-extension-python/llms.txt Explains how to use `get_js_files`, `get_json_files`, `get_html_files`, and `get_css_files` methods to retrieve lists of `File` objects for specific file types within an extension directory. This enables bulk modifications to these files. ```python from chrome_extension_python import Extension class CustomExtension(Extension): def __init__(self, config_value): super().__init__( extension_id="abcdefghijklmnopqrstuvwxyzabcdef", extension_name="custom-ext", config_value=config_value, ) def update_files(self, config_value): # Get all JavaScript files recursively js_files = self.get_js_files() print(f"Found {len(js_files)} JS files") # Get all JSON files (e.g., manifest.json, config files) json_files = self.get_json_files() # Get all HTML files (popup.html, options.html, etc.) html_files = self.get_html_files() # Get all CSS files css_files = self.get_css_files() # Modify each JS file for file in js_files: file.update_contents( lambda content: content.replace("DEFAULT_VALUE", config_value) ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.