Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NoDriver] Fetch.requestPaused not working #1871

Open
namename-123 opened this issue May 7, 2024 · 8 comments
Open

[NoDriver] Fetch.requestPaused not working #1871

namename-123 opened this issue May 7, 2024 · 8 comments

Comments

@namename-123
Copy link

The code below simply hangs instead of continuing.

async def receive_handler(event: cdp.fetch.RequestPaused):
  print(event.resource_type)
  return cdp.fetch.continue_request(request_id=event.request_id)

async def main():
  options = Options()
  service = Service('C:\Program Files\Google\Chrome\Application\chrome.exe')
  driver = await start(
    service=service,
    options=options,
  )

  cdp.fetch.enable()
  tab = driver.main_tab
  tab.add_handler(cdp.fetch.RequestPaused, receive_handler)

  page = await driver.get('https://abrahamjuliot.github.io/creepjs/')
  
  input("Press Enter to continue...")
  await page.close()

if __name__ == '__main__':

  loop().run_until_complete(main())
@ultrafunkamsterdam
Copy link
Owner

ultrafunkamsterdam commented May 8, 2024

Return cdp.fetch.continue_request(request_id=event.request_id)

Needs to be awaited. Not tested but 99% sure

Edit: you are returning a bare CDP command as well. You need to tab.send it as well

@ultrafunkamsterdam
Copy link
Owner

ultrafunkamsterdam commented May 8, 2024

The code below simply hangs instead of continuing.

async def receive_handler(event: cdp.fetch.RequestPaused, tab):
  print(event.resource_type)
  return await tab.send(cdp.fetch.continue_request(request_id=event.request_id))

async def main():
  options = Options()
  service = Service('C:\Program Files\Google\Chrome\Application\chrome.exe')
  driver = await start(
    service=service,
    options=options,
  )

  await cdp.fetch.enable()
  tab = driver.main_tab
  tab.add_handler(lambda e: receive_handler(e, tab))

  page = await driver.get('https://abrahamjuliot.github.io/creepjs/')
  
  input("Press Enter to continue...")
  await page.close()

if __name__ == '__main__':

  loop().run_until_complete(main())

And forgot some awaits. Untested corrected code above

@namename-123
Copy link
Author

namename-123 commented May 8, 2024

Doesn't work, it says RuntimeWarning: coroutine 'receive_handler' was never awaited

@namename-123
Copy link
Author

Alright I just moved the receive_handler function into the main function and it works now, it's probably better to make a whole class instead.
Thanks for help!

@namename-123
Copy link
Author

Hmm this is odd, it "should" work now but the site never loads, rather it's stuck on loading indefinitely.
I wonder what's causing this?

@ultrafunkamsterdam
Copy link
Owner

ultrafunkamsterdam commented May 8, 2024

this works

from nodriver import *
import asyncio



def receive_handler(event: cdp.fetch.RequestPaused, tab):
  print(event.resource_type)
  asyncio.ensure_future(tab.send(cdp.fetch.continue_request(request_id=event.request_id)))

async def main():
  driver = await start(
  )
  tab = driver.main_tab
  driver.main_tab.add_handler(cdp.fetch.RequestPaused, lambda e: receive_handler(e, tab))
  await tab.get('https://abrahamjuliot.github.io/creepjs/')
  input("Press Enter to continue...")
  await tab.close()


if __name__ == '__main__':
  loop().run_until_complete(main())
  

ofcourse it would make more sense to encapsulate it in a class

@ultrafunkamsterdam
Copy link
Owner

image

@flaming-chameleon
Copy link

flaming-chameleon commented May 29, 2024

cdp.fetch.continue_request it cant change url and headers

class RequestInterceptor:
    def __init__(self):
        self.driver = None
        self.tab = None

    async def start(self, driver):
        self.driver = driver
        self.tab = self.driver.main_tab
        self.tab.add_handler(cdp.fetch.RequestPaused, lambda e: self.receive_handler(e))
        await self.enable_fetch()

    async def enable_fetch(self):
        await self.tab.send(cdp.fetch.enable(patterns=[{"urlPattern": "*"}]))

    def receive_handler(self, event: cdp.fetch.RequestPaused):
        print(f"Intercepted {event.resource_type} request")
        asyncio.ensure_future(self.modify_request(event))


    async def modify_request(self, event: cdp.fetch.RequestPaused):
        try:
            url_fragment = event.request.url_fragment
            request_id = event.request_id

            # Convert headers to a dictionary if necessary
            headers = event.request.headers
            if hasattr(headers, 'to_dict'):
                headers_dict = headers.to_dict()
            else:
                headers_dict = {header: value for header, value in headers.items()}

            # Update headers
            for key, value in my_headers.items():
                headers_dict[key] = value

            # Format headers for continue_request
            formatted_headers = [{'name': key, 'value': value} for key, value in headers_dict.items()]

            if url_fragment and "tgWebAppPlatform=web" in url_fragment:
                print(f"Intercepted request with URL fragment: {url_fragment}")

                # Change URL params
                new_url_fragment = url_fragment.replace("tgWebAppPlatform=web", "tgWebAppPlatform=android")
                new_url = event.request.url.split('#')[0] + '#' + new_url_fragment

                print(f"New URL: {new_url}")
                print(f"Modified Headers: {formatted_headers}")

                # Log before sending the request
                print(f"Continuing request {request_id} with new URL and headers...")

                # Continue the request with updated URL and headers
                await self.tab.send(cdp.fetch.continue_request(
                    request_id=request_id, 
                    url=new_url, 
                    headers=formatted_headers
                ))

                # Log after sending the request
                print(f"Request {request_id} continued with modified URL and headers")
            else:
                print(f"Request URL: {event.request.url}")
                print(f"Original Headers: {formatted_headers}")

                # Log before sending the request
                print(f"Continuing request {request_id} with original URL and modified headers...")

                # Continue the request with the original URL and modified headers
                await self.tab.send(cdp.fetch.continue_request(
                    request_id=request_id, 
                    headers=formatted_headers
                ))

                # Log after sending the request
                print(f"Request {request_id} continued with original URL and modified headers")
        
        except Exception as e:
            print(f"Error in intercepting request: {e}")
            traceback.print_exc()


    async def close(self):
        await self.tab.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants