UNPKG

3.18 kBJavaScriptView Raw
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
17const {helper, assert} = require('./helper');
18
19class Dialog {
20 /**
21 * @param {!Puppeteer.CDPSession} client
22 * @param {string} type
23 * @param {string} message
24 * @param {(string|undefined)} defaultValue
25 */
26 constructor(client, type, message, defaultValue = '') {
27 this._client = client;
28 this._type = type;
29 this._message = message;
30 this._handled = false;
31 this._defaultValue = defaultValue;
32 }
33
34 /**
35 * @return {string}
36 */
37 type() {
38 return this._type;
39 }
40
41 /**
42 * @return {string}
43 */
44 message() {
45 return this._message;
46 }
47
48 /**
49 * @return {string}
50 */
51 defaultValue() {
52 return this._defaultValue;
53 }
54
55 /**
56 * @param {string=} promptText
57 */
58 /* async */ accept(promptText) {return (fn => {
59 const gen = fn.call(this);
60 return new Promise((resolve, reject) => {
61 function step(key, arg) {
62 let info, value;
63 try {
64 info = gen[key](arg);
65 value = info.value;
66 } catch (error) {
67 reject(error);
68 return;
69 }
70 if (info.done) {
71 resolve(value);
72 } else {
73 return Promise.resolve(value).then(
74 value => {
75 step('next', value);
76 },
77 err => {
78 step('throw', err);
79 });
80 }
81 }
82 return step('next');
83 });
84})(function*(){
85 assert(!this._handled, 'Cannot accept dialog which is already handled!');
86 this._handled = true;
87 (yield this._client.send('Page.handleJavaScriptDialog', {
88 accept: true,
89 promptText: promptText
90 }));
91 });}
92
93 /* async */ dismiss() {return (fn => {
94 const gen = fn.call(this);
95 return new Promise((resolve, reject) => {
96 function step(key, arg) {
97 let info, value;
98 try {
99 info = gen[key](arg);
100 value = info.value;
101 } catch (error) {
102 reject(error);
103 return;
104 }
105 if (info.done) {
106 resolve(value);
107 } else {
108 return Promise.resolve(value).then(
109 value => {
110 step('next', value);
111 },
112 err => {
113 step('throw', err);
114 });
115 }
116 }
117 return step('next');
118 });
119})(function*(){
120 assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
121 this._handled = true;
122 (yield this._client.send('Page.handleJavaScriptDialog', {
123 accept: false
124 }));
125 });}
126}
127
128Dialog.Type = {
129 Alert: 'alert',
130 BeforeUnload: 'beforeunload',
131 Confirm: 'confirm',
132 Prompt: 'prompt'
133};
134
135module.exports = {Dialog};
136helper.tracePublicAPI(Dialog);