Not sure if this is in the lines of what you need.
But this is more or less how I execute javascript from C++ in Mozilla:

/** Convert a std::string in UTF8 to Mozilla UTF16 string */
//inline nsEmbedString
inline nsString
toAStringUTF16 (const std::string& sString )            ///< UTF8 std::string to
convert
{
        return NS_ConvertUTF8toUTF16( sString.c_str() );
}


/** Convert a Mozilla UTF16 string (nsEmbedString) to a std::string in
UTF8 */
inline std::string
toStdStringUTF8 (const nsString& aString )              ///< Mozilla UTF16 
string
to convert
{
        return NS_ConvertUTF16toUTF8(aString).get();
}


bool executeJavascriptString(
        nsCOMPtr<nsIScriptContext> pIScriptContext,
        const std::string& sScript,             ///< [in]       String with 
script to
execute
        std::string& sScriptReturnValue )       ///< [out]      Return value of 
script
(if any)
{
        nsIPrincipal* thePrincipal = nsnull;
        nsString script= toAStringUTF16( sScript );
        PRBool bUndefined;
        nsString sReturnValue;
        nsresult rv = pIScriptContext->EvaluateString(
                script,
//              NS_LITERAL_STRING("bob();"),
                nsnull, thePrincipal, "", 1,
                JSVERSION_DEFAULT,
//              JSVERSION_1_8,
                &sReturnValue, &bUndefined );

        sScriptReturnValue      = toStdStringUTF8( sReturnValue );
        return !static_cast<bool>(bUndefined != 0);
}


/// Call the JS evaluate function

nsCOMPtr <nsIDOMWindow>  piMyDOMWindow;  // =  initialize to you dom
window
nsCOMPtr<nsIScriptGlobalObject>
pIScriptGlobalObject( do_GetInterface(piMyDOMWindow) );
nsCOMPtr<nsIScriptContext> pIScriptContext ( pIScriptGlobalObject-
>GetContext() );

std::string sRetVal;

executeJavascriptString (pIScriptContext, "alert('hey!');",
sRetVal );
_______________________________________________
dev-embedding mailing list
dev-embedding@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-embedding

Reply via email to