Invoke
Overview
Section titled “Overview”Executes JavaScript code within the specified PrismaView
. This method enables dynamic communication between your SKSE plugin and the web-based UI, allowing you to update the interface, call JavaScript functions, and retrieve data from the DOM.
Syntax
Section titled “Syntax”void Invoke( PrismaView view, const char* script, JSCallback callback = nullptr);
Parameters
Section titled “Parameters”view
(required)
Section titled “view (required)”- Type:
PrismaView
- Description: The
PrismaView
instance in which the script will be executed
script
(required)
Section titled “script (required)”- Type:
const char*
- Description: A string containing the JavaScript code to execute
- Examples: Function calls, variable assignments, DOM manipulations
callback
(optional)
Section titled “callback (optional)”- Type:
JSCallback
- Default:
nullptr
- Description: An optional callback function that will be invoked with the script’s execution result
- Parameters: Takes a single argument - the data returned from JavaScript as a string
Return Value
Section titled “Return Value”This method returns void
. Use the optional callback parameter to receive return values from JavaScript.
Usage Examples
Section titled “Usage Examples”// Simple JavaScript execution without return valuePrismaUI->Invoke(view, "setBodyBackgroundColor('red')");
// Corresponding JavaScript functionwindow.setBodyBackgroundColor = (color) => { document.body.style.backgroundColor = color;};
// JavaScript execution with callback to receive return valuestatic void OnReceiveBackgroundColor(const char* data) { logger::info("Current background color: {}", data);}
PrismaUI->Invoke(view, "getBodyBackgroundColor()", OnReceiveBackgroundColor);
// JavaScript function that returns a valuewindow.getBodyBackgroundColor = () => { return document.body.style.backgroundColor;};
JSON Format Example
Section titled “JSON Format Example”You can use the nlohmann/json library to easily send structured data from C++ to your web UI:
#include <nlohmann/json.hpp>using JSON = nlohmann::json;
void SendPlayerData() { // Create JSON object with player information JSON playerData = { {"name", "Dragonborn"}, {"level", 25}, {"health", 100}, {"magicka", 150} };
// Send JSON to JavaScript std::string script = "updatePlayerInfo(" + playerData.dump() + ")"; PrismaUI->Invoke(view, script.c_str());}
// JavaScript function to handle the JSON datawindow.updatePlayerInfo = (playerData) => { document.getElementById('player-name').textContent = playerData.name; document.getElementById('player-level').textContent = playerData.level; document.getElementById('health-value').textContent = playerData.health; document.getElementById('magicka-value').textContent = playerData.magicka;};
String Escaping
Section titled “String Escaping”Escape Function Example
Section titled “Escape Function Example”Here’s a utility function to safely escape strings for JavaScript:
std::string escapeStringForJavaScript(const std::string& input) { std::ostringstream ss; for (char c : input) { switch (c) { case '\'': ss << "\\'"; break; case '\"': ss << "\\\""; break; case '\\': ss << "\\\\"; break; case '\n': ss << "\\n"; break; case '\r': ss << "\\r"; break; case '\t': ss << "\\t"; break; case '\b': ss << "\\b"; break; case '\f': ss << "\\f"; break; case '/': ss << "\\/"; break; default: ss << c; break; } } return ss.str();}
Usage Example
Section titled “Usage Example”// Unsafe - could cause JavaScript syntax errorsstd::string someText = "I don't like Flash";std::string script = "someFunction('" + someText + "')"; // WRONG!PrismaUI->Invoke(view, script.c_str());
// Safe - properly escapedstd::string someText = "I don't like Flash";std::string script = "someFunction('" + escapeStringForJavaScript(someText) + "')";PrismaUI->Invoke(view, script.c_str());
Best Practices
Section titled “Best Practices”- DOM Ready: Always ensure the DOM is ready before calling
Invoke
- String Escaping: Always escape strings using the provided function or similar method
- Return Values: Use callbacks for JavaScript functions that return data
- Performance: Minimize frequent
Invoke
calls during game loops. If you need it, instead use InteropCall method