Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

is Cross policy an issue with puppeteer / headless chrome?

const fetch = require('node-fetch')

const requestInterceptor = async (request) => {
  try {
    const url = request.url()
    const requestHeaders = request.headers()
    const acceptHeader = requestHeaders.accept || ''
    if (url.includes("example.com") && (acceptHeader.includes('text/html'))) {
      const cookiesList = await page.cookies(url)
      const cookies = cookiesList.map(cookie => `${cookie.name}=${cookie.value}`).join('; ')
      delete requestHeaders['x-devtools-emulate-network-conditions-client-id']
      if (requestHeaders.Cookie) {
        requestHeaders.cookie = requestHeaders.Cookie
        delete requestHeaders.Cookie
      }
      const theseHeaders = Object.assign({'cookie': cookies}, requestHeaders, {'accept-language': 'en-US,en'})

      const init = {
        body: request.postData(),
        headers: theseHeaders,
        method: request.method(),
        follow: 20,
      }
      const result = await fetch(
        url,
        init,
      )
      const resultHeaders = {}
      result.headers.forEach((value, name) => {
        if (name.toLowerCase() !== 'content-security-policy') {
          resultHeaders[name] = value
        } else {
          console.log('CSP', `omitting CSP`, {originalCSP: value})
        }
      })
      const buffer = await result.buffer()
      await request.respond({
        body: buffer,
        resultHeaders,
        status: result.status,
      })
    } else {
      request.continue();
    }
  } catch (e) {
    console.log("Error while disabling CSP", e);
    request.abort();
  }
}

await page.setRequestInterception(true)
page.on('request', requestInterceptor)
Comment

PREVIOUS NEXT
Code Example
Python :: par e impar pygame 
Python :: hpw to create related model in django rest framework logic 
Python :: ipywidgets unobserve functools partial 
Python :: gdal user with anaconda 
Python :: which is best between c and python for making application 
Python :: lamda in f string 
Python :: if list is null python apply any function site:stackoverflow.com 
Python :: Percentage change between the current and the prior element. 
Python :: qtile: latest development version 
Python :: sns linear regression 
Python :: map dataframe parallel 
Python :: fibonci in python 
Python :: install matplotlib on nvidia jetson nx 
Python :: python multiprocessing queu empty error 
Python :: filter outside queryset in list django 
Python :: flask request file upload to dropbox 
Python :: pandas print nonzero in series 
Python :: python split files into even sets of folders 
Python :: mechanize python #11 
Python :: factorial python 
Python :: ffmpeg python slow down frame rate 
Python :: python argparse one or the other 
Python :: group by month and year 
Python :: loader.py line 19 in get_template 
Python :: how delet an obj from memori in python 
Python :: Pyturch training along with source code 
Python :: Command to install Voluptuous Python Library 
Python :: how to create dict key with list default -2 
Python :: meter replacement application 
Python :: python multi dimensional dict 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =