// try{
//   console.log("xxxxx");
//   x=xxxxx/0;
//   console.log("yyyyyy");
// }catch(e){
//   console.log("xxxxxxxxxxxxxxxx");
//   console.log(e.stack);
// }
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
(async () => {
   const browser = await puppeteer.launch({
      headless:false,
      args: ['--no-sandbox'],
  //    slowMo: 1000,
      //devtools: true
      //timeout: 10000,
    });
  // browser.on('targetcreated',d=>{
  //   console.log(d);
  // })
  // browser.on('targetchanged',d=>{
  //   console.log(d);
  // });
  const page = await browser.newPage();
  // console.log(iPhone);
  // await page.emulate(iPhone);
  // page.on('console', msg => console.log('PAGE LOG:', msg.text()));
  // page.on('pageerror', err => console.log(err));
  // page.on('dialog', dlg => {
  //   console.log(dlg.message());
  //   dlg.dismiss();
  //   });
  await page.goto('https://www.baidu.com');
  // var m=await page.evaluate(()=>{
  //   console.log("vvvvvvvvvvvvvvvvvvvvvv");
  //   window.alert("hello");
  //   return "xxxxx";
  // });
  // var imgs=await page.waitForXPath('//img')
  // imgs.$eval('img',(node)=>{
  //   console.log(node);
  // });
  var inp=await page.$("#kw");
  const watchDog = page.waitForFunction('window.innerWidth < 100');
  await page.setViewport({width: 50, height: 50});
  await watchDog;


  const client = await page.target().createCDPSession();
  await client.send('Animation.enable');
  client.on('Animation.animationCreated', () => console.log('Animation created!'));
  const response = await client.send('Animation.getPlaybackRate');
  console.log('playback rate is ' + response.playbackRate);
  await client.send('Animation.setPlaybackRate', {
    playbackRate: response.playbackRate / 2
  });
  // var ei=await page.$("#kw");
  // await ei.type("hello");
  // var su=await page.$("#su");
  // await su.click();
  // console.log(m);
  // const aHandle = await page.evaluateHandle(() => document.body);
  // const resultHandle = await page.evaluateHandle(body => body.innerHTML, aHandle);
  // console.log(await resultHandle.jsonValue());
  // await resultHandle.dispose();

  //await page.$('https://www.baidu.com');
  // await page.goto('https://www.baidu.com');
  // await page.evaluate(() => window.open('https://www.bing.com/'));

  //console.log(newWindowTarget.browser==browser);
  //   // 创建一个匿名的浏览器上下文
  // const context = await browser.createIncognitoBrowserContext();
  // // // // // 在一个原生的上下文中创建一个新页面
  // const page2 = await context.newPage();
  // var ps=await browser.pages();
  // console.log(ps.length);
  // await page.mouse.move(0, 0);
  // await page.mouse.down();
  // await page.mouse.move(0, 100);
  // await page.mouse.move(100, 100);
  // await page.mouse.move(100, 0);
  // await page.mouse.move(0, 0);
  // await page.mouse.up();
  // console.log(ps);
  // await page2.goto('https://www.baidu.com');
  // await page.screenshot({path: 'example.png'});
  // var page2=await browser.defaultBrowserContext().newPage();
  //await page2.goto('https://www.baidu.com');
  //await browser.close();
})();
//生成pdf
// (async () => {
//   const browser = await puppeteer.launch({
//     args: ['--no-sandbox'],
//     timeout: 10000,
//   });
//   const page = await browser.newPage();
//   await page.goto('https://www.163.com');
//   await page.pdf({path: 'hn.pdf', format: 'A4'});
//   await browser.close();
// })();
//在文档上下文里执行脚本，并返回结果
// (async () => {
//   const browser = await puppeteer.launch({
//       args: ['--no-sandbox'],
//       timeout: 10000,
//       headless: false,
//       devtools: true,
//     });
//   const page = await browser.newPage();
//
//   await page.goto('http://www.163.com');
//   page.on('console', msg => console.log('PAGE LOG:', msg.text()));
//   // Get the "viewport" of the page, as reported by the page.
//   const dimensions = await page.evaluate(() => {
//     console.log("xxxxxxxxx");
//     return {
//       width: document.documentElement.clientWidth,
//       height: document.documentElement.clientHeight,
//       deviceScaleFactor: window.devicePixelRatio,
//       slowMo: 20000,
//     };
//   });
//
//   console.log('Dimensions:', dimensions);
//
//   await browser.close();
// })();
// (async () => {
//   const browser = await puppeteer.launch({
//     args: ['--no-sandbox'],
//     timeout: 10000,
//     headless: false,
//     devtools: true,
//     //args: [ '--proxy-server=127.0.0.1:9876' ]
//   });
//   const page = await browser.newPage();
//   await page.goto('https://image.baidu.com/');
//   console.log('go to https://image.baidu.com/');
//
//   await page.setViewport({
//     width: 1920,
//     height: 1080
//   });
//   console.log('reset viewport');
//   await page.focus('#kw');
//   await page.keyboard.sendCharacter('狗');
//   await page.$eval('.s_btn',el=>{
//     el.click();
//   });
//   page.on('load', async () => {
//     console.log("hello load..........");
//     console.log('go to search list');
//     const srcs = await page.evaluate(() => {
//       const images = document.querySelectorAll('img.main_img');
//       return Array.prototype.map.call(images, img => img.src);
//     });
//     console.log(`get ${srcs.length} images, start download`);
//   });
//
//   //await browser.close();
//
// })();
