feat: Page.removeScriptToEvaluateOnNewDocument (#10250)

Co-authored-by: Alex Rudenko <alexrudenko@chromium.org>
This commit is contained in:
Junyan 2023-05-26 15:56:45 +08:00 committed by GitHub
parent 031b021703
commit b5a124ff73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 220 additions and 112 deletions

View File

@ -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) | |

View File

@ -0,0 +1,17 @@
---
sidebar_label: NewDocumentScriptEvaluation
---
# NewDocumentScriptEvaluation interface
#### Signature:
```typescript
export interface NewDocumentScriptEvaluation
```
## Properties
| Property | Modifiers | Type | Description | Default |
| ---------- | --------- | ------ | ----------- | ------- |
| identifier | | string | | |

View File

@ -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>;
}
```
@ -32,7 +35,7 @@ class Page {
**Returns:**
Promise&lt;void&gt;
Promise&lt;[NewDocumentScriptEvaluation](./puppeteer.newdocumentscriptevaluation.md)&gt;
## Example

View File

@ -75,7 +75,7 @@ page.off('request', logRequest);
## Methods
| Method | Modifiers | Description |
| ------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ---------------------------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [$(selector)](./puppeteer.page._.md) | | Runs <code>document.querySelector</code> within the page. If no element matches the selector, the return value resolves to <code>null</code>. |
| [$$(selector)](./puppeteer.page.__.md) | | The method runs <code>document.querySelectorAll</code> within the page. If no elements match the selector, the return value resolves to <code>[]</code>. |
| [$$eval(selector, pageFunction, args)](./puppeteer.page.__eval.md) | | This method runs <code>Array.from(document.querySelectorAll(selector))</code> within the page and passes the result as the first argument to the <code>pageFunction</code>. |
@ -126,6 +126,7 @@ page.off('request', logRequest);
| [pdf(options)](./puppeteer.page.pdf.md) | | Generates a PDF of the page with the <code>print</code> CSS media type. |
| [queryObjects(prototypeHandle)](./puppeteer.page.queryobjects.md) | | This method iterates the JavaScript heap and finds all objects with the given prototype. |
| [reload(options)](./puppeteer.page.reload.md) | | |
| [removeScriptToEvaluateOnNewDocument(identifier)](./puppeteer.page.removescripttoevaluateonnewdocument.md) | | Removes script that injected into page by Page.evaluateOnNewDocument. |
| [screenshot(options)](./puppeteer.page.screenshot.md) | | Captures screenshot of the current page. |
| [screenshot(options)](./puppeteer.page.screenshot_1.md) | | |
| [screenshot(options)](./puppeteer.page.screenshot_2.md) | | |

View File

@ -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;

View File

@ -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}
@ -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');
}

View File

@ -40,6 +40,7 @@ import {
ScreenshotOptions,
WaitForOptions,
WaitTimeoutOptions,
NewDocumentScriptEvaluation,
} from '../api/Page.js';
import {assert} from '../util/assert.js';
import {
@ -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', {
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,
});
}

View File

@ -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"],
@ -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"]
},
{
@ -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"],

View File

@ -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();