Skip to content

Commit

Permalink
suppor to dump {head|body|document} dom
Browse files Browse the repository at this point in the history
  • Loading branch information
yadvendar committed Jul 8, 2017
1 parent f353309 commit 16b6ee9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
.idea/
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,32 @@ docker run -it -p 8080:8080 --cap-add SYS_ADMIN --name bot-render-container bot-

Send a request to the server running inside the container:
```bash
curl http://localhost:8080/?url=https://dynamic-meta.appspot.com
curl -X GET "http://localhost:8080/?url={urlToDump}&dom={document|body|head}"
```
Examples:

```bash
# To dump head dom (Default)
curl -X GET "http://localhost:8080/?url=https://dynamic-meta.appspot.com&dom=head"

# To dump body dom
curl -X GET "http://localhost:8080/?url=https://dynamic-meta.appspot.com&dom=body"

# To dump document dom
curl -X GET "http://localhost:8080/?url=https://dynamic-meta.appspot.com&dom=document"
```

Stop the container:
```bash
docker kill bot-render-container
docker stop bot-render-container
```

Remove the container:
```bash
docker rm bot-render-container
```

Clear containers:
Clear all containers:
```bash
docker rm -f $(docker ps -a -q)
```
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const express = require('express');
const app = express();

app.get('/', async function(request, response) {
const head = await new Renderer(request.query.url).extractHead().catch((err) => console.error(err));
response.send(head);
const dom = await new Renderer(request.query.url).extractDOM(request.query.dom).catch((err) => console.error(err));
response.send(dom);
});

app.get('/_ah/health', (request, response) => response.send('OK'));
Expand Down
7 changes: 4 additions & 3 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Renderer {
this._url = url;
}

extractHead() {
extractDOM(htmlElement) {
return new Promise((resolve, reject) => {
CDP.New().then((tab) => {
return CDP({tab: tab});
Expand All @@ -31,10 +31,11 @@ class Renderer {
Page.navigate({url: this._url});
});

// Load and dump DOM of head element.
// Load and dump DOM of {head(default)|body|document} element.
Page.loadEventFired(() => {
setTimeout(async() => {
let result = await Runtime.evaluate({expression: 'document.head.outerHTML'});
let expression = htmlElement == 'document'? 'document.documentElement.outerHTML' : (htmlElement == 'body' ? 'document.body.outerHTML' : 'document.head.outerHTML');
let result = await Runtime.evaluate({expression: expression});
CDP.Close({id: client.tab.id});
resolve(result.result.value);
}, 1500);
Expand Down

0 comments on commit 16b6ee9

Please sign in to comment.