i am not able to call more than one function which returns a value using csInterface in js
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
there is nothing in check method just for now i have simple 1 still its not returning any value.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
actually initially code was
Copy link to clipboard
Copied
Upload the whole extension folder to dropbox and give the public access url here.
-Manan
Copy link to clipboard
Copied
https://www.dropbox.com/sh/7zxqc34bv243vwo/AADxRT_Aqpxo5wvA2JXCkWNHa?dl=0
thanks for your help...
Copy link to clipboard
Copied
Works fine for me, See the recording below
I suspect the issue that you might be facing is because of settimeout, maybe you are not giving the code a pause to execute the callback method. I hope you do know that JS is single threaded so at any point of time only one path of execution is run, so if a code piece is execting when settimeout callback is called it will wait for the current code to finish before executing the callback. I would suggest refactoring the code and avoid using settimeout if possible, if this check method has to be called after the gettext callback, see if you can embed the call to check within the gettext callback itself. JS code is not sequential but a series of chained callbacks or promises in mordern parlance.
So in short, mujhe to shayad har baar kuch mila 🙂
-Manan
Copy link to clipboard
Copied
u executed the same code and its working for you....?
Copy link to clipboard
Copied
Yes, you can see for yourself in the screengrab i sent
-Manan
Copy link to clipboard
Copied
yeah...i see that and i am amazed a bit...actually i am waiting for the current execution still either i am getting blank when i alert(data) and undefined when i write alert(this.data) .if i remove settimeout it runs asychronously .
Copy link to clipboard
Copied
You are executing the code with a textbox selected right?
-Manan
Copy link to clipboard
Copied
yes...is there windows or macbook making difference?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?

