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

问题描述 投票:1回答:1

我有许多无序项目放在其他文件夹中(通过Web进行文件管理)每个元素都有一个名称和格式,每个格式都有一个类。 截图: Screenshot

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

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>

脚本如何运行所有文件,参与文本并检查它?

例如,如果我想选择包含关键字“MacOS”的所有元素,则选择包含“MacOS”一词或其格式的所有元素。我没有在扩展或用户脚本中看到过类似的内容。

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

如果你不能创造这样的东西,那么我猜你可以拿你的'课'并自动选择所有匹配的?

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

DEMO on JSFiddle

javascript checkbox userscripts tampermonkey
1个回答
1
投票

首先标识指示所需文件的节点。对于.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>
© www.soinside.com 2019 - 2024. All rights reserved.