Skip to main content
Known Participant
June 15, 2020
Question

i am not able to call more than one function which returns a value using csInterface in js

  • June 15, 2020
  • 3 replies
  • 693 views

i have already called a function in js to fetch a value from jsx using csInterface but i am not able to call another function which returns something using csInterface.please suggest what should i do

This topic has been closed for replies.

3 replies

Braniac
June 16, 2020

I am not very hopeful that a platform issue might happen here. I don't have access to WIN box at the moment so can't verify. However the question remains is this a settimeout issue or a csinterface issue. Did you try what i suggested? Just make plain simple two calls sequentially for getText and then check method. Are you even able to get both the methods called even in this case? If not then it is an InDesign problem, else it means it has something to do with your settimeout or callback chaining, and that is more of a JS question.

 

Anyhow in the current state i don't get what workflow you are trying to make, why is check not in the callback of getText, is it dependent on its result or not, why did you use settimeout in the first place, why is it needed

 

-Manan

vaishnaviAuthor
Known Participant
June 21, 2020

ok thanks for replying...i used json method for retuning but wanted to ask if instead of res if i use another function in callback like check in callback of gettext then check will take the ouput of gettext function?

Braniac
June 15, 2020

Ok so you mean to say check method is called but the return value is not what you expect and its blank or undefined, right? If so then we will need to have a look at your jsx code for check method, that should be the culpritm csinterface is working fine on sending your call to jsx engine. Paste the snippet for check method, i will have a look

 

-Manan

vaishnaviAuthor
Known Participant
June 15, 2020
function check()
{
    return "1";
}

there is nothing in check method just for now i have simple 1 still its not returning any value.

Braniac
June 15, 2020

If this is so simple then i think i would have to look at your whole extension. Please upload a stripped version of the whole extension folder on Dropbox or other cloud service and post the public url here. The code you posted previously looks strange to me, you have added the callback function to the csinterface call, then the other code seems to be out of the method, it seems incorrect to me. However if i have the whole extension code i can look into it. Do mind include only the specific code needed and give instructions on how to reproduce the issue

 

-Manan

Braniac
June 15, 2020

This does not give you anything to help you. Tell us what do you call? How do you call? Do you mean you are just able to call only a particular method using csinterface, or that you are only able to make just one method call in a session?

Paste code snippets, explain it better so that we can understand the problem.

 

-Manan

vaishnaviAuthor
Known Participant
June 15, 2020
in this snippet i am not able to get returned value from check fn. but i have got returned value from gettext .why is it so when i have used the same process.check() function returns 1 or 0  from jsx code.actually the other fns used do not retiurn any value so they work well but as i have a secong fn i.e check which returns a value it does not work
 

 

 

 

function translate()
{
	csInterface.evalScript("getText()", function(res)
	{
		var t='?translator_id='+document.getElementById("f").value;
		//alert(t);
		var v='&target='+document.getElementById("g").value;
		//alert(v);
		var url='http://localhost:3000/translate'+t+v;
		//var url = 'http://localhost:5000/name?pd=1&target=fr' ;
		var para = res.split("\r");
		for(var i=para.length;i>=0;i--)
		{
			if(para[i]==undefined)
				continue;
			//csInterface.evalScript("alert('" + para[i] + "')");
			var da = JSON.stringify({ "data" : para[i] });
			var xhr = new XMLHttpRequest();
			xhr.open('POST', url, false);
			xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
			xhr.onload = function (e)
			{
				if (this.status == 200 || this.status == 304)
				{
					var lan = document.getElementById("g").value;
					if(lan=="HI" || lan=="BN" || lan=="AR" || lan=="ZH" || lan=="GU" || lan=="KO" ||lan=="JA" || lan=="ML"|| lan=="MI" || lan=="NE" || lan=="MN" || lan=="PA" || lan=="MR" || lan=="TA" || lan=="TO" ||lan=="BO" || lan=="UR"){
						csInterface.evalScript("printTranslation('"+ this.responseText + "','" + i + "')");
					}
					else
					{
						csInterface.evalScript("translation('"+ this.responseText + "','" + i + "')");
					}
					
				}
				else
					csInterface.evalScript("alert('Server error: " + this.status + "')");
			};
			xhr.send(da);
		}
		csInterface.evalScript("alert('Translation Complete!')");
	});
	setTimeout(function(){
            			csInterface.evalScript("check()", function(data)
            			{
							if(data==1)
							{
								csInterface.evalScript("alert('" + this.data + "')");
							}
							else{
								csInterface.evalScript("alert('" + this.data + "')");
								csInterface.evalScript("alert('nothing')");
							}

            			});
					  }.bind(this), 1000);
	
}