-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
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 |
And forgot some awaits. Untested corrected code above |
Doesn't work, it says |
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. |
Hmm this is odd, it "should" work now but the site never loads, rather it's stuck on loading indefinitely. |
this works
ofcourse it would make more sense to encapsulate it in a class |
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() |
The code below simply hangs instead of continuing.
The text was updated successfully, but these errors were encountered: