如何在java代码中获取html中的js返回的结果

本文介绍如何在Java中通过解析HTML文档调用其中的JavaScript函数并获取返回值。利用Jsoup库解析HTML,借助Nashorn引擎执行JS代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何在java代码中获取html中的js返回的结果

  1. 背景

    在开发html转pdf中,需求中有个逻辑需要获取html中js代码,js代码对数据进行处理,并返回给java使用;

  2. 使用

    1. 依赖:

      <!-- 解析html -->
      <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.8.3</version>
      </dependency>
      
    2. html代码

      <!DOCTYPE html>
      <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
      <script>
        function reverse(code, type) {
          var output = "EP";
          return output = output + type + code;
        }
      </script>
      
      <head>
        <meta charset="UTF-8">
        </meta>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        </meta>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        </meta>
        <title>Document</title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
        <style>
          body {
            font-family: SimSun;
          }
      
          ul {
            padding: 0;
          }
      
          ul li {
            list-style-type: none;
          }
      
          div {
            box-sizing: border-box;
          }
      
          .homeContent {
            padding: 40px
              /* 40/28 */
              40px 20px
              /* 20/28 */
              40px;
            width: 650px;
            height: 900px
              /* 742/28 */
            ;
            margin: 0 auto;
      
          }
      
          .headerLogo {
            width: 100%;
            height: 88px
              /* 88/28 */
            ;
            overflow: hidden;
      
          }
      
          .headerLogo .left {
            width: 248px
              /* 248/28 */
            ;
            height: 80px
              /* 80/28 */
            ;
            float: left;
      
          }
      
          .headerLogo .left img {
            width: 100%;
            height: 100%;
          }
      
          .headerLogo .right {
            width: 378px
              /* 378/28 */
            ;
            height: 88px
              /* 88/28 */
            ;
            float: right;
          }
      
          .headerLogo .right img {
            width: 100%;
            height: 100%;
          }
      
          .contentTable {
            padding-top: 20px
              /* 20/28 */
            ;
          }
      
          .information {
            border: 3px solid #D9D9D9;
          }
      
          .information .infoItem {
              padding: 15px 25px;
            border-bottom: 1px solid #d9d9d9;
            overflow: hidden;
          }
      
          .infoTitle {
            width: 20%;
            color: #666666;
            font-size: 14px;
            float: left;
          }
      
          .infoTag {
            width: 80%;
            color: #000000;
            font-size: 14px;
            float: right;
          }
      
          .information .infoItem:last-child {
            border-bottom: none;
          }
      
          .infoPage {
            margin-top: 20px;
            float: right;
          }
        </style>
      </head>
      
      <body>
        <div class="homeContent">
            <div class="right">
              <img th:src="@{${oneDimCode}}"></img>
            </div>
          </div>
          <div class="contentTable">
            <h2 style="text-align: center;" th:text="${erFormDefName}"></h2>
            <div class="information">
              <div class="infoItem">
                <div class="infoTitle">单据编号</div>
                <div class="infoTag" th:text="${erFormCode}"></div>
              </div>
              <div class="infoItem">
                <div class="infoTitle">单据名称</div>
                <div class="infoTag" th:text="${djName}"></div>
              </div>
              <div class="infoItem">
                <div class="infoTitle">公司</div>
                <div class="infoTag">
                  <span th:text="${corpName}"></span> (
                  <span th:text="${custCode}"></span> )
                </div>
              </div>
              <div class="infoItem">
                <div class="infoTitle">经办人</div>
                <div class="infoTag" th:text="${userName}"></div>
              </div>
              <div class="infoItem">
                <div class="infoTitle">部门</div>
                <div class="infoTag" th:text="${userPersonHROrgunitName}"></div>
              </div>
              <div class="infoItem">
                <div class="infoTitle">金额合计</div>
                <div class="infoTag" th:text="${totalMoney}"></div>
              </div>
              <div class="infoItem">
                <div class="infoTitle">创建日期</div>
                <div class="infoTag" th:text="${erFormDateFormat}"></div>
              </div>
            </div>
          </div>
        </div>
      </body>
      </html>
      
    3. java代码

      private String generateCode() {
        String html = ""; // html串
        Document doc = Jsoup.parse(html);
        Element tds = doc.getElementsByTag("script").get(0); // 标识获取html中第一个<script>标签
        String data = tds.toString().replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "\n\t"); // 去除html中的标签
        String o = null;
        try {
          engine.eval(data);
          Invocable invokeEngine = (Invocable) engine;
          o = (String)invokeEngine.invokeFunction("reverse", "--code--", "==type=="); //执行的js,以及参数
        } catch (ScriptException | NoSuchMethodException e) {
          e.printStackTrace();
        }
      }
      return o;
      
    4. 测试

      public static void main(String[] args) {
        String html = ""; // 上面的html代码
        String c = generateCode();
        System.out.println(o); // EP==type==--code--
      }
      
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Andy's

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

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

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

打赏作者

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

抵扣说明:

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

余额充值