NopeCHALLC / nopecha-extension

Automated CAPTCHA solver for your browser. Works with Selenium, Puppeteer, Playwright, and more.
MIT License
6.47k stars 94 forks source link

Temporarily disable/enable "automation build" Extension #46

Open StigP1337 opened 3 months ago

StigP1337 commented 3 months ago

I am currently using the "automation build" Version of the Extension. Is there a way to temporarily disable the Extension from my Python Selenium script when captcha solving is not needed or not wanted?

With the "graphical build" Version I could use the Magic URL (https://nopecha.com/setup#enabled=false) to disable and enable the extension from my script when I need too.

This doesn't work in the "automation build" Version. Is their another way to achieve the same behaviour with this version? Or do you have plans to add such a feature?

Other extensions offer the possibility to use Javascript to change settings or to turn it off/on. Maybe you can add such a way to your extension?

j-w-yun commented 3 months ago

You're right, the graphical build has a way to export settings encoded as a URL - see documentation.

However, this URL will not work in the automation build. The reason for this is because settings are imported via manifest.json and the extension cannot write to it.

It would be useful to understand why you need this feature. For example, if you want to the extension to not run on certain pages, you can add the URL of the page to ignore in the disabled_hosts property of the manifest settings.

StigP1337 commented 3 months ago

One example is when I need to solve a captcha to submit a form and I am only interessted in the error feedback. I don't need to solve the captcha again when the form with error messages is displayed again.

jakeee51 commented 3 months ago

I agree with @StigP1337 , such a feature to change settings on the fly would be useful. Although my issue is a bit different I felt it's similar enough to keep it in this post.

I'd like to be able to use the Magic URL in the automation build to update which nopecha key to use when one key runs out of credits.

I did notice the "keys": [] field in the manifest.json but I couldn't get it to work. I probably just don't understand its functionality fully.

Le0Developer commented 3 months ago

The keys field lets you round-robin across all keys in the array (it randomly picks one everytime it does a request) The UI/extension popup always uses the key field, so some confusion might be caused by that.

keys is a band-aid solution until we have proper "multi-key" server-side.

jakeee51 commented 3 months ago

Thanks @Le0Developer , looking forward to that multi-key support.

In the meantime perhaps @StigP1337 can use the solution I discovered. In order to disable the autmation build version of the extension without closing your selenium browser session you can use driver.execute_script() method which lets you execute JavaScript code in the browser.

For example:

def re_enable_nopecha_ext(driver):
      driver.get('chrome://extensions/'); time.sleep(1) 
      # Enable Developer mode using JavaScript
      driver.execute_script(
         'document.querySelector("extensions-manager").shadowRoot.querySelector("extensions-toolbar").shadowRoot.querySelector("#devMode").click()')
      time.sleep(1)

      # Construct the JavaScript command to click the enable/disable toggle button
      disable_script = f"""
      var manager = document.querySelector("extensions-manager").shadowRoot;
      var item = manager.querySelector('extensions-item-list').shadowRoot.querySelector('extensions-item[id="{nopecha_extension_id}"]').shadowRoot;
      item.querySelector('cr-toggle[aria-label="On, extension enabled"]').click();
      """
      enable_script = f"""
      var manager = document.querySelector("extensions-manager").shadowRoot;
      var item = manager.querySelector('extensions-item-list').shadowRoot.querySelector('extensions-item[id="{nopecha_extension_id}"]').shadowRoot;
      item.querySelector('cr-toggle[aria-label="Off"]').click();
      """
      driver.execute_script(disable_script)
      time.sleep(1)
      driver.execute_script(enable_script)
      print("Extension reloaded")

For my own project I edit the manifest.json then click the reload extension button.

Le0Developer commented 3 months ago

Or alternatively disable auto-open and open the captcha yourself.

StigP1337 commented 3 months ago

Thank you @jakeee51 and @Le0Developer for the replies. I will try the suggested solutions.