1 | /**
|
2 | * Copyright 2017 Google Inc. All rights reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | ;
|
18 |
|
19 | const puppeteer = require('puppeteer');
|
20 |
|
21 | (async() => {
|
22 | const browser = await puppeteer.launch();
|
23 | const page = await browser.newPage();
|
24 |
|
25 | // Define a window.onCustomEvent function on the page.
|
26 | await page.exposeFunction('onCustomEvent', e => {
|
27 | console.log(`${e.type} fired`, e.detail || '');
|
28 | });
|
29 |
|
30 | /**
|
31 | * Attach an event listener to page to capture a custom event on page load/navigation.
|
32 | * @param {string} type Event name.
|
33 | * @return {!Promise}
|
34 | */
|
35 | function listenFor(type) {
|
36 | return page.evaluateOnNewDocument(type => {
|
37 | document.addEventListener(type, e => {
|
38 | window.onCustomEvent({type, detail: e.detail});
|
39 | });
|
40 | }, type);
|
41 | }
|
42 |
|
43 | await listenFor('app-ready'); // Listen for "app-ready" custom event on page load.
|
44 |
|
45 | await page.goto('https://www.chromestatus.com/features', {waitUntil: 'networkidle2'});
|
46 |
|
47 | await browser.close();
|
48 | })();
|