Search code examples
javascriptcheckboxuserscriptstampermonkey

Checked related checkbox by keyword or class?


I have many unordered items to place in other folders (file management over the web) Each element has a name and format, also each format has a class.
Screenshot:
Screenshot

Here we see how the files are presented, as I mentioned earlier they have a name and format. It is also seen that each format is shown with a different class, those that are audio type, text type, tablets, etc.

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>

How would a script run through all the files, take part of the text and check it?

For example, if I want to select all the elements that contain the keyword 'MacOS', then select all those that contain the word 'MacOS' or maybe its format. I have not seen anything like this either in extensions or userscripts.

Can you create something like that?

If you can not create something like that, then I guess you could take your 'class' and automatically select all the matching ones?

In summary: How would a script only mark the '.rar' files, for example, among all the others?

DEMO on JSFiddle


Solution

  • First identify nodes that indicate the file(s) you want. For .rar files it would be like:

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

    If you really want only .rar and not other kinds of zipped files, then:

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



    Then, traverse the DOM to the related checkboxes:

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


    And check them:

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

    All that in jQuery:

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

    -- where the .has(...) bit is optional.



    If the page is AJAX (javascript) driven, use waitForKeyElements or MutationObserver.

    Demo:

    // ==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>