a9fdf28c30246c78625f1798fec2d9e5a20dc211
[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 <https://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: data, publicKeys: keyId, armor: true })
33                     .then(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: 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: data, keys: 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: 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({
105                     pattern: data,
106                     prepare_sync: true,
107                     search: true }
108                 ).then(function (keys){
109                     if (keys.length === 1){
110                         document.getElementById(
111                             'pubkey').value = keys[0].fingerprint;
112                     } else if (keys.length > 1) {
113                         alert('The pattern was not unambigious enough for a Key. '
114                         + keys.length + ' Keys were found');
115                     } else {
116                         alert('No keys found');
117                     }
118                 }, function (errormsg){
119                     alert( errormsg.message);
120                 });
121             });
122     });
123 });