Closed suryadip2008 closed 3 months ago
Indeed, the sleep function makes the current thread wait. If you want to create a background task, you can use java's Thread class. Here's an example: https://github.com/SnapEnhance/docs/blob/aa7c1a2ffd93dc55826dae0c4f114a53b2976c45/examples/unsafe_classloader.js#L12-L25
Indeed, the sleep function makes the current thread wait. If you want to create a background task, you can use java's Thread class. Here's an example: https://github.com/SnapEnhance/docs/blob/aa7c1a2ffd93dc55826dae0c4f114a53b2976c45/examples/unsafe_classloader.js#L12-L25
Thanks for the suggestion. Really appreciated! So, I implemented the logic that you had suggested however, I am encountering an error:
|I/2024-07-29 21:19:10/Scripting/Message scheduled. Will be sent in 5 seconds...
|E/2024-07-29 21:19:10/Scripting/ReferenceError: "java" is not defined.
|E/2024-07-29 21:19:10/Scripting/java.lang.Throwable
at me.rhunk.snapenhance.common.scripting.JSModule.load$lambda$48$lambda$39(Unknown Source:53)
at me.rhunk.snapenhance.common.scripting.JSModule.j(Unknown Source:0)
at a3.j.invoke(Unknown Source:11)
at me.rhunk.snapenhance.common.scripting.ktx.RhinoKtxKt$putFunction$1.call(Unknown Source:53)
at org.mozilla.javascript.optimizer.OptRuntime.callName(Unknown Source:8)
at org.mozilla.javascript.gen.scheduled_messages_5._c_anonymous_11(scheduled_messages:71)
at org.mozilla.javascript.gen.scheduled_messages_5.call(Unknown Source:101)
at org.mozilla.javascript.ContextFactory.doTopCall(Unknown Source:0)
at org.mozilla.javascript.ScriptRuntime.doTopCall(SourceFile:8)
at org.mozilla.javascript.gen.scheduled_messages_5.call(Unknown Source:18)
at org.mozilla.javascript.ArrowFunction.call(Unknown Source:11)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.getObject$lambda$6$lambda$2$lambda$1$lambda$0(Unknown Source:18)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.d(Unknown Source:0)
at a3.h.invoke(Unknown Source:326)
at me.rhunk.snapenhance.common.scripting.ktx.RhinoKtxKt.contextScope(Unknown Source:88)
at me.rhunk.snapenhance.common.scripting.ktx.RhinoKtxKt.contextScope$default(Unknown Source:5)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.getObject$lambda$6$lambda$2$lambda$1(Unknown Source:19)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.e(Unknown Source:0)
at R0.l.run(Unknown Source:73)
at java.lang.Thread.run(Thread.java:1012)
Here is the script so that you can better understand the issue:
var messaging = require("messaging");
var im = require("interface-manager");
var interfaces = require('java-interfaces');
(function () {
'use strict';
var inputMessage = "";
var conversationId = null;
function displayMessage(message) {
console.log(message);
if (typeof shortToast === "function") {
shortToast(message);
} else {
console.warn("shortToast is not available. Message:", message);
}
}
function sendMessage(conversationId, message) {
displayMessage("Attempting to send message: " + message);
if (typeof messaging.sendChatMessage !== "function") {
displayMessage("Error: messaging.sendChatMessage is not a function");
return;
}
try {
messaging.sendChatMessage(conversationId, message, function (error) {
if (error) {
displayMessage("Error sending message: " + JSON.stringify(error));
} else {
displayMessage("Message sent successfully");
}
});
} catch (error) {
displayMessage("Unexpected error in sendMessage: " + JSON.stringify(error));
}
}
function createConversationToolboxUI() {
im.create("conversationToolbox", function (builder, args) {
conversationId = args["conversationId"];
builder.textInput("Enter your message", "", function (value) {
inputMessage = value;
}).singleLine(true);
builder.row(function (rowBuilder) {
rowBuilder.button(" Send Now ", function() {
if (inputMessage.trim() !== "") {
sendMessage(conversationId, inputMessage);
} else {
displayMessage("Please enter a message");
}
});
rowBuilder.text(" ");
rowBuilder.button(" Schedule (5s) ", function() {
if (inputMessage.trim() !== "") {
displayMessage("Message scheduled. Will be sent in 5 seconds...");
// New scheduling logic
type("java.lang.Thread").newInstance(
interfaces.runnable(() => {
try {
java.lang.Thread.sleep(5000);
sendMessage(conversationId, inputMessage);
} catch (e) {
logError(e);
}
})
).start();
} else {
displayMessage("Please enter a message");
}
});
});
});
}
function start() {
createConversationToolboxUI();
}
start();
module.onSnapMainActivityCreate = function (activity) {};
})();
@suryadip2008 replace “java.lang.Thread.sleep(5000);” with “sleep(5000);” on line 68, it should work
@suryadip2008 replace “java.lang.Thread.sleep(5000);” with “sleep(5000);” on line 68, it should work
Did as you suggested and this time Snapchat crashes once 5 seconds is over. Log:
java.lang.RuntimeException:Can't find top level scope for ClassCache.get
at org.mozilla.javascript.ClassCache.get(UnknownSource:15)
at org.mozilla.javascript.InterfaceAdapter.create(UnknownSource:10)
at org.mozilla.javascript.NativeJavaObject.createInterfaceAdapter(UnknownSource:17)
at org.mozilla.javascript.NativeJavaObject.coerceTypeImpl(UnknownSource:166)
at org.mozilla.javascript.Context.jsToJava(UnknownSource:0)
at org.mozilla.javascript.NativeJavaMethod.call(UnknownSource:143)
at org.mozilla.javascript.optimizer.OptRuntime.callN(UnknownSource:0)
at org.mozilla.javascript.gen.scheduled_messages_3._c_sendMessage_3(scheduled_messages:29)
at org.mozilla.javascript.gen.scheduled_messages_3.call(UnknownSource:53)
at org.mozilla.javascript.optimizer.OptRuntime.callName(UnknownSource:8)
at org.mozilla.javascript.gen.scheduled_messages_3._c_anonymous_11(scheduled_messages:70)
at org.mozilla.javascript.gen.scheduled_messages_3.call(UnknownSource:101)
at org.mozilla.javascript.ContextFactory.doTopCall(UnknownSource:0)
at org.mozilla.javascript.ScriptRuntime.doTopCall(SourceFile:8)
at org.mozilla.javascript.gen.scheduled_messages_3.call(UnknownSource:18)
at org.mozilla.javascript.ArrowFunction.call(UnknownSource:11)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.getObject$lambda$6$lambda$2$lambda$1$lambda$0(UnknownSource:18)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.d(UnknownSource:0)
at a3.h.invoke(UnknownSource:326)
at me.rhunk.snapenhance.common.scripting.ktx.RhinoKtxKt.contextScope(UnknownSource:88)
at me.rhunk.snapenhance.common.scripting.ktx.RhinoKtxKt.contextScope$default(UnknownSource:5)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.getObject$lambda$6$lambda$2$lambda$1(UnknownSource:19)
at me.rhunk.snapenhance.common.scripting.impl.JavaInterfaces.e(UnknownSource:0)
at RO.I.run(UnknownSource:73)
java.lang.Thread.run(Thread.java:1012)
Description
So, currently we are working on a scheduled message script in which we implemented the sleep function for the duration the message has been scheduled.[For example if the user presses the schedule 5 seconds button, the sleep function gets activated for 5 seconds)However, pressing any of the "Send in ..." buttons (e.g., "Schedule (5s)", "Schedule (1m)", "Schedule (30m)") causes Snapchat's user interface to freeze for the duration of the scheduled delay.
Not sure if this is a bug. I looked for several workarounds to avoid this. Like I thought of using setTimeout or setInterval but both of them are not defined in the scripting enviroment. I also tried using loops but those freeze the UI too.
Reproduction steps
...
....
....
Logs
No response
Snapchat Version
13.1.0.37
SnapEnhance Version
Latest CI Build
Agreement