按关键字或 class 检查相关复选框?

Checked related checkbox by keyword or class?

我有很多未排序的项目放在其他文件夹中(通过网络进行文件管理)每个元素都有一个名称和格式,每个格式都有一个 class。
截图:

这里我们看到文件是如何呈现的,正如我之前提到的,它们有名称和格式。 还可以看出,每种格式都显示了不同的class,即音频类型、文本类型、平板电脑等

HTML:

<div id="FilesListContainer">
  ...
  <div id="listView">
    <div class="filerow alt fileItemContainer">
      <div class="fileinfo tab">
        <ul class="borderRadius tabGradientBg">
          <li><span>56,5 MB</span></li>
          <li><span class="date">6 mar 19 20:04</span></li>
          <li><span><input type="checkbox" value="6729995901" name="selectFileItem"></span></li>
        </ul>
      </div>
      <div onmouseover="$('.visibleArrow', this).css('visibility', 'visible')" onmouseout="$('.visibleArrow', this).css('visibility', 'hidden');" class="filename txt">
        <h3>
          <a class="expanderHeader downloadAction downloadContext" href="/Barbarella-Dejah.Thoris.002.2019.5.covers.Digital.DR.and.Quinch-Empire,6729995901.cbr" title="Barbarella-Dejah.Thoris.002.2019.5.covers.Digital.DR.and.Quinch-Empire">
            <span class="bold">Barbarella-Dejah.Thoris.002.2019.5.covers.Digital.<span class="e"> </span>DR.and.Quinch-Empire</span>.cbr
          </a>
        </h3>
      </div>
      <div style="clear:left;">
        <span class="filedescription" style="display: none"></span>
      </div>
    </div>
    <div class="filerow fileItemContainer">
      <div class="fileinfo tab">
        <ul class="borderRadius tabGradientBg">
          <li><span>3,90 GB</span></li>
          <li><span class="date">6 mar 19 18:44</span></li>
          <li><span><input type="checkbox" value="6729949482" name="selectFileItem"></span></li>
        </ul>
      </div>
      <div onmouseover="$('.visibleArrow', this).css('visibility', 'visible')" onmouseout="$('.visibleArrow', this).css('visibility', 'hidden');" class="filename zip">
        <h3>
          <a class="expanderHeader downloadAction downloadContext" href="/Syrnont.2016.F01.1080c.OyhEnl.k265.10o.NNP.2.0.EMrebK,6729949482.rar(archive)" title="Syrnont.2016.F01.1080c.OyhEnl.k265.10o.NNP.2.0.EMrebK">
            <span class="bold">Syrnont.2016.F01.1080c.OyhEnl.k265.10o.NNP.2.0.EMr<span class="e"> </span>ebK</span>.rar
          </a>
        </h3>
      </div>
      <div style="clear:left;">
        <span class="filedescription" style="display: none"></span>
      </div>
    </div>
    <div class="filerow alt fileItemContainer">
      <div class="fileinfo tab">
        <ul class="borderRadius tabGradientBg">
          <li><span>139 KB</span></li>
          <li><span class="date">6 mar 19 17:15</span></li>
          <li><span><input type="checkbox" value="6729877801" name="selectFileItem"></span></li>
        </ul>
      </div>
      <div onmouseover="$('.visibleArrow', this).css('visibility', 'visible')" onmouseout="$('.visibleArrow', this).css('visibility', 'hidden');" class="filename pdf">
        <h3>
          <a class="expanderHeader downloadAction downloadContext" href="/December-2009-FA4A,6729877801.pdf" title="December-2009-FA4A">
            <span class="bold">December-2009-FA4A</span>.pdf
          </a>
        </h3>
      </div>
      <div style="clear:left;">
        <span class="filedescription" style="display: none"></span>
      </div>
    </div>
  </div>
</div>

脚本如何运行遍历所有文件,提取部分文本并进行检查?

例如,如果我想select所有包含关键字的元素 'MacOS',然后是 select 所有包含单词 'MacOS' 或者它的词的那些 格式。 我在扩展程序或用户脚本中都没有看到这样的东西。

你能创造出类似的东西吗?

如果你不能创建类似的东西,那么我想你可以使用你的 'class' 并自动 select 所有匹配的?

总结:脚本如何只标记'.rar'文件,例如,在所有其他文件中?

DEMO on JSFiddle

首先确定指示您想要的文件的节点。对于 .rar 个文件,它就像:

var zipFiles = document.querySelectorAll (".fileItemContainer > .filename.zip");

如果您真的只想要 .rar 而不需要其他类型的压缩文件,那么:

zipFiles = Array.from (zipFiles).filter (node => /\.rar\b/i.test (node.querySelector (".downloadContext").textContent) );



然后,遍历DOM到相关的复选框:

zipFiles.forEach (node => {
    var theChckBox = node.previousElementSibling.querySelector ("input[type='checkbox']");
} );


然后检查它们:

zipFiles.forEach (node => {
    var theChckBox = node.previousElementSibling.querySelector ("input[type='checkbox']");
    theChckBox.checked = true;
} );

在jQuery中的所有内容:

$(".fileItemContainer > .filename.zip").has (".downloadContext:contains(.rar)").prev ().find ("input[type='checkbox']").prop ("checked", true);

-- 其中 .has(...) 位是可选的。



如果页面由 AJAX (javascript) 驱动,请使用 waitForKeyElementsMutationObserver

演示:

// ==UserScript==
// @name     _Check select text boxes
// @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==
// @grant    none
//- The @grant directives are needed to restore the proper sandbox.
/* global $, waitForKeyElements */
/* eslint-disable no-multi-spaces, */

waitForKeyElements (".fileItemContainer > .filename.zip", clickRelatedCheckbox);

function clickRelatedCheckbox (jNode) {
    var theChckBox = jNode.prev ().find ("input[type='checkbox']");
    theChckBox.prop ("checked", true);
}

/********************************************************************
******* Everything below this block, including the other      *******
******* blocks is simulated target page.                      *******
******* It's NOT part of the userscript.                      *******
********************************************************************/
ul, li, div {margin: 0; padding: 0;}
li {display: inline-block; margin-right: 1em;}
h3 {
    margin: 0 0 2ex 0;
    max-width: 95%;
    overflow: auto;
    white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//greasyfork.org/scripts/2199-waitforkeyelements/code/waitForKeyElements.js"></script>
<div id="FilesListContainer">
  ...
  <div id="listView">
    <div class="filerow alt fileItemContainer">
      <div class="fileinfo tab">
        <ul class="borderRadius tabGradientBg">
          <li><span>56,5 MB</span></li>
          <li><span class="date">6 mar 19 20:04</span></li>
          <li><span><input type="checkbox" value="6729995901" name="selectFileItem"></span></li>
        </ul>
      </div>
      <div class="filename txt">
        <h3>
          <a class="expanderHeader downloadAction downloadContext"
            href="/Barbarella-Dejah.Thoris.002.2019.5.covers.Digital.DR.and.Quinch-Empire,6729995901.cbr">
            <span class="bold">Barbarella-Dejah.Thoris.002.2019.5.covers.Digital.<span class="e">
              </span>DR.and.Quinch-Empire</span>.cbr
          </a>
        </h3>
      </div>
    </div>
    <div class="filerow fileItemContainer">
      <div class="fileinfo tab">
        <ul class="borderRadius tabGradientBg">
          <li><span>3,90 GB</span></li>
          <li><span class="date">6 mar 19 18:44</span></li>
          <li><span><input type="checkbox" value="6729949482" name="selectFileItem"></span></li>
        </ul>
      </div>
      <div class="filename zip">
        <h3>
          <a class="expanderHeader downloadAction downloadContext"
            href="/Syrnont.2016.F01.1080c.OyhEnl.k265.10o.NNP.2.0.EMrebK,6729949482.rar(archive)">
            <span class="bold">Syrnont.2016.F01.1080c.OyhEnl.k265.10o.NNP.2.0.EMr<span class="e"> </span>ebK</span>.rar
          </a>
        </h3>
      </div>
    </div>
    <div class="filerow alt fileItemContainer">
      <div class="fileinfo tab">
        <ul class="borderRadius tabGradientBg">
          <li><span>139 KB</span></li>
          <li><span class="date">6 mar 19 17:15</span></li>
          <li><span><input type="checkbox" value="6729877801" name="selectFileItem"></span></li>
        </ul>
      </div>
      <div class="filename pdf">
        <h3>
          <a class="expanderHeader downloadAction downloadContext" href="/December-2009-FA4A,6729877801.pdf"
            title="December-2009-FA4A">
            <span class="bold">December-2009-FA4A</span>.pdf
          </a>
        </h3>
      </div>
    </div>
  </div>
</div>