Anuj-Panthri commited on
Commit
9c34b87
·
1 Parent(s): 6802ffc

added html with screenshot

Browse files
Files changed (1) hide show
  1. index.js +25 -21
index.js CHANGED
@@ -77,12 +77,12 @@ const getData = async (url) => {
77
  };
78
  };
79
 
80
- const getHTML = async (url,headers=null) => {
81
-
82
  const browser = await initBrowser;
83
  var page = await browser.newPage();
84
  page = await addRequestFilter(page);
85
- if(headers){
86
  // console.log("headers",headers);
87
  await page.setExtraHTTPHeaders(headers);
88
  }
@@ -94,7 +94,7 @@ const getHTML = async (url,headers=null) => {
94
  return await page.content();
95
 
96
  }
97
- const getScreenshot = async (url,headers=null) => {
98
 
99
  const browser = await initBrowser;
100
  var page = await browser.newPage();
@@ -106,8 +106,11 @@ const getScreenshot = async (url,headers=null) => {
106
  const image = await page.screenshot({
107
  type: "png",
108
  });
 
 
 
109
  page.close();
110
- return image;
111
 
112
  }
113
 
@@ -143,47 +146,48 @@ app.get("/testscreenshot", async (req, res) => {
143
  })
144
  app.post("/html", async (req, res) => {
145
  const data = req.body;
146
- if(!("url" in data)){
147
  res.type("json");
148
  return res.send(JSON.stringify({
149
- "error":"no url parameter in request",
150
  }));
151
  }
152
- const {url,headers} = data;
153
 
154
- try{
155
- const html = await getHTML(url,headers);
156
  res.type("json").send(JSON.stringify({
157
  html: html
158
  }));
159
  }
160
- catch(e){
161
  return res.type("json").send(JSON.stringify({
162
- "error":"can't open page",
163
  }));
164
  }
165
  })
166
  app.post("/screenshot", async (req, res) => {
167
  const data = req.body;
168
- if(!("url" in data)){
169
  return res.type("json").send(JSON.stringify({
170
- "error":"no url parameter in request",
171
  }));
172
  }
173
  const url = data['url'];
174
-
175
- try{
176
- const image = await getScreenshot(url);
177
  // convert buffer to base64 string
178
  const base64Image = await image.toString('base64');
179
-
180
  return res.type("json").send(JSON.stringify({
181
- "screenshot": "data:image/png;base64," + base64Image
 
182
  }));
183
  }
184
- catch(e){
185
  return res.type("json").send(JSON.stringify({
186
- "error":"can't open page",
187
  }));
188
  }
189
  })
 
77
  };
78
  };
79
 
80
+ const getHTML = async (url, headers = null) => {
81
+
82
  const browser = await initBrowser;
83
  var page = await browser.newPage();
84
  page = await addRequestFilter(page);
85
+ if (headers) {
86
  // console.log("headers",headers);
87
  await page.setExtraHTTPHeaders(headers);
88
  }
 
94
  return await page.content();
95
 
96
  }
97
+ const getScreenshot = async (url, headers = null) => {
98
 
99
  const browser = await initBrowser;
100
  var page = await browser.newPage();
 
106
  const image = await page.screenshot({
107
  type: "png",
108
  });
109
+
110
+ const html = await page.content();
111
+
112
  page.close();
113
+ return { image, html };
114
 
115
  }
116
 
 
146
  })
147
  app.post("/html", async (req, res) => {
148
  const data = req.body;
149
+ if (!("url" in data)) {
150
  res.type("json");
151
  return res.send(JSON.stringify({
152
+ "error": "no url parameter in request",
153
  }));
154
  }
155
+ const { url, headers } = data;
156
 
157
+ try {
158
+ const html = await getHTML(url, headers);
159
  res.type("json").send(JSON.stringify({
160
  html: html
161
  }));
162
  }
163
+ catch (e) {
164
  return res.type("json").send(JSON.stringify({
165
+ "error": "can't open page",
166
  }));
167
  }
168
  })
169
  app.post("/screenshot", async (req, res) => {
170
  const data = req.body;
171
+ if (!("url" in data)) {
172
  return res.type("json").send(JSON.stringify({
173
+ "error": "no url parameter in request",
174
  }));
175
  }
176
  const url = data['url'];
177
+
178
+ try {
179
+ const { image, html } = await getScreenshot(url);
180
  // convert buffer to base64 string
181
  const base64Image = await image.toString('base64');
182
+
183
  return res.type("json").send(JSON.stringify({
184
+ "base64": "data:image/png;base64," + base64Image,
185
+ "html":html,
186
  }));
187
  }
188
+ catch (e) {
189
  return res.type("json").send(JSON.stringify({
190
+ "error": "can't open page",
191
  }));
192
  }
193
  })