I create a QScriptEngine and set a QObject as global object that has some signals / slots. Then I load some script files and pass it to the engine (using evaluate). The script creates an object and connects some signals of the global object to the its functions.
Sadly the property (this.password) of the script object is cleared when its function gets called from the singal (its set during evaluate, I checked that).
Here is the script:
function Chanserv(password) {
this.password = password;
// print("#### Constructor local: " + password + " / global: " + Bot.Password);
}
Chanserv.prototype.test = function() {
// print("This is a test function / " + Bot.Password + " / " + this.password);
}
Chanserv.prototype.auth = function() {
print("#### entered auth function! " + this.password);
// if (this.password && this.password.length > 0) {
if (Bot.Password && Bot.Password.length > 0) {
Bot.sendMessage("nickserv", "identify " + Bot.Password);
// print("Trying to authenticate with " + this.password);
}
else {
print("Bot.Password undefined.");
// print("this.password = " + this.password
// + ", this.password.length = " + (this.password.length > 0));
}
}
var chanservObject = new Chanserv(Bot.Password); // this.password gets set
chanservObject.test();
try {
Bot.joinedChannel.connect(chanservObject.auth); // this.password is empty when called...
Bot.joinedChannel.connect(chanservObject.test);
// Bot.connected.connect(chanserv.auth);
}
catch (e) {
print(e);
}
Any ideas why that may happen?
Greetings Ben
Javascript objects are passed by reference. Are you modifying Bot.Password before calling chanservObject.auth?
Related
I am still trying to build my website where people can text each other, send photos etc.
The chat thing works really well until I wanna add the functionality checking whether the second user in the chat is typing.
Here is my code without the typing thing, this works really well ;)
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
// temporary user and receiver's names
const username = prompt("Nickname:");
const receiver = prompt("Receiver's name:");
// sending a message
document.getElementById("send-message").addEventListener("submit", postChat);
function postChat(e)
{
e.preventDefault();
const timestamp = Date.now();
const chatTxt = document.getElementById("chat-txt");
const message = chatTxt.value;
chatTxt.value = "";
db.ref("messages/" + username + "/" + receiver + "/" + timestamp).set({
usr: username,
msg: message,
});
db.ref("messages/" + receiver + "/" + username + "/" + timestamp).set({
usr: username,
msg: message,
});
}
// printing the message
const fetchChat = db.ref("messages/" + username + "/" + receiver + "/");
fetchChat.on("child_added", function (snapshot)
{
const messages = snapshot.val();
const msg = "<li>" + messages.usr + " : " + messages.msg + "</li>";
document.getElementById("messages").innerHTML += msg;
});
The problem appears when I want to check if the second user is typing. When I am adding the code that's below messages just stop to work. There is a random null in the database as a new user who sent a message to another null. The chat between users also stops to work, users don't see same messages, sometimes can't see any of them and always "undefined" types "undefined" when I refresh the website.
At least it correctly shows when someone is typing, but the rest of the functionalities (which used to work) just don't work anymore.
Here is the code of the typing thing, I also tried to check whether the username and receiver's name aren't null but it didn't reallly help.
// showing whether the receiver is typing
function sendTyping(tmp)
{
if(tmp)
{
db.ref("messages/" + username + "/" + receiver).set({
tpg: "yes"
});
}
else
{
db.ref("messages/" + username + "/" + receiver).set({
tpg: "no"
});
}
}
var searchTimeout;
document.getElementById("chat-txt").onkeydown = function() {
if (searchTimeout != undefined)
clearTimeout(searchTimeout);
searchTimeout = setTimeout(callServerScript, 1500);
sendTyping(true);
}
function callServerScript() {
sendTyping(false);
}
let areTheyTyping = db.ref("messages/" + receiver + "/" + username);
areTheyTyping.on("value", function(snapshot) {
const typing = snapshot.val();
const status = typing.tpg;
if(status == "yes")
document.getElementById("writing").innerHTML = "typing...";
else
document.getElementById("writing").innerHTML = "";
});
I mostly wrote this by myself, I will appreciate any kind of help, just please use a straightforward language so I can easily understand the explanation of the problem, I am kind of new.
The function
const fetchChat = db.ref("messages/" + username + "/" + receiver + "/");
fetchChat.on("child_added", function (snapshot)
{
const messages = snapshot.val();
const msg = "<li>" + messages.usr + " : " + messages.msg + "</li>";
document.getElementById("messages").innerHTML += msg;
});
It fetches the chat whenever a child is added to the directory of your messages, so when you store the typing status in "messages/" + username + "/" + receiver + "/" the function .on knows that you've changed/added something, therefore posting a message. It's undefined because the message is in fact empty. You should create a new directory where you store the areTheyTyping status, something like "status/" + username + "/" + receiver + "/" Hope I helped.
My experience showed that firestore has problem with undefined values. You can try adding a default value for undefined or null usernames. Maybe you can add zero (0) as the default value for this matter.
Change the path db.ref("messages/" + receiver + "/" + username) to db.ref("messages/" + username + "/" + receiver) at areTheyTyping. See if it works
function sendTyping(tmp)
{
if(tmp) {
db.ref("messages/" + username + "/" + receiver).set({
tpg: "yes"
});
}
else {
db.ref("messages/" + username + "/" + receiver).set({
tpg: "no"
});
}
}
var searchTimeout;
document.getElementById("chat-txt").onkeydown = function() {
if (searchTimeout !== undefined)
clearTimeout(searchTimeout);
searchTimeout = setTimeout(callServerScript, 1500);
sendTyping(true);
}
function callServerScript() {
sendTyping(false);
}
let areTheyTyping = db.ref("messages/" + username + "/" + receiver);
areTheyTyping.on("value", function(snapshot) {
const typing = snapshot.val();
const status = typing.tpg;
if(status === "yes")
document.getElementById("writing").innerHTML = "typing...";
else
document.getElementById("writing").innerHTML = "";
});
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 6 years ago.
I am having problem identifying what and how is some property of "local" scope in JS. How is the property _secretNum "private" when it is clearly available outside the scope of the constructor as per below line.
document.write("Value of secretNum in secret: " + secret._secretNum
+ "<br/>");
The full code is as below,
<body>
<script type="text/javascript">
function SecretCode() {
var _secretNum = 78;
this.guessNum = function(num) {
if (num > _secretNum) {
return "Lower";
} else if (num < _secretNum) {
return "Higher";
} else {
return "You guessed it";
}
}
this.setNum = function(newNum) {
this._secretNum = newNum;
}
this.getNum = function() {
return this._secretNum;
}
}
var secret = new SecretCode();
for ( var prop in secret) {
document.write(prop + " : " + secret[prop] + "<br/>");
}
document.write("Is 70 the number: " + secret.guessNum(70) + "<br/>");
secret.setNum(9);
document.write("Value of secretNum in secret: " + secret.getNum()
+ "<br/>");
document.write("Value of secretNum in secret: " + secret._secretNum
+ "<br/>");
var secretAliter = new SecretCode();
secretAliter.setNum(17);
document.write("Value of secretNum in secretAliter : "
+ secretAliter.getNum() + "<br/>");
document.write("Value of secretNum in secretAliter : "
+ secretAliter._secretNum + "<br/>");
</script>
</body>
How is the property _secretNum "private"
It's not, I don't know where you got that code from, but it's broken.
The local variable declared by var _secretNum = 78 is private, but it's also being ignored by the setNum and getNum accessor methods. They're using this._secretNum, which is a completely different not private variable, that has nothing to do with the var _secretNum = 78 line. You can change the value of this._secretNum all you want, both directly and by using setNum, and it won't affect the variable _secretNum which guessNum uses.
The point of declaring var _secretNum as a local variable within your constructor function is that it is only accessible within that function, and by the functions that "close over" it when they are returned. Drop the this._secretNum from your accessors and just use _secretNum.
By way of example:
function MyConstructor() {
var _name = "bob" // Completely private
this._age = 45 // Not private
this.getName = function () {
return _name // public accessor for private variable
}
this.getAge = function() {
return this._age // public accessor for public property
}
}
var x = new MyConstructor();
x._name = "blah" // unrelated, ignored by getName
alert(x.getName()) // bob, not "blah"
x._age = 25 // overwrite age
alert(x.getAge()) // 25
I am attempting to do a small PoC with PDFs and have run into an issue. I am looking to post a message to a PDF and have the PDF post a message to the browser.
The deets:
I am viewing the PDF in an "object" element in IE9. I am using itextsharp to prefill a pdf template on the server, inject some app level javascript (post message and on message stuff) and then serve that up to the browser via a filestreamresult. I am using Reader 10 to view the PDF in IE9.
What works:
So far, everything works except for the PDF posting a message to the browser. I can post a message to the PDF, from the browser, no problem and all of the fields are prefilled as desired.
What doesn't work:
When I try using this.hostContainer.postMessage(["something","somethingmore"]) I get an Acrobat Escript window that says "hostContainer is not defined". I have also tried using "event.target.hostContainer" but I get "event.target is not defined". I am at a loss of what to do and any insight would be super helpful.
Reference links:
Acrobat Javascript API
Stackoverflow How-To on this topic
Original guide I used
The code:
My form view:
<object id="pdfFrame" style="width:100%;height: 100%;" data="#Url.Action("LoadForm")">No luck :(</object>
My custom javascript string method:
private static string GetCustomJavascript(string existingJavaScript)
{
const string newJs =
"this.disclosed = true; " +
"if (this.external && this.hostContainer) { " +
"function onMessageFunc( stringArray ) { " +
// "var name = this.myDoc.getField(personal.name); " +
// "var login = this.myDoc.getField(personal.loginname); " +
"try{" +
"app.alert(doc.xfa);" +
"console.println('Doc xfa value = ' + doc.xfa);" +
// "event.target.hostContainer.postMessage(['hello from pdf!']);" +
// "this.hostContainer.postMessage(['hello from pdf!']);"+
// "name.value = stringArray[0]; " +
// "login.value = stringArray[1]; " +
"} catch(e){ onErrorFunc(e); } " +
"} " +
"function onErrorFunc( e ) { " +
"console.show(); " +
"console.println(e.toString()); " +
"} " +
"try {" +
"if(!this.hostContainer.messageHandler) { " +
"this.hostContainer.messageHandler = new Object(); " +
"this.hostContainer.messageHandler.myDoc = this; " +
"this.hostContainer.messageHandler.onMessage = onMessageFunc; " +
"this.hostContainer.messageHandler.onError = onErrorFunc; " +
"this.hostContainer.messageHandler.onDisclose = function(){ return true; }; " +
"}" +
"} catch(e){onErrorFunc(e);}" +
"}";
var jsToReturn = existingJavaScript + newJs;
return jsToReturn;
}
My method for filling and sending the form to the browser:
public MemoryStream GetFilledRequestForm(string fileDirectory, User user, FormView formView)
{
var pdfStream = new MemoryStream();
var templateFilePath = GetRequestTypeTemplateFilePath(fileDirectory, _requestModule.FormTemplateFileName);
var pdfReader = new PdfReader(templateFilePath);
// pdfReader.RemoveUsageRights();
var stamper = new PdfStamper(pdfReader, pdfStream);
var formFields = GetFormFields(user, formView, pdfReader);
foreach (var field in formFields.Where(f => f.Value != null))
{
stamper.AcroFields.SetField(field.Name, field.Value);
}
stamper.FormFlattening = false;
var newJs = GetCustomJavascript(stamper.Reader.JavaScript);
stamper.AddJavaScript("newJs", newJs);
stamper.Close();
byte[] byteInfo = pdfStream.ToArray();
var outputStream = new MemoryStream();
outputStream.Write(byteInfo, 0, byteInfo.Length);
outputStream.Position = 0;
return outputStream;
}
Ok, so I have resolved it, with some help of course. I found the key at this stack overflow post. I needed to wait for the object to load before assigning the message handler. Additionally, I needed a global variable in the pdf javascript to be able to post the message.
Html/Javascript: (the key here is the loadListener() function)
#model WebModel.FormView
<object id="pdfFrame" style="width:100%;height: 100%;" data="#Url.Action("LoadForm")">No luck :(</object>
<input id="buttonPost" type="button" value="post to pdf"/>
<script type="text/javascript">
var PDFObject = document.getElementById("pdfFrame");
function loadListener() {
if (typeof PDFObject.readyState === 'undefined') { // ready state only works for IE, which is good because we only need to do this for IE because IE sucks in the first place
debugger;
PDFObject.messageHandler = { onMessage: messageFunc };
return;
}
if (PDFObject.readyState == 4) {
debugger;
PDFObject.messageHandler = { onMessage: messageFunc };
} else {
setTimeout(loadListener, 500);
}
}
function messageFunc(data) {
debugger;
var messagedata = data;
alert('finally!!');
}
function sendToPdf() {
if(PDFObject!= null){
PDFObject.postMessage(
["a", "b"]);
}
}
$('#pdfFrame').ready(function() {
loadListener();
$('#buttonPost').on('click', function() {
sendToPdf();
});
});
</script>
My new function to create the javascript: (the key here is var appHostContainer)
private static string GetCustomJavascript(string existingJavaScript)
{
const string newJs =
"this.disclosed = true; " +
"var appHostContainer = this.hostContainer;" +
"if (this.external && this.hostContainer) { " +
"function onMessageFunc( stringArray ) { " +
// "var name = this.myDoc.getField(personal.name); " +
// "var login = this.myDoc.getField(personal.loginname); " +
"try{" +
"app.alert(stringArray);" +
"appHostContainer.postMessage(['hello from pdf!']);" +
// "name.value = stringArray[0]; " +
// "login.value = stringArray[1]; " +
"} catch(e){ onErrorFunc(e); } " +
"} " +
"function onErrorFunc( e ) { " +
"console.show(); " +
"console.println(e.toString()); " +
"} " +
"try {" +
"if(!this.hostContainer.messageHandler) { " +
"this.hostContainer.messageHandler = new Object(); " +
"this.hostContainer.messageHandler.myDoc = this; " +
"this.hostContainer.messageHandler.onMessage = onMessageFunc; " +
"this.hostContainer.messageHandler.onError = onErrorFunc; " +
"this.hostContainer.messageHandler.onDisclose = function(){ return true; }; " +
"}" +
"} catch(e){onErrorFunc(e);}" +
"}";
var jsToReturn = existingJavaScript + newJs;
return jsToReturn;
}
I was wondering how can I make it posible to get rid of putting "new" before a function, for example:
new functionToDo("thingsToDo").iGotYouBruh("Halo Humans");
is there a posible way of doing this without the "new"?
here is the code I'm trying to use without the "new":
function local (title) {
var storeTitle = title;
this.addL = function(lString) {
var storeText = lString;
localStorage.setItem(storeTitle, storeText);
console.info("Locally stored " + storeTitle.toUpperCase() + " with " + storeText.substring(0, 10) + "... As text.");
};
this.removeL = function() {
localStorage.removeItem(storeTitle);
console.info("Locally removed " + storeTitle + ".");
};
this.getL = function () {
localStorage.getItem(storeTitle);
console.info("Locally got string of " + storeTitle + ": " + localStorage.getItem(storeTitle));
};
};
and here's what I would have to do to invoke the function:
new local("storedElement").getL();
This is possible by checking whether this is an instance of the function itself and returning a new instance otherwise:
function local (title) {
if (!(this instanceof local)) {
return new local(title);
}
var storeTitle = title;
this.addL = function(lString) {
var storeText = lString;
localStorage.setItem(storeTitle, storeText);
console.info("Locally stored " + storeTitle.toUpperCase() + " with " + storeText.substring(0, 10) + "... As text.");
};
this.removeL = function() {
localStorage.removeItem(storeTitle);
console.info("Locally removed " + storeTitle + ".");
};
this.getL = function () {
localStorage.getItem(storeTitle);
console.info("Locally got string of " + storeTitle + ": " + localStorage.getItem(storeTitle));
};
};
You could use JavaScript closures. In particular look at the "Using Closures for the Module Pattern" section of this webpage for a full description. The idea is to have the function return an literal with all the required methods. Any functions or variables that you want to be kept private are just local variables for the function.
Using the following code, I'm getting this error RewardPurchases.PurchasesArray[i].Student_Name is undefined:
$('button#random').click( function() {
var Num = Math.floor(Math.random() * Total+1);
Num--;
for (var i in RewardPurchases.PurchasesArray) {
/* --------> */
$('#display').text("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")").show().delay(300);
if (i == Num) {
var TutorGroup = '';
Frog.API.get('timetable.getClasses',
{
'params': {'student': RewardPurchases.PurchasesArray[i].Student_ID },
'onSuccess': function(data) {
for (var i = 0; i < data.length; i++) {
if (data[i].subject.name == "Tut Period") {
TutorGroup = data[i].name.replace("/Tp", "");
}
}
}
});
$('#display').animate({'font-size': 36}, 1500, function() {
$(this).prepend('<p>!!! WINNER !!!</p>');
});
alert("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")");
}
}
} );
However, if I move this line $('#display').text(... as follows, the error disappears:
$('button#random').click( function() {
var Num = Math.floor(Math.random() * Total+1);
Num--;
for (var i in RewardPurchases.PurchasesArray) {
if (i == Num) {
var TutorGroup = '';
/* --------> */
$('#display').text("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")").show().delay(300);
Frog.API.get('timetable.getClasses',
{
'params': {'student': RewardPurchases.PurchasesArray[i].Student_ID },
'onSuccess': function(data) {
for (var i = 0; i < data.length; i++) {
if (data[i].subject.name == "Tut Period") {
TutorGroup = data[i].name.replace("/Tp", "");
}
}
}
});
$('#display').animate({'font-size': 36}, 1500, function() {
$(this).prepend('<p>!!! WINNER !!!</p>');
});
alert("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")");
}
}
} );
I don't understand why this is the case? i isn't being defined by the if statement?
I'm trying to display each name in an array, before picking a random name and displaying it in a large font with "WINNER!" above it.
Thanks in advance,
Using for .. in isn't the greatest practice in situations like these. It does a deep dredge of ALL object properties, including functions belonging to the prototype.
The reason why only $('#display').text(... causes you issues is that you try to use a property of a property RewardPurchases.PurchasesArray[i]. Elsewhere, you use it by itself, which won't fail, it will just silently return undefined in those cases. (a.k.a., 'params': {'student': RewardPurchases.PurchasesArray[i].Student_ID }.)
Using a test that you wrap all the code inside your for .. in loop, typeof RewardPurchases.PurchasesArray[i] === 'object' && typeof RewardPurchases.PurchasesArray[i] !== null should do the trick, ensuring that each property that you use in your iterations is simply an object, and not a function or some "scalar" value.
NB: You can also use RewardPurchases.PurchasesArray[i].hasOwnProperty('propertyName'), but it isn't supported universally in all browsers, so the above example I gave is safer and works for your purpose.
What is the integrity of your array like?
//RewardPurchases.PurchasesArray
[0] undefined
[1] { Student_Name: undefined }
[2] { Student_Name: 'Bob' }
The above are all valid in an array. [0] and [1] will both give you the error you received.
If PurchasesArray is not an array but an object - then you need to do a check inside your loop.
for (var i in RewardPurchases.PurchasesArray) {
if(!RewardPurchases.PurchasesArray.hasOwnProperty(i)) {
continue;
}
//rest of code...
}