diff --git a/designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java b/designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java index 97ffeadc75..463cf39b7d 100644 --- a/designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java +++ b/designer-base/src/main/java/com/fr/design/jxbrowser/JxUIPane.java @@ -206,12 +206,11 @@ public class JxUIPane extends ModernUIPane { * 执行一段js * * @param javaScript 待执行的js脚本 + * @see JxUIPane#executeJS(String) */ public void executeJavaScript(String javaScript) { if (browser != null) { - browser.mainFrame().ifPresent(frame -> { - frame.executeJavaScript(javaScript); - }); + browser.mainFrame().ifPresent(frame -> frame.executeJavaScript(javaScript)); } } @@ -227,6 +226,22 @@ public class JxUIPane extends ModernUIPane { return Optional.ofNullable(frame.executeJavaScript(name)); } + /** + * 执行js脚本并返回,使用范围包含{@link JxUIPane#executeJavaScript(String)},可以代替使用 + * + * @param name 变量命名 + * @return js对象 + */ + public

Optional

executeJS(String name) { + if (browser != null) { + Optional frame = browser.mainFrame(); + if (frame.isPresent()) { + return Optional.ofNullable(frame.get().executeJavaScript(name)); + } + } + return Optional.empty(); + } + /** * 由于自定义scheme目前走的是url,因此路径会被自动转化,比如windows路径下对冒号问题 * C:\\abc 变成 /C/abc,这里对冒号进行编码转义 diff --git a/designer-base/src/test/java/com/fr/design/jxbrowser/JxUIPaneTest.java b/designer-base/src/test/java/com/fr/design/jxbrowser/JxUIPaneTest.java index 0b4bea61c5..c3d735de36 100644 --- a/designer-base/src/test/java/com/fr/design/jxbrowser/JxUIPaneTest.java +++ b/designer-base/src/test/java/com/fr/design/jxbrowser/JxUIPaneTest.java @@ -2,6 +2,7 @@ package com.fr.design.jxbrowser; import com.fr.design.DesignerEnvManager; import com.teamdev.jxbrowser.js.JsAccessible; +import com.teamdev.jxbrowser.js.JsObject; import javax.swing.JButton; import javax.swing.JFrame; @@ -10,6 +11,7 @@ import javax.swing.JPanel; import javax.swing.WindowConstants; import java.awt.BorderLayout; import java.awt.FlowLayout; +import java.util.Optional; public class JxUIPaneTest { @@ -30,16 +32,49 @@ public class JxUIPaneTest { JPanel panel = new JPanel(new FlowLayout()); contentPane.add(panel, BorderLayout.SOUTH); + + testJxUIPaneUpdate(panel, pane); + testExecuteJS1(panel, pane); + testExecuteJS2(panel, pane); + + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } + + private static void testJxUIPaneUpdate(JPanel container, JxUIPane pane) { JButton button = new JButton("点击我可以看到Swing的弹框,输出填写的信息"); - panel.add(button); button.addActionListener(e -> { Model returnValue = pane.update(); if (returnValue != null) { - JOptionPane.showMessageDialog(frame, String.format("姓名为:%s,年龄为:%d", returnValue.getName(), returnValue.getAge())); + JOptionPane.showMessageDialog(container.getRootPane(), String.format("姓名为:%s,年龄为:%d", returnValue.getName(), returnValue.getAge())); } }); - frame.setVisible(true); - frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + container.add(button); + } + + private static void testExecuteJS1(JPanel container, JxUIPane pane) { + JButton button = new JButton("执行回显js对象内容"); + button.addActionListener(e -> { + pane.executeJavaScript("var pagesize={\"page\":1,\"size\":10};"); + Optional value = pane.executeJS("pagesize"); + value.ifPresent(v -> JOptionPane.showMessageDialog(container.getRootPane(), + String.format("page为:%s,年龄为:%s", v.property("page").get(), v.property("size").get()))); + + }); + container.add(button); + + } + + private static void testExecuteJS2(JPanel container, JxUIPane pane) { + JButton button = new JButton("执行回显数字内容"); + button.addActionListener(e -> { + pane.executeJavaScript("var tom = {\"age\":18};"); + Optional value = pane.executeJS("tom.age"); + JOptionPane.showMessageDialog(container.getRootPane(), + String.format("var tom = {\"age\":18}; age:%s", value.get())); + }); + container.add(button); + } @JsAccessible