summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/javascripthttp/javascripthttp.sty
blob: 68a89a830a347c2b424517dc4470c7ac1f4b47b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
%% javascripthttp.sty
%% Copyright 2023 Cedric V. Zwahlen
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Cedric V. Zwahlen
%
% This work consists of the files javascripthttp.sty and javascripthttp-doc.tex
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{javascripthttp}[JavascriptHTTP Package, Version 1.1]

\RequirePackage{setspace}
\RequirePackage{insdljs}
\RequirePackage{xparse}

\newcommand{\SimpleButtonPOST}[7][\{\}] {
\PushButton[name=#2, onclick={ processSimpleButton(#4,'POST',#5,#7,#6,#1); }]{\sffamily \large #3}
}

\newcommand{\SimpleTextFieldReadonly}[1] {
\TextField[name=#1,width=29em,height=2em,bordercolor={0.5 .5 .5},readonly=true]{}
}

\newcommand{\SimpleTextFieldShortReadonly}[1] {
\TextField[name=#1,width=9em,height=2em,bordercolor={0.5 .5 .5},readonly=true]{}
}

\newcommand{\SimpleTextFieldMultilineReadonly}[1] {
\TextField[name=#1,width=29em,height=4em,bordercolor={0.5 .5 .5},readonly=true,multiline=true]{}
}

\newcommand{\SimpleTextField}[1] {
\TextField[name=#1,width=29em,height=2em,bordercolor={0.5 .5 .5}]{}
}

\newcommand{\ErrorField} {
\TextField[name=errorField,width=29em,height=2em,bordercolor={0.0 .0 .0},readonly=true]{}
}

% new

\NewDocumentCommand{\SimpleGET}{v v +v +v o}{

	\IfNoValueTF{#5}
		{ \PushButton[name=#1, onclick={ processSimpleButton(#3,'GET','','',#4,{}); }]{\sffamily \large #2} }
		{ \PushButton[name=#1, onclick={ processSimpleButton(#3,'GET','#5','',#4,{}); }]{\sffamily \large #2} }


}

\NewDocumentCommand{\SimpleClosure}{v v +v}{
	
	\PushButton[name=#1, onclick={ this["simpleClosure"] = #3; this["simpleClosure"](this); }]{\sffamily \large #2}

}

\NewDocumentCommand{\SimplePOST}{v v +v +v +v +v o}{

	\IfNoValueTF{#7} 
		{ \PushButton[name=#1, onclick={ processSimpleButton(#3,'POST','#6',#5,#4,{}); }]{\sffamily \large #2} }
		{ \PushButton[name=#1, onclick={ processSimpleButton(#3,'POST','#7',#5,#4,#6); }]{\sffamily \large #2} }
	
}




\begin{insDLJS}{test}{JavaScript}

/*
Displays a message e in a textfield named 'errorField'. If no such textfield exists and sentiment is 0, an alert is displayed instead.
The parameter sentiment indicates the nature of the message e. If sentiment is:

	0 => e is an error message
	1 => e indicates success
	2 => e is neutral
	
*/
function state(e, sentiment) {
	var f = this.getField("errorField")
	
	if (f != null) {
	
		f.value = e;
	
		if (sentiment == 0) { f.borderColor = ["RGB",0.8,0,0]; f.lineWidth = 1; }
		if (sentiment == 1) { f.borderColor = ["RGB",0,0.6,0]; f.lineWidth = 1; }
		if (sentiment == 2) { f.borderColor = ["RGB",1,1,1]; f.lineWidth = 0; }
	
	} else {
		if (sentiment == 0) {
			app.alert(e);
		}	
	}
}

/*
Attempts to retrieve a JSON object from a REST API endpoint.

	url => the endpoint to contact
	closure => is called with the received javascript object
	keypath => the keypath of a property in the javascript object received that should be displayed in a textfield
	payload => a javascript object to send in the body, as part of the request
	name => the name of the textfield, where the property defined by keypath should be displayed
	func => GET or POST
	errors => a javascript object that defines error messages 
		example: { "404" : "The requested resource does not exist." }
	doc => a reference to this
	
	requires adobe acrobat pro, and the pdf needs to exist in a trusted folder

*/
function getJSONFrom(url,closure,keypath,payload,name,func,errors,doc)
{	

	doc["httpjavascripts_speedTest_t0"] = new Date();
	
	var o = {};
	
	if (payload != '') {
		o = payload();
	}

    	var params = 
		{
			cVerb: func,
			cURL: url,
			oRequest: util.streamFromString(JSON.stringify(o), "utf-8"),
			oHandler: 
			{
				response: function(resp,uri,e) 
				{
								
					if (e != undefined) {
					
						const known = errors[e.error];
						
						if (known != undefined) {
							state(known,0);
						} else {
							
							// still perform some basic checks
							
							state(e.text,0);
							
						}
						
					} else {
					
						const raw = util.stringFromStream(resp);
				
						const json = JSON.parse(raw);
						
						state("",2);	
						const toDisplay = closure(json,keypath,doc);
												
						if (toDisplay != undefined) {
							doc.display(toDisplay,name,doc);
						}
					}	
				}
			}
		}

	
		Net.HTTP.request(params);		
}
/*
Preprocesses the URL, and then passes the data to getJSONFrom function.

if the URL is a function, then that function is executed. This could be a security concern if the origin of the document is not known.
if the URL contains an interpolated link, the interpolated elements are resolved.
	example for an interpolated link:
	
	"https://api.datamuse.com/words?rel_rhy={xyz}"
	
	where xyz is the name of a textfield.

otherwise, a string is expected.

*/
function processSimpleButton(url, method, name, payload, keypath, errors)
{	

	var resolved = url;
	
	if (typeof url != 'string') { 
		resolved = url(); // may be a security concern
	} else if ( url.indexOf('{') >= 0 && url.indexOf('}') >= 0 ) {
	
		const re = /[^{}]*({(\w*)})+/g;
	
		var m;
		
		var stitch = 0;
		var res = '';
	
		do {
    		 	m = re.exec(resolved);
    			if (m) {
       				
				const f = this.getField(m[2]);
								
				if (f != null) {
					stitch += m[0].length;
					res += m[0].substring(0, m[0].length - m[1].length) + f.value;
				} else {
					console.println("DEBUG HINT: The textfield used for the interpolation of the URL string could not be found.");
				}
				
    			}
			
		} while (m);
		
		const appendix = url.substring(stitch);
		resolved = res;
	
	}
	
	getJSONFrom(resolved, extractKeypath, keypath, payload, name, method, errors, this);
}

function removeHTML(str) {

	const re = /(.*?)(&.*?;)/g;
	
		var m;
		
		var stitch = 0;
		var res = '';
	
		do {
    		 	m = re.exec(str);
    			if (m) {
       				
				console.println(m[0]);
				console.println(m[1]);
				
				stitch += m[0].length;
				res += m[1];
			
    			}
			
		} while (m);
		
		return res + str.substring(stitch);
}

/*
Extracts a property of a javascript object based on the provided keypath.

	Example: the keypath 'rst.xyz' extracts the value of the property xyz from the object abc:
	
		const abc = { rst : { xyz = 123 } };
		
	elements of an array can be accessed via ascending numbers, for example the keypath '0.xyz' would access the first element of an array, an then extract the value of xyz of that first object.

*/
function extractKeypath(json,keypath,doc) {
		
	if (typeof keypath == 'function') {
		return keypath(json,doc);
	}
	
	if (typeof keypath == 'string') {
	
		const fun = doc[keypath];
				
		if (typeof fun == 'function') {
			return fun(); // may be a security concern
		} else {
		
			return keypath.split('.').reduce((previous, current) => { 
				const parsed = parseInt(current);  
				
				if (previous == undefined) {
					console.println("DEBUG HINT: Keypath may not match the json object, or a referenced textfield is empty.");
					return '';
				}
			
				if (isNaN(parsed)) { 
					return previous[current]; 
				} else { 
					return previous[parsed]; 
				} 
			
			}, json);
		}
	}	
}

/*
Displays a text in a textfield, provided the textfield exists.
*/
function display(text,name,doc) {
	
	var f = doc.getField(name);
	
	if (f != undefined) {
		f.value = text;
	}
}

\end{insDLJS}