Skip to content

Commit

Permalink
feat(Frame): implement frame.content and frame.setContent methods (pu…
Browse files Browse the repository at this point in the history
…ppeteer#1447)

This refactors the page.content and page.setContent methods to be defined on the Frame class. This allows access from the Page still but also on all frames.

Fixes puppeteer#754
  • Loading branch information
trentmwillis authored and aslushnikov committed Nov 23, 2017
1 parent 3b60ad1 commit 770c17b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
11 changes: 11 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@
* [frame.addScriptTag(options)](#frameaddscripttagoptions)
* [frame.addStyleTag(options)](#frameaddstyletagoptions)
* [frame.childFrames()](#framechildframes)
* [frame.content()](#framecontent)
* [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args)
* [frame.executionContext()](#frameexecutioncontext)
* [frame.isDetached()](#frameisdetached)
* [frame.name()](#framename)
* [frame.parentFrame()](#frameparentframe)
* [frame.select(selector, ...values)](#frameselectselector-values)
* [frame.setContent(html)](#framesetcontenthtml)
* [frame.title()](#frametitle)
* [frame.url()](#frameurl)
* [frame.waitFor(selectorOrFunctionOrTimeout[, options[, ...args]])](#framewaitforselectororfunctionortimeout-options-args)
Expand Down Expand Up @@ -1484,6 +1486,11 @@ Adds a `<link rel="stylesheet">` tag into the page with the desired url or a `<s
#### frame.childFrames()
- returns: <[Array]<[Frame]>>

#### frame.content()
- returns: <[Promise]<[String]>>

Gets the full HTML contents of the frame, including the doctype.

#### frame.evaluate(pageFunction, ...args)
- `pageFunction` <[function]|[string]> Function to be evaluated in browser context
- `...args` <...[Serializable]|[ElementHandle]> Arguments to pass to `pageFunction`
Expand Down Expand Up @@ -1546,6 +1553,10 @@ frame.select('select#colors', 'blue'); // single selection
frame.select('select#colors', 'red', 'green', 'blue'); // multiple selections
```

#### frame.setContent(html)
- `html` <[string]> HTML markup to assign to the page.
- returns: <[Promise]>

#### frame.title()
- returns: <[Promise]<[string]>> Returns page's title.

Expand Down
25 changes: 25 additions & 0 deletions lib/FrameManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,31 @@ class Frame {
return result;
}

/**
* @return {!Promise<String>}
*/
async content() {
return await this.evaluate(() => {
let retVal = '';
if (document.doctype)
retVal = new XMLSerializer().serializeToString(document.doctype);
if (document.documentElement)
retVal += document.documentElement.outerHTML;
return retVal;
});
}

/**
* @param {string} html
*/
async setContent(html) {
await this.evaluate(html => {
document.open();
document.write(html);
document.close();
}, html);
}

/**
* @return {string}
*/
Expand Down
15 changes: 2 additions & 13 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,25 +428,14 @@ class Page extends EventEmitter {
* @return {!Promise<String>}
*/
async content() {
return await this.evaluate(() => {
let retVal = '';
if (document.doctype)
retVal = new XMLSerializer().serializeToString(document.doctype);
if (document.documentElement)
retVal += document.documentElement.outerHTML;
return retVal;
});
return await this._frameManager.mainFrame().content();
}

/**
* @param {string} html
*/
async setContent(html) {
await this.evaluate(html => {
document.open();
document.write(html);
document.close();
}, html);
await this._frameManager.mainFrame().setContent(html);
}

/**
Expand Down

0 comments on commit 770c17b

Please sign in to comment.