const axios = require('axios');
const puppeteer = require('puppeteer');
const injectJquery = require('../src/lib/injectJQuery');
const timeout = require('../src/lib/timeout');

const URL = 'https://www.amazon.com.au/gp/site-directory/';

async function main() {
  const browser = await puppeteer.launch({
    headless: false,
    devtools: process.env.DEV_TOOLS,
  });
  const page = await browser.newPage();
  const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 ' +
    '(KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36';
  const response = await axios.get(URL, {
    headers: {
      'user-agent': UA,
      accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
      'accept-language': 'en-AU,en-GB;q=0.9,en-US;q=0.8,en;q=0.7',
    },
  });

  // await page.waitFor('div,span');
  await page.setRequestInterception(true);

  page.on('request', (interceptedRequest) => {
    if (interceptedRequest.url.match(/.(png|jpg|gif|jpeg|bmp|ico|mp4|mp3|mp2|js)$/)) {
      interceptedRequest.abort();
    } else {
      interceptedRequest.continue();
    }
  });

  await page.evaluate((html) => {
    document.documentElement.innerHTML = html;
  }, response.data);

  // await page.setContent(response.data);

  await injectJquery(page);

  // await page.waitFor('div,span');

  // await page.setContent('');

  // await page.waitFor('body');

  // await page.setContent(response.data);

  // await timeout(2000);

  await page.evaluate(() => {
    console.log(window.$scraperJS('#shopAllLinks a'));
  });
}

main();
