# Cas d'usages et configurations avancées

# Audit d'un site nécessitant un cookie

Obligation de créer cookie pour fermer une popin, par exemple.

# Utilisation du fichier extra-header.json

{ "Cookie": "monster=blue", "x-men": "wolverine" }

# Utilisation du fichier input-file.json

{
  "extra-header": {
    "Cookie": "monster=blue",
    "x-men": "wolverine"
  }
  // ...
}

# Audit d'un site à accès sécurisé

# En mode simple (liste d'URLs)

npx lighthouse-plugin-ecoindex collect -u https://greenit.eco/ -u https://greenit.eco/wp-login.php/ -u https://greenit.eco/wp-admin/plugins.php --auth.url %test_url% --auth.user.target '#user_login' --auth.user.value %username_value% --auth.pass.target '#user_pass' --auth.pass.value %password_value% -o html

# En mode mesure de parcours (avec le fichier de config JSON)

{
"$schema": "/workspace/docs/static/schema/5.1/schema.json",
  "extra-header": {
    "Cookie": "monster=blue",
    "x-men": "wolverine"
  },
  "output": ["html", "json", "statement"],
  "user-agent": "random",
  "output-path": "./reports/multi",
  "auth": {
    "url": "https://domain.ltd/login/",
    "user": {
      "target": "#user_login",
      "value": "******"
    },
    "pass": {
      "target": "#user_pass",
      "value": "*****"
    }
  },
  "courses": [...]
}
// https://pptr.dev/guides/configuration
// https://github.com/GoogleChrome/lighthouse-ci/blob/main/docs/configuration.md#puppeteerscript
/**
 * @param {puppeteer.Browser} browser
 * @param {{url: string, options: LHCI.CollectCommand.Options}} context
 */
module.exports = async (browser, context) => {
  // launch browser for LHCI
  const page = await browser.newPage(context.options)
  const session = await page.target().createCDPSession()
  await page.goto(context.url, { waitUntil: 'networkidle0' })
  await startEcoindexPageMesure(page, session)
  await endEcoindexPageMesure()
  // close session for next run
  await page.close()
}

async function startEcoindexPageMesure(page, session) {
  page.setViewport({
    width: 1920,
    height: 1080,
  })
  await new Promise(r => setTimeout(r, 3 * 1000))
  const dimensions = await page.evaluate(() => {
    var body = document.body,
      html = document.documentElement

    var height = Math.max(
      body.scrollHeight,
      body.offsetHeight,
      html.clientHeight,
      html.scrollHeight,
      html.offsetHeight,
    )
    return {
      width: document.documentElement.clientWidth,
      height: height,
      deviceScaleFactor: window.devicePixelRatio,
    }
  })
  // console.log('dimensions', dimensions)
  // We need the ability to scroll like a user. There's not a direct puppeteer function for this, but we can use the DevTools Protocol and issue a Input.synthesizeScrollGesture event, which has convenient parameters like repetitions and delay to somewhat simulate a more natural scrolling gesture.
  // https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-synthesizeScrollGesture
  await session.send('Input.synthesizeScrollGesture', {
    x: 100,
    y: 600,
    yDistance: -dimensions.height,
    speed: 1000,
  })
}

/**
 * End Ecoindex flow. Wait 3s.
 */
async function endEcoindexPageMesure() {
  await new Promise(r => setTimeout(r, 3 * 1000))
}