<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">%% 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 =&gt; e is an error message
	1 =&gt; e indicates success
	2 =&gt; 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 =&gt; the endpoint to contact
	closure =&gt; is called with the received javascript object
	keypath =&gt; the keypath of a property in the javascript object received that should be displayed in a textfield
	payload =&gt; a javascript object to send in the body, as part of the request
	name =&gt; the name of the textfield, where the property defined by keypath should be displayed
	func =&gt; GET or POST
	errors =&gt; a javascript object that defines error messages 
		example: { "404" : "The requested resource does not exist." }
	doc =&gt; 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('{') &gt;= 0 &amp;&amp; url.indexOf('}') &gt;= 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 = /(.*?)(&amp;.*?;)/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) =&gt; { 
				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}</pre></body></html>