JCEF-鼠标右键菜单
为鼠标添加自定义菜单,比较简单,实现一个Handler就行
Hanler实现类
public class MenuHandler extends CefContextMenuHandlerAdapter{
private final static int MENU_ID_INJECTION=10000;
private final static int MENU_ID_ADDTEXT=10001;
@Override
public void onBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams params, CefMenuModel model) {
//清除菜单项
model.clear();
//剪切、复制、粘贴
model.addItem(MenuId.MENU_ID_COPY, "复制");
model.addItem(MenuId.MENU_ID_CUT, "剪切");
model.addItem(MenuId.MENU_ID_PASTE, "粘贴");
model.addSeparator();
model.addItem(MenuId.MENU_ID_BACK, "返回");
model.setEnabled(MenuId.MENU_ID_BACK, browser.canGoBack());
model.addItem(MenuId.MENU_ID_FORWARD, "前进");
model.setEnabled(MenuId.MENU_ID_FORWARD, browser.canGoForward());
model.addItem(MenuId.MENU_ID_RELOAD, "刷新");
model.addSeparator();
//创建子菜单
CefMenuModel cmodel=model.addSubMenu(MENU_ID_INJECTION, "脚本注入");
cmodel.addItem(MENU_ID_ADDTEXT, "添加一段文本");
}
/*
* @see org.cef.handler.CefContextMenuHandler#onContextMenuCommand(org.cef.browser.CefBrowser, org.cef.browser.CefFrame, org.cef.callback.CefContextMenuParams, int, int)
*/
@Override
public boolean onContextMenuCommand(CefBrowser browser, CefFrame frame, CefContextMenuParams params, int commandId, int eventFlags) {
switch(commandId) {
case MenuId.MENU_ID_RELOAD:
browser.reload();
return true;
case MENU_ID_ADDTEXT:
browser.executeJavaScript("document.body.innerHTML+='
添加一段文本
';", browser.getURL(), 0);
return true;
}
return false;
}
}
测试类,在cefClient中添加此Handler:cefClient.addContextMenuHandler(new MenuHandler());
public class MouseMenuTestFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 5944953587408136931L;
public static void main(String[] args) {
String url=System.getProperty("user.dir")+"/jstest.html";
new MouseMenuTestFrame(url);
}
public MouseMenuTestFrame(String url) {
//是否Linux系统
boolean useOSR=OS.isLinux();
//是否透明
boolean isTransparent=false;
//添加Handler,在CEFAPP状态为终止时退出程序
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
@Override
public void stateHasChanged(org.cef.CefApp.CefAppState state) {
// Shutdown the app if the native CEF part is terminated
if (state == CefAppState.TERMINATED) System.exit(0);
}
});
CefSettings settings = new CefSettings();
settings.windowless_rendering_enabled = useOSR;
//获取CefApp实例
CefApp cefApp=CefApp.getInstance(settings);
//创建客户端实例
CefClient cefClient = cefApp.createClient();
//添加鼠标右键菜单handler
cefClient.addContextMenuHandler(new MenuHandler());
//添加一个JS交互
jsActive(cefClient);
//创建浏览器实例
CefBrowser cefBrowser = cefClient.createBrowser(url, useOSR, isTransparent);
//将浏览器UI添加到窗口中
getContentPane().add(cefBrowser.getUIComponent(), BorderLayout.CENTER);
pack();无锡人流多少钱 http://www.bhnnk120.com/
setTitle("测试JCEF-鼠标右键事件");
setSize(800, 600);
setVisible(true);
//添加一个窗口关闭监听事件
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
CefApp.getInstance().dispose();
dispose();
}
});
}
/**
* 添加js交互
* @author:liuming
*/
public void jsActive(CefClient client) {
//配置一个查询路由,html页面可使用 window.java({}) 和 window.javaCancel({}) 来调用此方法
CefMessageRouterConfig cmrc=new CefMessageRouterConfig("java","javaCancel");
//创建查询路由
CefMessageRouter cmr=CefMessageRouter.create(cmrc);
cmr.addHandler(new CefMessageRouterHandler() {
@Override
public void setNativeRef(String str, long val) {
System.out.println(str+" "+val);
}
@Override
public long getNativeRef(String str) {
System.out.println(str);
return 0;
}
@Override
public void onQueryCanceled(CefBrowser browser, CefFrame frame, long query_id) {
System.out.println("取消查询:"+query_id);
}
@Override
public boolean onQuery(CefBrowser browser, CefFrame frame, long query_id, String request, boolean persistent,
CefQueryCallback callback) {
System.out.println("request:"+request+"\nquery_id:"+query_id+"\npersistent:"+persistent);
callback.success("Java后台处理了数据");
return true;
}
}, true);
client.addMessageRouter(cmr);
}
}