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

How to get the asynchronous response result of the specified URL? #623

Open
quyunet opened this issue May 10, 2024 · 1 comment
Open

How to get the asynchronous response result of the specified URL? #623

quyunet opened this issue May 10, 2024 · 1 comment

Comments

@quyunet
Copy link

quyunet commented May 10, 2024

I need to further operate the browser without closing the page and without closing the browser, becase the page will not change after the form is submitted, only the results are output asynchronously, the following methods always time out and cannot directly output asynchronous response results, unless printing at the network listening interruption point, but after I pass the result to the outside, it cannot be captured.

$responseData = null
$page->waitForElement('button[onclick="SaveDefaultNameservers();"]');

//Monitor network responses
$page->getSession()->on('method:Network.responseReceived', function ($response) use(&$responseData): void {
	if (strpos($response['response']['url'], 'https://example.com/updateDefaultNameservers') !== false) {
		$responseBody = $page->getSession()->sendMessageSync(new \HeadlessChromium\Communication\Message('Network.getResponseBody', ['requestId' => $response['requestId']]));		//Get asynchronous response
		//$page->getSession()->sendMessageSync(new \HeadlessChromium\Communication\Message('Page.stopLoading'));
		if (!isset($responseBody['error'])) {
			$responseData = $responseBody['result']['body'];
			//print_r($responseData);exit;    //Breakpoint printing can output the results correctly.
		}
	}
});

//submit Form
$page->evaluate(
	'(() => {
		document.querySelector("#accountDefaultNs").value = "ns1.netsto.net\nns2.netsto.net";
		document.querySelector("button[onclick=\'SaveDefaultNameservers();\']").click();
	})();'
)->waitForPageReload();

//Polling results.
while (!$responseData) {
	sleep(1);
}
var_dump($responseData);
@quyunet
Copy link
Author

quyunet commented May 10, 2024

at present, I have come up with the following solutions, use try{} throw asynchronous response content in error form, but it may not be the optimal solution, is there a better solution?

$responseData = null
$page->waitForElement('button[onclick="SaveDefaultNameservers();"]');

try{
	//Monitor network responses
	$page->getSession()->on('method:Network.responseReceived', function ($response) use(&$responseData): void {
		if (strpos($response['response']['url'], 'https://example.com/updateDefaultNameservers') !== false) {
			$responseBody = $page->getSession()->sendMessageSync(new \HeadlessChromium\Communication\Message('Network.getResponseBody', ['requestId' => $response['requestId']]));		//Get asynchronous response
			//$page->getSession()->sendMessageSync(new \HeadlessChromium\Communication\Message('Page.stopLoading'));
			if (!isset($responseBody['error'])) {
				$responseData = $responseBody['result']['body'];
				throw new Exception($responseData);		//Throw asynchronous response content in error form.
			}
		}
	});

	//submit Form
	$page->evaluate(
		'(() => {
			document.querySelector("#accountDefaultNs").value = "ns1.netsto.net\nns2.netsto.net";
			document.querySelector("button[onclick=\'SaveDefaultNameservers();\']").click();
		})();'
	)->waitForPageReload();

} catch (Exception $e) {
	echo $e->getMessage();		//capture response content by capturing errors.
}

@quyunet quyunet changed the title How to get the asynchronous response result of the specified URL. How to get the asynchronous response result of the specified URL? May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant