Skip to content

Commit

Permalink
feat: Page.removeScriptToEvaluateOnNewDocument (#10250)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Rudenko <alexrudenko@chromium.org>
  • Loading branch information
Junyan and OrKoN committed May 26, 2023
1 parent 031b021 commit b5a124f
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 112 deletions.
1 change: 1 addition & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ sidebar_label: API
| [MouseOptions](./puppeteer.mouseoptions.md) | |
| [MouseWheelOptions](./puppeteer.mousewheeloptions.md) | |
| [NetworkConditions](./puppeteer.networkconditions.md) | |
| [NewDocumentScriptEvaluation](./puppeteer.newdocumentscriptevaluation.md) | |
| [Offset](./puppeteer.offset.md) | |
| [PageEventObject](./puppeteer.pageeventobject.md) | <p>Denotes the objects received by callback functions for page events.</p><p>See [PageEmittedEvents](./puppeteer.pageemittedevents.md) for more detail on the events and when they are emitted.</p> |
| [PDFMargin](./puppeteer.pdfmargin.md) | |
Expand Down
17 changes: 17 additions & 0 deletions docs/api/puppeteer.newdocumentscriptevaluation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
sidebar_label: NewDocumentScriptEvaluation
---

# NewDocumentScriptEvaluation interface

#### Signature:

```typescript
export interface NewDocumentScriptEvaluation
```

## Properties

| Property | Modifiers | Type | Description | Default |
| ---------- | --------- | ------ | ----------- | ------- |
| identifier | | string | | |
7 changes: 5 additions & 2 deletions docs/api/puppeteer.page.evaluateonnewdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class Page {
evaluateOnNewDocument<
Params extends unknown[],
Func extends (...args: Params) => unknown = (...args: Params) => unknown
>(pageFunction: Func | string, ...args: Params): Promise<void>;
>(
pageFunction: Func | string,
...args: Params
): Promise<NewDocumentScriptEvaluation>;
}
```

Expand All @@ -32,7 +35,7 @@ class Page {

**Returns:**

Promise&lt;void&gt;
Promise&lt;[NewDocumentScriptEvaluation](./puppeteer.newdocumentscriptevaluation.md)&gt;

## Example

Expand Down
179 changes: 90 additions & 89 deletions docs/api/puppeteer.page.md

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions docs/api/puppeteer.page.removescripttoevaluateonnewdocument.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
sidebar_label: Page.removeScriptToEvaluateOnNewDocument
---

# Page.removeScriptToEvaluateOnNewDocument() method

Removes script that injected into page by Page.evaluateOnNewDocument.

#### Signature:

```typescript
class Page {
removeScriptToEvaluateOnNewDocument(identifier: string): Promise<void>;
}
```

## Parameters

| Parameter | Type | Description |
| ---------- | ------ | ----------------- |
| identifier | string | script identifier |

**Returns:**

Promise&lt;void&gt;
24 changes: 22 additions & 2 deletions packages/puppeteer-core/src/api/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ export interface PageEventObject {
workerdestroyed: WebWorker;
}

/**
* @public
*/
export interface NewDocumentScriptEvaluation {
identifier: string;
}

/**
* Page provides methods to interact with a single tab or
* {@link https://developer.chrome.com/extensions/background_pages | extension background page}
Expand Down Expand Up @@ -2109,8 +2116,21 @@ export class Page extends EventEmitter {
async evaluateOnNewDocument<
Params extends unknown[],
Func extends (...args: Params) => unknown = (...args: Params) => unknown
>(pageFunction: Func | string, ...args: Params): Promise<void>;
async evaluateOnNewDocument(): Promise<void> {
>(
pageFunction: Func | string,
...args: Params
): Promise<NewDocumentScriptEvaluation>;
async evaluateOnNewDocument(): Promise<NewDocumentScriptEvaluation> {
throw new Error('Not implemented');
}

/**
* Removes script that injected into page by Page.evaluateOnNewDocument.
*
* @param identifier - script identifier
*/
async removeScriptToEvaluateOnNewDocument(identifier: string): Promise<void>;
async removeScriptToEvaluateOnNewDocument(): Promise<void> {
throw new Error('Not implemented');
}

Expand Down
23 changes: 20 additions & 3 deletions packages/puppeteer-core/src/common/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
ScreenshotOptions,
WaitForOptions,
WaitTimeoutOptions,
NewDocumentScriptEvaluation,
} from '../api/Page.js';
import {assert} from '../util/assert.js';
import {
Expand Down Expand Up @@ -1288,10 +1289,26 @@ export class CDPPage extends Page {
override async evaluateOnNewDocument<
Params extends unknown[],
Func extends (...args: Params) => unknown = (...args: Params) => unknown
>(pageFunction: Func | string, ...args: Params): Promise<void> {
>(
pageFunction: Func | string,
...args: Params
): Promise<NewDocumentScriptEvaluation> {
const source = evaluationString(pageFunction, ...args);
await this.#client.send('Page.addScriptToEvaluateOnNewDocument', {
source,
const {identifier} = await this.#client.send(
'Page.addScriptToEvaluateOnNewDocument',
{
source,
}
);

return {identifier};
}

override async removeScriptToEvaluateOnNewDocument(
identifier: string
): Promise<void> {
await this.#client.send('Page.removeScriptToEvaluateOnNewDocument', {
identifier,
});
}

Expand Down
32 changes: 16 additions & 16 deletions test/TestExpectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.removeScriptToEvaluateOnNewDocument *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[EventEmitter.spec] EventEmitter *",
"platforms": ["darwin", "linux", "win32"],
Expand Down Expand Up @@ -294,15 +306,15 @@
"expectations": ["SKIP"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument should evaluate before anything else on the page",
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument should work with CSP",
"testIdPattern": "[evaluation.spec] Evaluation specs Page.removeScriptToEvaluateOnNewDocument *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
Expand Down Expand Up @@ -905,18 +917,6 @@
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument should evaluate before anything else on the page",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[evaluation.spec] Evaluation specs Page.evaluateOnNewDocument should work with CSP",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[fixtures.spec] Fixtures dumpio option should work with pipe option",
"platforms": ["darwin", "linux", "win32"],
Expand Down
24 changes: 24 additions & 0 deletions test/src/evaluation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,30 @@ describe('Evaluation specs', function () {
});
});

describe('Page.removeScriptToEvaluateOnNewDocument', function () {
it('should remove new document script', async () => {
const {page, server} = getTestState();

const {identifier} = await page.evaluateOnNewDocument(function () {
(globalThis as any).injected = 123;
});
await page.goto(server.PREFIX + '/tamperable.html');
expect(
await page.evaluate(() => {
return (globalThis as any).result;
})
).toBe(123);

await page.removeScriptToEvaluateOnNewDocument(identifier);
await page.reload();
expect(
await page.evaluate(() => {
return (globalThis as any).result || null;
})
).toBe(null);
});
});

describe('Frame.evaluate', function () {
it('should have different execution contexts', async () => {
const {page, server} = getTestState();
Expand Down

0 comments on commit b5a124f

Please sign in to comment.