#include "cefapplicationhandler.h"
#include "include/cef_app.h"
#include "applicationV8Handler.h"
bool cefapplicationhandler ::OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
if (message->GetName() == "InvokeScript") {
return true; // message handled
}
return false;
}
void cefapplicationhandler::OnWebKitInitialized()
{
std::string extensionCode =
"var test;"
"if (!test)"
" test = {};"
"(function() {"
" test.myfunc = function() {"
" native function myfunc();"
" return myfunc();"
" };"
"})();";
// Create an instance of my CefV8Handler object.
CefRefPtr<CefV8Handler> handler = new applicationV8Handler();
// Register the extension.
CefRegisterExtension("v8/test", extensionCode, handler);
}
#include "include\cef_v8.h"
class applicationV8Handler : public CefV8Handler
{
public:
applicationV8Handler() {}
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE
{
if (name == "ChangeTextInJS")
{
// Return my string value.
retval = CefV8Value::CreateString("My Value!");
return true;
}
// Function does not exist.
return false;
}
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(applicationV8Handler);
};
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert(window.myfunc());
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
<p>By clicking the button above, a function will be called. The function will alert a message.</p>
</body>
</html>
Users browsing this forum: No registered users and 151 guests