/*
_ __ _____ _
| |/ / ___ _____ _ __ _ __ | ____|_ _____ ___ _ _| |_ ___
| ' / | | \ \ / / _ \| '__| '_ \ | _| \ \/ / _ \/ __| | | | __/ _ \
| . \ |_| |\ V / (_) | | | | | | | |___ > < __/ (__| |_| | || __/
|_|\_\__, | \_/ \___/|_| |_| |_| |_____/_/\_\___|\___|\__,_|\__\___|
|___/
Developed by the iBallexus Team
© 2025 Peretas Technologies
You are free to modify this code to your liking in order to suit your executor's needs.
We are glad to have you supported as an executor with Kyvorn Execute.
If you own a mid-sized or larger executor that supports Kyvorn Execute, you can be featured
on the Kyvorn website. Reach out to us at ib.peretas.tech/d
*/
const API_SERVER_URL = "http://localhost:3000"; // should stay as is unless using a development enviornment
const DEFAULT_EXECUTOR_NAME = "Kyvorn Execute JS Testing"; // edit this to your executor's name
const SCRIPT_CHECK_WAIT_DURATION = 5000; // 5 seconds
let executorToken = localStorage.getItem('executorToken');
let executorName = DEFAULT_EXECUTOR_NAME;
let checkSessionInterval;
function log(message, type = '') {
if (type === 'error') {
console.error("[Kyvorn Execute] " + message)
} else if (type === 'warning') {
console.warn("[Kyvorn Execute] " + message)
} else {
console.log("[Kyvorn Execute] " + message)
}
}
log(`Ready`)
// Pair your executor to the user
async function login(token) {
if (!token) {
log("Executor token required", "error")
return;
}
try {
const response = await fetch(`${API_SERVER_URL}/api/auth/executor/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token, name: DEFAULT_EXECUTOR_NAME, })
});
const data = await response.json();
if (data.success) {
executorToken = token;
executorName = name;
// Save our executor info to localStorage to persist sessions
// This may need to change based on your framework
localStorage.setItem('executorToken', token);
localStorage.setItem('executorName', name);
log("Connected to Kyvorn Execute API")
startSessionCheck();
} else {
log(`Login failed: ${data.error || 'Unknown error'}`, "error");
}
} catch (error) {
log(`Failed to login: ${error.message}`, "error");
}
}
// Loop to check if the executor is still paired
async function checkSession() {
if (!executorToken) return;
try {
const response = await fetch(`${API_SERVER_URL}/api/auth/executor/session`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token: executorToken })
});
const data = await response.json();
if (!data.paired) {
log("User has unpaired executor, logging out...")
logout();
return;
}
log("Connected")
await checkPendingScripts();
} catch (error) {
log(`User session check failed: ${error}`, "error");
}
}
/*
❗❗❗❗❗❗❗❗❗🚧🚧🚧🚧🚧
THIS FUNCTION NEEDS TO BE EDITED BEFORE PRODUCTION
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
*/
async function getScriptById(sid) {
try {
const response = await fetch(`${API_SERVER_URL}/api/posts/${sid}`);
const data = await response.json();
const scr = data.scriptContent;
// This function is inside of this file
execute(scr)
log(`Executed ${sid} from Kyvorn`)
} catch (error) {
log(`Failed to fetch script from Kyvorn: ${error}`, "error")
}
}
function execute(script) {
// Note: This is in its own function to support execution from the Kyvorn Editor
// You will need to add your logic to execute the script here
// "parent.execute(script)" is the function used by Sight to execute a script
// The "script" variable is the full script content.
// View this function and its logic on Sight:
// https://github.com/peretashacking/sight/blob/main/src/main.js line 124:1
parent.execute(script)
}
// Loop to check for pending scripts
async function checkPendingScripts() {
try {
const response = await fetch(`${API_SERVER_URL}/api/execute/receive/${executorToken}`);
const data = await response.json();
if (data.scripts && data.scripts.length > 0) {
data.scripts.forEach(script => {
if (script.type = "sid") {
getScriptById(script.scriptId)
} else if (script.type = "editor") {
execute(script.scr)
}
});
}
} catch (error) {
log(`Failed checking pending scripts: ${error}`, "error")
}
}
// Logic to start the pair check
function startSessionCheck() {
checkSession();
checkSessionInterval = setInterval(checkSession, SCRIPT_CHECK_WAIT_DURATION);
}
// Remove items if user unpairs executor
function logout() {
log(`Removing items, user has unpaired this executor ${localStorage.getItem('executorToken')}`)
localStorage.removeItem('executorToken');
localStorage.removeItem('executorName');
executorToken = null;
executorName = null;
clearInterval(checkSessionInterval);
log(`Logged out of Kyvorn Execute`)
}
// Check for existing session on page load to reconnect
if (executorToken && executorName) {
startSessionCheck();
}