Anuj-Panthri commited on
Commit
6802ffc
·
1 Parent(s): 4f1ca17

added headers

Browse files
Files changed (1) hide show
  1. index.js +17 -16
index.js CHANGED
@@ -14,14 +14,11 @@ app.use(bodyParser.urlencoded({ extended: false }));
14
  app.use(mutler().array(""));
15
 
16
 
17
- const initBrowser = async () => {
18
- const browser = await puppeteer.launch({
19
- executablePath: process.env.CHROME_BIN || null,
20
- defaultViewport: null,
21
- headless: true,
22
- });
23
- return browser;
24
- }
25
 
26
  async function addRequestFilter(page) {
27
 
@@ -44,7 +41,7 @@ async function addRequestFilter(page) {
44
  const getData = async (url) => {
45
 
46
  // Open a new page
47
- const browser = await initBrowser();
48
  var page = await browser.newPage();
49
  page = await addRequestFilter(page);
50
 
@@ -80,11 +77,15 @@ const getData = async (url) => {
80
  };
81
  };
82
 
83
- const getHTML = async (url) => {
84
-
85
- const browser = await initBrowser();
86
  var page = await browser.newPage();
87
  page = await addRequestFilter(page);
 
 
 
 
88
 
89
  await page.goto(url, {
90
  // waitUntil: "domcontentloaded",
@@ -93,9 +94,9 @@ const getHTML = async (url) => {
93
  return await page.content();
94
 
95
  }
96
- const getScreenshot = async (url) => {
97
 
98
- const browser = await initBrowser();
99
  var page = await browser.newPage();
100
 
101
  await page.goto(url, {
@@ -148,10 +149,10 @@ app.post("/html", async (req, res) => {
148
  "error":"no url parameter in request",
149
  }));
150
  }
151
- const url = data['url'];
152
 
153
  try{
154
- const html = await getHTML(url);
155
  res.type("json").send(JSON.stringify({
156
  html: html
157
  }));
 
14
  app.use(mutler().array(""));
15
 
16
 
17
+ const initBrowser = puppeteer.launch({
18
+ executablePath: process.env.CHROME_BIN || null,
19
+ defaultViewport: null,
20
+ headless: true,
21
+ });
 
 
 
22
 
23
  async function addRequestFilter(page) {
24
 
 
41
  const getData = async (url) => {
42
 
43
  // Open a new page
44
+ const browser = await initBrowser;
45
  var page = await browser.newPage();
46
  page = await addRequestFilter(page);
47
 
 
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
+ }
89
 
90
  await page.goto(url, {
91
  // waitUntil: "domcontentloaded",
 
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();
101
 
102
  await page.goto(url, {
 
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
  }));