Re: Extending Native javascript in dom

Martin Lutken <[email protected]>
Newsgroups gmane.comp.mozilla.devel.embedding
Organization http://groups.google.com
Message-ID <8d60c418-b6ac-491e-abf0-0bf7455b7fdb@k22g2000yqh.googlegroups.com>
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 );
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.