'use strict'

const chromeLauncher = require('chrome-launcher')
const path = require('path')

// borrow from: http://qiita.com/saekis/items/c2b41cd8940923863791
function escapeHtml (string) {
  if (typeof string !== 'string') {
    return string
  }
  return string.replace(/[&'`"<>]/g, function (match) {
    return {
      '&': '&amp;',
      '\'': '&#x27;',
      '`': '&#x60;',
      '"': '&quot;',
      '<': '&lt;',
      '>': '&gt;',
    }[match]
  })
}

function escapeSingleQuote (string) {
  if (typeof string !== 'string') {
    return string
  }
  return string.replace(/'/g, '\\\'')
}

async function createChromeLauncher (startingUrl, options) {
  const flags = []
  let chromeInstance

  // Lighthouse adds '--disable-setuid-sandbox' flag automatically.
  // The flag causes an error on linux when staring headless chrome.
  // '--no-sandbox' suppresses an error caused by '--disable-setuid-sandbox'.
  if (process.platform === 'linux') {
    flags.push('--no-sandbox')
  }
  if (!options.visible) {
    flags.push('--headless')
    flags.push('--hide-scrollbars')
  }
  if (options.chromeFlags && Array.isArray(options.chromeFlags)) {
    options.chromeFlags.forEach(f => {
      if (f.indexOf('--') === -1) {
        throw new Error('An item of chromFlags option must have "--" at start of itself. the value: ' + f)
      }
      flags.push(f)
    })
  }
  if (options.additionalChromeFlags && Array.isArray(options.additionalChromeFlags)) {
    console.warn('[chromy] additionalChromeFlags is deprecated. Use chromeFlags instead of this.')
    options.additionalChromeFlags.forEach(f => {
      if (f.indexOf('--') === -1) {
        throw new Error('An item of chromFlags option must have "--" at start of itself. the value: ' + f)
      }
      flags.push(f)
    })
  }
  const params = {
    port: options.port,
    chromeFlags: flags,
    startingUrl: startingUrl,
    logLevel: 'error',
    enableExtensions: options.enableExtensions,
    handleSIGINT: false,
  }
  if (options.chromePath) {
    params.chromePath = options.chromePath
  }
  if (options.userDataDir) {
    params.userDataDir = options.userDataDir
  }

  chromeInstance = await chromeLauncher.launch(params)

  return chromeInstance
}

function completeUrl (url) {
  const reg = new RegExp('^[a-zA-Z0-9]+:', 'i')
  const regAbbr = new RegExp('^//', 'i')
  if (reg.test(url)) {
    return url
  } else if (regAbbr.test(url)) {
    return 'http:' + url
  } else {
    return 'file://' + path.join(process.cwd(), url)
  }
}

exports.escapeHtml = escapeHtml
exports.escapeSingleQuote = escapeSingleQuote
exports.createChromeLauncher = createChromeLauncher
exports.completeUrl = completeUrl


@@ -87,16 +87,510 @@
 path')%0A%0A
+const DEFAULT_ARGS = %5B%0A  '--disable-background-networking',%0A  '--disable-background-timer-throttling',%0A  '--disable-client-side-phishing-detection',%0A  '--disable-default-apps',%0A  '--disable-hang-monitor',%0A  '--disable-popup-blocking',%0A  '--disable-prompt-on-repost',%0A  '--disable-sync',%0A  '--enable-automation',%0A  '--enable-devtools-experiments',%0A  '--metrics-recording-only',%0A  '--no-first-run',%0A  '--password-store=basic',%0A  '--safebrowsing-disable-auto-update',%0A  '--use-mock-keychain',%0A%5D;%0A%0A
 // borro
@@ -1165,16 +1165,37 @@
 ags = %5B%5D
+.concat(DEFAULT_ARGS)
 %0A  let c
@@ -1552,16 +1552,16 @@
 dless')%0A
-
     flag
@@ -1588,16 +1588,47 @@
 lbars')%0A
+    flags.push('--mute-audio')%0A
   %7D%0A  if

Add puppeteer default flags