1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const {helper, assert} = require('./helper');
|
18 |
|
19 | class Dialog {
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
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 |
|
36 |
|
37 | type() {
|
38 | return this._type;
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 | message() {
|
45 | return this._message;
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 | defaultValue() {
|
52 | return this._defaultValue;
|
53 | }
|
54 |
|
55 | |
56 |
|
57 |
|
58 | 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 | 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 |
|
128 | Dialog.Type = {
|
129 | Alert: 'alert',
|
130 | BeforeUnload: 'beforeunload',
|
131 | Confirm: 'confirm',
|
132 | Prompt: 'prompt'
|
133 | };
|
134 |
|
135 | module.exports = {Dialog};
|
136 | helper.tracePublicAPI(Dialog);
|