Tampermonkey用户脚本通过关键字搜索获取表格单元格数据?

时间:2018-10-29 10:57:06

标签: javascript userscripts tampermonkey

我是第一次使用Tampermonkey并尝试从包含行data-akfk-type="event">AHOD的表中获取员工姓名

例如,在下表中,此行应返回'Clare''Ivan',因为这些行中的每一行都有data-akfk-type="event">AHOD

该表将始终有两行包含'AHOD',但它们可以在表的任何行上。

<table id="iCalTbl" class="groupCalendar">
<tr id="clare@test.com_data_row" data-dp-employeeid="clare@test.com" class="iCalTblRow">
    <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" id="header-clare@test.com">
        <div class="rowHeaderCell"><a href="#" title="Display employee details"
                data-akfk-type="employee" id="clare@test.com">
                Clare</a>&nbsp;&nbsp;<img src="images/flags/country/spain.png">
        </div>
    </td>
    <td data-dp-col="1" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="2" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="18" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <td data-dp-col="19" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <!-- ... -->
    <td data-dp-col="29" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="30" class="tblCalendDailyViewMain">
        <div data-akfk-type="cell" data-dp-employeeid="clare@test.com">
            <div class="shiftlightblue eventDailyContainer" data-akfk-type="eventContainer">
            <span id="event_1601787" data-akfk-type="event">AHOD</span></div>
        </div>
    </td>
    <td data-dp-col="31" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
</tr>

<tr id="sally@test.com_data_row" data-dp-employeeid="sally@test.com" class="iCalTblRow">
    <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" id="header-sally@test.com">
        <div class="rowHeaderCell"><a href="#" title="Display employee details"
                data-akfk-type="employee" id="sall@test.com">
            Sally</a>&nbsp;&nbsp;<img src="images/flags/country/united_arab_emirates.png">
        </div>
    </td>
    <td data-dp-col="1" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="2" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="18" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="19" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="29" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="30" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="31" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
</tr>
<tr id="ivan@test.com_data_row" data-dp-employeeid="ivan@test.com" class="iCalTblRow">
    <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" id="header-ivan@test.com">
        <div class="rowHeaderCell"><a href="#" title="Display employee details"
            data-akfk-type="employee" id="ivan@test.com">Ivan</a>&nbsp;&nbsp;<img src="images/flags/country/italy.png">
        </div>
    </td>
    <td data-dp-col="1" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="2" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="18" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <td data-dp-col="19" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <!-- ... -->
    <td data-dp-col="29" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="30" class="tblCalendDailyViewMain">
        <div data-akfk-type="cell" data-dp-employeeid="ivan@test.com">
            <div class="shiftlightblue eventDailyContainer" data-akfk-type="eventContainer">
                <span id="event_1601789" data-akfk-type="event">
            AHOD</span></div>
        </div>
    </td>
    <td data-dp-col="31" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
</tr>
</table>

我尝试过:

document.getElementsByClassName("groupCalendar")[0].textContent

这给了我所有的文字内容

  

”   000102030405060708091011121314151617181920212223克莱尔(AHLA)莎莉·伊凡(Cally)的克莱尔(Clare)   AHOD”。

然后使用'AHOD'上的split尝试通过split来获取值的第一个值。

我怀疑我需要将搜索范围缩小到表的DOM元素,而不是整个表。然后在与搜索匹配的行中抓取并赋予属性。

我不知道如何搜索关键字'AHOD',然后获得该员工的姓名?

3 个答案:

答案 0 :(得分:0)

您需要遍历每个tr,请参见下面的代码

var results = '',
  trs = document.getElementsByTagName('tr');

for (var i = 0; i < trs.length; i++) {
  if (trs[i].innerHTML.indexOf('data-akfk-type="event">AHOD') > -1) {
    results += trs[i].getElementsByTagName('a')[0].textContent + "\n";
  }
}

console.log("Results: \n" + results)
<table>

<tr id="clare@test.com_data_row" data-dp-employeeid="clare@test.com" class="iCalTblRow">
  <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" style="white-space:nowrap;" id="header-clare@test.com">
    <div class="cell rowHeaderCell" style="width: 148px;"><a href="#" title="Display employee details" onclick="iCal_showEmployeeInfo(this,event);return false;" data-akfk-type="employee" id="clare@test.com">Clare</a>&nbsp;&nbsp;<img height="16" width="20" src="images/flags/country/spain.png" alt="Spain">
    </div>
  </td>
  <td data-dp-col="0" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="1" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="2" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="3" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="4" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="5" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="6" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="7" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="8" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="9" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="10" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="11" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="12" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="13" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="14" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="15" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="16" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="17" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="18" class="tblCalendDailyViewMain iCalTblCellNow" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="19" class="tblCalendDailyViewMain iCalTblCellNow" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="20" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="21" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="22" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="23" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="24" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="25" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="26" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="27" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="28" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="29" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="30" class="tblCalendDailyViewMain" colspan="16" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" data-akfk-type="cell" data-dp-employeeid="clare@test.com" style="width:255px">
      <div class="shiftlightblue eventDailyContainer" data-akfk-type="eventContainer" style="height:1.6em;padding-top:0.6em"><span id="event_1601787" data-akfk-type="event">AHOD</span></div>
    </div>
  </td>
  <td data-dp-col="31" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="32" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
</tr>

<tr id="sally@test.com_data_row" data-dp-employeeid="sally@test.com" class="iCalTblRow">
  <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" style="white-space:nowrap;" id="header-sally@test.com">
    <div class="cell rowHeaderCell" style="width: 148px;"><a href="#" title="Display employee details" onclick="iCal_showEmployeeInfo(this,event);return false;" data-akfk-type="employee" id="sall@test.com">Sally</a>&nbsp;&nbsp;<img height="16" width="20" src="images/flags/country/united_arab_emirates.png"
        alt="United Arab Emirates"></div>
  </td>
  <td data-dp-col="1" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="2" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="3" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="4" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="5" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="6" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="7" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="8" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="9" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="10" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="11" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="12" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="13" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="14" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="15" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="16" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="17" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="18" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="19" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="20" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="21" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="22" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="23" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="24" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="25" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="26" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="27" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="28" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="29" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="30" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="31" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="32" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="33" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="34" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="35" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="36" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="37" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="38" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="39" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="40" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="41" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="42" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="43" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="44" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="45" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="46" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="47" class="tblCalendDailyViewMain" style="border-left:1px solid #BEC7CC">
    <div style="width:15px"></div>
  </td>
  <td data-dp-col="48" class="tblCalendDailyViewMain" style="border-left: 1px solid rgb(190, 199, 204);">
    <div style="width:15px"></div>
  </td>
</tr>



<tr id="ivan@test.com_data_row" data-dp-employeeid="ivan@test.com" class="iCalTblRow" style="">
  <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" style="white-space:nowrap;" id="header-ivan@test.com">
    <div class="cell rowHeaderCell" style="width: 148px;"><a href="#" title="Display employee details" onclick="iCal_showEmployeeInfo(this,event);return false;" data-akfk-type="employee" id="ivan@test.com">Ivan</a>&nbsp;&nbsp;<img height="16" width="20" src="images/flags/country/italy.png" alt="Italy"></div>
  </td>
  <td data-dp-col="0" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="1" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="2" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="3" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="4" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="5" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="6" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="7" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="8" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="9" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="10" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="11" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="12" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="13" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="14" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="15" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="16" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="17" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="18" class="tblCalendDailyViewMain iCalTblCellNow" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="19" class="tblCalendDailyViewMain iCalTblCellNow" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="20" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="21" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="22" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="23" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="24" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="25" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="26" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="27" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="28" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="29" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="30" class="tblCalendDailyViewMain" colspan="16" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" data-akfk-type="cell" data-dp-employeeid="ivan@test.com" style="width:255px">
      <div class="shiftlightblue eventDailyContainer" data-akfk-type="eventContainer" style="height:1.6em;padding-top:0.6em"><span id="event_1601789" data-akfk-type="event">AHOD</span></div>
    </div>
  </td>
  <td data-dp-col="31" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
  <td data-dp-col="32" class="tblCalendDailyViewMain" style="vertical-align:top;border-left:1px solid #BEC7CC">
    <div class="cell" style="width:15px">&nbsp;</div>
  </td>
</tr>

</table>

答案 1 :(得分:0)

使用jQuery对“ AHOD”节点进行激光定位,然后遍历树以查找所需的数据:

var ahodNodes = $("span[data-akfk-type='event']:contains('AHOD')");
//-- Get the desired related text for each:
ahodNodes.each ( (J, zNode) => {
    var parentRow       = $(zNode).closest ("tr.iCalTblRow");
    var employeeCell    = parentRow.find (".iCalTblEmployeeCell");
    var employeeName    = employeeCell.text ().trim ();
    console.log ("employeeName: ", employeeName);
} );

这也使过渡到AJAX驱动的页面(下文中有更多内容)更加容易。

一个完整的工作脚本(它只是******块上方的部分)。 :
要查看实际效果,请运行代码片段。

// ==UserScript==
// @name     _Get AHOD employees
// @match    *://YOUR_SERVER.COM/YOUR_PATH/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM_addStyle
// @grant    GM.getValue
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.
/* global $ */
/* eslint-disable no-multi-spaces */

var ahodNodes = $("span[data-akfk-type='event']:contains('AHOD')");
//-- Get the desired related text for each:
ahodNodes.each ( (J, zNode) => {
    var parentRow       = $(zNode).closest ("tr.iCalTblRow");
    var employeeCell    = parentRow.find (".iCalTblEmployeeCell");
    var employeeName    = employeeCell.text ().trim ();
    console.log ("employeeName: ", employeeName);
} );

/********************************************************************
******* EVERYTHING below this block is simulated target page. *******
******* It's NOT part of the userscript.                      *******
********************************************************************/
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="iCalTbl" class="groupCalendar">
<tr id="clare@test.com_data_row" data-dp-employeeid="clare@test.com" class="iCalTblRow">
    <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" id="header-clare@test.com">
        <div class="rowHeaderCell"><a href="#" title="Display employee details"
                data-akfk-type="employee" id="clare@test.com">
                Clare</a>&nbsp;&nbsp;<img src="images/flags/country/spain.png">
        </div>
    </td>
    <td data-dp-col="1" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="2" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="18" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <td data-dp-col="19" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <!-- ... -->
    <td data-dp-col="29" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="30" class="tblCalendDailyViewMain">
        <div data-akfk-type="cell" data-dp-employeeid="clare@test.com">
            <div class="shiftlightblue eventDailyContainer" data-akfk-type="eventContainer">
            <span id="event_1601787" data-akfk-type="event">AHOD</span></div>
        </div>
    </td>
    <td data-dp-col="31" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
</tr>

<tr id="sally@test.com_data_row" data-dp-employeeid="sally@test.com" class="iCalTblRow">
    <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" id="header-sally@test.com">
        <div class="rowHeaderCell"><a href="#" title="Display employee details"
                data-akfk-type="employee" id="sall@test.com">
            Sally</a>&nbsp;&nbsp;<img src="images/flags/country/united_arab_emirates.png">
        </div>
    </td>
    <td data-dp-col="1" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="2" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="18" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="19" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="29" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="30" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="31" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
</tr>
<tr id="ivan@test.com_data_row" data-dp-employeeid="ivan@test.com" class="iCalTblRow">
    <td data-dp-col="0" class="iCalTblCell iCalTblEmployeeCell" id="header-ivan@test.com">
        <div class="rowHeaderCell"><a href="#" title="Display employee details"
            data-akfk-type="employee" id="ivan@test.com">Ivan</a>&nbsp;&nbsp;<img src="images/flags/country/italy.png">
        </div>
    </td>
    <td data-dp-col="1" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="2" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
    <td data-dp-col="18" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <td data-dp-col="19" class="tblCalendDailyViewMain iCalTblCellNow">.</td>
    <!-- ... -->
    <td data-dp-col="29" class="tblCalendDailyViewMain">.</td>
    <td data-dp-col="30" class="tblCalendDailyViewMain">
        <div data-akfk-type="cell" data-dp-employeeid="ivan@test.com">
            <div class="shiftlightblue eventDailyContainer" data-akfk-type="eventContainer">
                <span id="event_1601789" data-akfk-type="event">
            AHOD</span></div>
        </div>
    </td>
    <td data-dp-col="31" class="tblCalendDailyViewMain">.</td>
    <!-- ... -->
</tr>
</table>


对于AJAX驱动的页面,过渡很容易:

// ==UserScript==
// @name     _Get AHOD employees
// @match    *://YOUR_SERVER.COM/YOUR_PATH/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// @grant    GM.getValue
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.
/* global $, waitForKeyElements */
/* eslint-disable no-multi-spaces */

waitForKeyElements ("span[data-akfk-type='event']:contains('AHOD')", getEmployeeName);

function getEmployeeName (jNode) {
    var parentRow       = jNode.closest ("tr.iCalTblRow");
    var employeeCell    = parentRow.find (".iCalTblEmployeeCell");
    var employeeName    = employeeCell.text ().trim ();
    console.log ("employeeName: ", employeeName);
}

答案 2 :(得分:0)

我使用了下面的代码,但还将探索其他建议并尝试从中学习:

var results = '',
  trs = document.getElementsByTagName('tr');

for (var i = 0; i < trs.length; i++) {
  if (trs[i].innerHTML.indexOf('data-akfk-type="event">AHOD') > -1) {
    results += trs[i].getElementsByTagName('a')[0].textContent + "\n";
  }
}

console.log("Results: \n" + results) 

非常感谢您的帮助。