js: importKey feedback refactor
[gpgme.git] / lang / js / DemoExtension / maindemo.js
1 /* gpgme.js - Javascript integration for gpgme
2  * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GPGME is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1+
19  *
20  * Author(s):
21  *     Maximilian Krambach <mkrambach@intevation.de>
22  */
23
24 /* global document, Gpgmejs */
25
26 document.addEventListener('DOMContentLoaded', function() {
27     Gpgmejs.init().then(function(gpgmejs){
28         document.getElementById('buttonencrypt').addEventListener('click',
29             function(){
30                 let data = document.getElementById('inputtext').value;
31                 let keyId = document.getElementById('pubkey').value;
32                 gpgmejs.encrypt(data, keyId).then(
33                     function(answer){
34                         if (answer.data){
35                             document.getElementById(
36                                 'answer').value = answer.data;
37                         }
38                     }, function(errormsg){
39                         alert( errormsg.message);
40                     });
41             });
42
43         document.getElementById('buttondecrypt').addEventListener('click',
44             function(){
45                 let data = document.getElementById('inputtext').value;
46                 gpgmejs.decrypt(data).then(
47                     function(answer){
48                         if (answer.data){
49                             document.getElementById(
50                                 'answer').value = answer.data;
51                         }
52                     }, function(errormsg){
53                         alert(errormsg.message);
54                     });
55             });
56
57         document.getElementById('getdefaultkey').addEventListener('click',
58             function(){
59                 gpgmejs.Keyring.getDefaultKey().then(function(answer){
60                     document.getElementById('pubkey').value =
61                         answer.fingerprint;
62                 }, function(errormsg){
63                     alert(errormsg.message);
64                 });
65             });
66
67         document.getElementById('signtext').addEventListener('click',
68             function(){
69                 let data = document.getElementById('inputtext').value;
70                 let keyId = document.getElementById('pubkey').value;
71                 gpgmejs.sign(data, keyId).then(
72                     function(answer){
73                         if (answer.data){
74                             document.getElementById(
75                                 'answer').value = answer.data;
76                         }
77                     }, function(errormsg){
78                         alert( errormsg.message);
79                     });
80             });
81
82         document.getElementById('verifytext').addEventListener('click',
83             function(){
84                 let data = document.getElementById('inputtext').value;
85                 gpgmejs.verify(data).then(
86                     function(answer){
87                         let vals = '';
88                         if (answer.all_valid === true){
89                             vals = 'Success! ';
90                         } else {
91                             vals = 'Failure! ';
92                         }
93                         vals = vals + (answer.count - answer.failures) + 'of '
94                             + answer.count + ' signature(s) were successfully '
95                             + 'verified.\n\n' + answer.data;
96                         document.getElementById('answer').value = vals;
97                     }, function(errormsg){
98                         alert( errormsg.message);
99                     });
100             });
101         document.getElementById('searchkey').addEventListener('click',
102             function(){
103                 let data = document.getElementById('inputtext').value;
104                 gpgmejs.Keyring.getKeys(data, true, true).then(function(keys){
105                     if (keys.length === 1){
106                         document.getElementById(
107                             'pubkey').value = keys[0].fingerprint;
108                     } else if (keys.length > 1) {
109                         alert('The pattern was not unambigious enough for a Key. '
110                         + keys.length + ' Keys were found');
111                     } else {
112                         alert('No keys found');
113                     }
114                 }, function(errormsg){
115                     alert( errormsg.message);
116                 });
117             });
118     });
119 });