CEF JS调用C++代码

CEF JS调用C++代码

flyfish

CEF supports the creation of JS functions with native implementations. Functions are created using the CefV8Value::CreateFunction() static method that accepts name and CefV8Handler arguments. Functions can only be created and used from within a context .

CEF3支持本地实现JS函数的创建,函数使用静态方法CefV8Value::CreateFunction() 创建。接收name和CefV8Handler参数。函数仅能在context中创建和使用。

An implementation of the CefV8Handler interface that must be provided by the client application
本地应用程序必须提供的CefV8Handler接口的实现

代码如下

#pragma once
#include "include/cef_v8.h"
class MyV8Handler :
    public CefV8Handler
{
public:
    MyV8Handler();
    ~MyV8Handler();

    virtual bool Execute(const CefString& name,
        CefRefPtr<CefV8Value> object,
        const CefV8ValueList& arguments,
        CefRefPtr<CefV8Value>& retval,
        CefString& exception) override;


    IMPLEMENT_REFCOUNTING(MyV8Handler);
};
#include "stdafx.h"
#include "MyV8Handler.h"


MyV8Handler::MyV8Handler()
{
}


MyV8Handler::~MyV8Handler()
{
}
 bool MyV8Handler::Execute(const CefString& name,
    CefRefPtr<CefV8Value> object,
    const CefV8ValueList& arguments,
    CefRefPtr<CefV8Value>& retval,
    CefString& exception) 
{


    if (name == "JSFunction")
    {
        if (arguments.size() == 2)
        {
            CefString A = arguments.at(0)->GetStringValue();
            CefString B = arguments.at(1)->GetStringValue();

            TCHAR szT[256] = { 0 };
            _stprintf_s(szT, 256, _T("A = %s, B = %s\r\n"), A.c_str(), B.c_str());
            AfxMessageBox(szT);
            retval = CefV8Value::CreateInt(0);
            return true;
        }
    }

    // Function does not exist.
    return false;
}

帮助文档是重新写了一个类MyRenderProcessHandler 继承自CefRenderProcessHandler
这里是在原来的ClientApp类中增加基类CefRenderProcessHandler。

class ClientApp: public CefApp,
public CefBrowserProcessHandler, public CefRenderProcessHandler

ClientApp类的头文件增加

    void OnContextCreated(CefRefPtr<CefBrowser> browser,
        CefRefPtr<CefFrame> frame,
        CefRefPtr<CefV8Context> context);
    void OnWebKitInitialized() override;// Called after WebKit has been initialized.
    CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler()
    {
        return this;
    }

实现文件

void ClientApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefFrame> frame,
    CefRefPtr<CefV8Context> context)
{

    CefRefPtr<CefV8Value> object = context->GetGlobal();
    CefRefPtr<CefV8Value> func = CefV8Value::CreateFunction("JSFunction", new MyV8Handler);
    object->SetValue("JSFunction", func, V8_PROPERTY_ATTRIBUTE_NONE);
}

void ClientApp::OnWebKitInitialized()
{
}

测试使用代码

CefRefPtr<CefBrowser> browser=m_cefHandler->GetBrowser();
browser->GetMainFrame()->LoadURL();

HTML样本

<html>
<head>
</head>
<body>
 <script type="text/javascript">
      function Test(){
        window.JSFunction(document.getElementById("A").value, document.getElementById("B").value);
      }
    </script>
<form>
A: <input type="text" id="A" />B: 
<input type="text" id="B" /><input  type="button" value="Test" onclick="Test()"/>
</form>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西笑生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值