Skip to content

Commit 546826c

Browse files
committed
feat: follow the best practice
1 parent f08b3fe commit 546826c

File tree

1 file changed

+9
-11
lines changed
  • sources/academy/webscraping/puppeteer_playwright/executing_scripts

1 file changed

+9
-11
lines changed

sources/academy/webscraping/puppeteer_playwright/executing_scripts/index.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ Within our code, we generate a `randomString` in the Node.js context:
105105
const randomString = Math.random().toString(36).slice(2);
106106
```
107107

108-
Now, let's say we want to change the title of the document to be this random string. In order to use this `randomString` variable in the callback of our `page.evaluate()`, we'll have to pass in a second parameter containing the variable.
109-
110-
> It's best practice to make this second parameter an object, as you are usually passing more than one value in.
108+
Now, let's say we want to change the title of the document to be this random string. To have the random string available in the callback of our `page.evaluate()`, we'll pass it in a second parameter. It's best practice to have this second parameter as an object, because in real world situations you often need to pass more than one value.
111109

112110
<Tabs groupId="main">
113111
<TabItem value="Playwright" label="Playwright">
@@ -120,11 +118,11 @@ const page = await browser.newPage();
120118

121119
await page.goto('https://google.com/');
122120

123-
const randomString = Math.random().toString(36).slice(2);
121+
const params = { randomString: Math.random().toString(36).slice(2) }
124122

125-
await page.evaluate((randomStringInner) => {
126-
document.querySelector('title').textContent = randomStringInner;
127-
}, randomString);
123+
await page.evaluate(({ randomString }) => {
124+
document.querySelector('title').textContent = randomString;
125+
}, params);
128126

129127
await page.waitForTimeout(10000);
130128

@@ -142,11 +140,11 @@ const page = await browser.newPage();
142140

143141
await page.goto('https://google.com/');
144142

145-
const randomString = Math.random().toString(36).slice(2);
143+
const params = { randomString: Math.random().toString(36).slice(2) }
146144

147-
await page.evaluate((randomStringInner) => {
148-
document.querySelector('title').textContent = randomStringInner;
149-
}, randomString);
145+
await page.evaluate(({ randomString }) => {
146+
document.querySelector('title').textContent = randomString;
147+
}, params);
150148

151149
await page.waitForTimeout(10000);
152150

0 commit comments

Comments
 (0)