Clicking multiple buttons automatically with the addition of waitForKeyElements












0















I've been searching for a way to automatically select survey form buttons that contain specific words across different websites. I am using Tampermonkey in Google Chrome to achieve this. In this case, I would like to automatically select buttons with the word 'Male' and 'Mr.' in them.



Here is an example of the type of button I am talking about (including CSS/HTML):



enter image description here



I am currently using the following code in Tampermonkey to achieve the pressing effect:



// ==UserScript==
// @name Male Test 3
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://tasupport.co1.qualtrics.com/jfe/form/SV_0qez6NHJGoCAmMt
// @match https://tasupport.co1.qualtrics.com/jfe2/form/SV_0qez6NHJGoCAmMt/next?rand=775440350&tid=1&t=1547858208474
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==

const fire = () => Array.from(document.querySelectorAll('label > span')).find(e => e.textContent === 'Male').parentElement.click();

waitForKeyElements (
"label",
fire
);


The above script successfully selects "Male" upon page load of the above site, but I am unsure how to click on multiple buttons at the same time. I want to also automatically select "Mr." as well, which is another button on the page. So ultimately, I want the script to select Male AND Mr. when the page loads. Fortunately, they are both within a "label," so I think the waitForKeyElements might be fine.



Unfortunately, JavaSript isn't my wheelhouse and I'm having trouble finding the correct syntax to make this possible. It seems like such an easy fix.



I tried things like find(e => e.textContent === 'Male', 'Mr.') but to no avail.



Essentially, all I need to do is include the text for 'Mr.' along with the already specified 'Male.' Any and all help would be awesome.










share|improve this question



























    0















    I've been searching for a way to automatically select survey form buttons that contain specific words across different websites. I am using Tampermonkey in Google Chrome to achieve this. In this case, I would like to automatically select buttons with the word 'Male' and 'Mr.' in them.



    Here is an example of the type of button I am talking about (including CSS/HTML):



    enter image description here



    I am currently using the following code in Tampermonkey to achieve the pressing effect:



    // ==UserScript==
    // @name Male Test 3
    // @namespace http://tampermonkey.net/
    // @version 0.1
    // @description try to take over the world!
    // @author You
    // @match https://tasupport.co1.qualtrics.com/jfe/form/SV_0qez6NHJGoCAmMt
    // @match https://tasupport.co1.qualtrics.com/jfe2/form/SV_0qez6NHJGoCAmMt/next?rand=775440350&tid=1&t=1547858208474
    // @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
    // @require https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant GM_addStyle
    // ==/UserScript==

    const fire = () => Array.from(document.querySelectorAll('label > span')).find(e => e.textContent === 'Male').parentElement.click();

    waitForKeyElements (
    "label",
    fire
    );


    The above script successfully selects "Male" upon page load of the above site, but I am unsure how to click on multiple buttons at the same time. I want to also automatically select "Mr." as well, which is another button on the page. So ultimately, I want the script to select Male AND Mr. when the page loads. Fortunately, they are both within a "label," so I think the waitForKeyElements might be fine.



    Unfortunately, JavaSript isn't my wheelhouse and I'm having trouble finding the correct syntax to make this possible. It seems like such an easy fix.



    I tried things like find(e => e.textContent === 'Male', 'Mr.') but to no avail.



    Essentially, all I need to do is include the text for 'Mr.' along with the already specified 'Male.' Any and all help would be awesome.










    share|improve this question

























      0












      0








      0








      I've been searching for a way to automatically select survey form buttons that contain specific words across different websites. I am using Tampermonkey in Google Chrome to achieve this. In this case, I would like to automatically select buttons with the word 'Male' and 'Mr.' in them.



      Here is an example of the type of button I am talking about (including CSS/HTML):



      enter image description here



      I am currently using the following code in Tampermonkey to achieve the pressing effect:



      // ==UserScript==
      // @name Male Test 3
      // @namespace http://tampermonkey.net/
      // @version 0.1
      // @description try to take over the world!
      // @author You
      // @match https://tasupport.co1.qualtrics.com/jfe/form/SV_0qez6NHJGoCAmMt
      // @match https://tasupport.co1.qualtrics.com/jfe2/form/SV_0qez6NHJGoCAmMt/next?rand=775440350&tid=1&t=1547858208474
      // @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
      // @require https://gist.github.com/raw/2625891/waitForKeyElements.js
      // @grant GM_addStyle
      // ==/UserScript==

      const fire = () => Array.from(document.querySelectorAll('label > span')).find(e => e.textContent === 'Male').parentElement.click();

      waitForKeyElements (
      "label",
      fire
      );


      The above script successfully selects "Male" upon page load of the above site, but I am unsure how to click on multiple buttons at the same time. I want to also automatically select "Mr." as well, which is another button on the page. So ultimately, I want the script to select Male AND Mr. when the page loads. Fortunately, they are both within a "label," so I think the waitForKeyElements might be fine.



      Unfortunately, JavaSript isn't my wheelhouse and I'm having trouble finding the correct syntax to make this possible. It seems like such an easy fix.



      I tried things like find(e => e.textContent === 'Male', 'Mr.') but to no avail.



      Essentially, all I need to do is include the text for 'Mr.' along with the already specified 'Male.' Any and all help would be awesome.










      share|improve this question














      I've been searching for a way to automatically select survey form buttons that contain specific words across different websites. I am using Tampermonkey in Google Chrome to achieve this. In this case, I would like to automatically select buttons with the word 'Male' and 'Mr.' in them.



      Here is an example of the type of button I am talking about (including CSS/HTML):



      enter image description here



      I am currently using the following code in Tampermonkey to achieve the pressing effect:



      // ==UserScript==
      // @name Male Test 3
      // @namespace http://tampermonkey.net/
      // @version 0.1
      // @description try to take over the world!
      // @author You
      // @match https://tasupport.co1.qualtrics.com/jfe/form/SV_0qez6NHJGoCAmMt
      // @match https://tasupport.co1.qualtrics.com/jfe2/form/SV_0qez6NHJGoCAmMt/next?rand=775440350&tid=1&t=1547858208474
      // @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
      // @require https://gist.github.com/raw/2625891/waitForKeyElements.js
      // @grant GM_addStyle
      // ==/UserScript==

      const fire = () => Array.from(document.querySelectorAll('label > span')).find(e => e.textContent === 'Male').parentElement.click();

      waitForKeyElements (
      "label",
      fire
      );


      The above script successfully selects "Male" upon page load of the above site, but I am unsure how to click on multiple buttons at the same time. I want to also automatically select "Mr." as well, which is another button on the page. So ultimately, I want the script to select Male AND Mr. when the page loads. Fortunately, they are both within a "label," so I think the waitForKeyElements might be fine.



      Unfortunately, JavaSript isn't my wheelhouse and I'm having trouble finding the correct syntax to make this possible. It seems like such an easy fix.



      I tried things like find(e => e.textContent === 'Male', 'Mr.') but to no avail.



      Essentially, all I need to do is include the text for 'Mr.' along with the already specified 'Male.' Any and all help would be awesome.







      javascript greasemonkey autofill userscripts tampermonkey






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 3:36









      theCrabNebulatheCrabNebula

      8619




      8619
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can make a function that, given a <span>'s text to find, .finds the appropriate element and clicks its parent. Once waitForKeyElements triggers, call that function with Male and Mr.:



          const findAndClickParent = text => [...document.querySelectorAll('label > span')]
          .find(e => e.textContent === text)
          .parentElement
          .click();

          waitForKeyElements (
          "label",
          () => {
          findAndClickParent('Male');
          findAndClickParent('Mr.');
          }
          );


          If you wanted to be more efficient, you could create an object of those matching elements when the callback triggers (assuming none that you care about overlap), and access the appropriate property each time, for example:



          waitForKeyElements (
          "label",
          () => {
          const parentsByTextContent = Array.prototype.reduce.call(
          document.querySelectorAll('label > span'),
          (a, e) => {
          a[e.textContent] = e.parentElement;
          return a;
          },
          {}
          );
          parentsByTextContent['Male'].click();
          parentsByTextContent['Mr.'].click();
          }
          );





          share|improve this answer
























          • worked perfectly. thank you very much. Also going to try the second approach

            – theCrabNebula
            Jan 19 at 5:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263861%2fclicking-multiple-buttons-automatically-with-the-addition-of-waitforkeyelements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can make a function that, given a <span>'s text to find, .finds the appropriate element and clicks its parent. Once waitForKeyElements triggers, call that function with Male and Mr.:



          const findAndClickParent = text => [...document.querySelectorAll('label > span')]
          .find(e => e.textContent === text)
          .parentElement
          .click();

          waitForKeyElements (
          "label",
          () => {
          findAndClickParent('Male');
          findAndClickParent('Mr.');
          }
          );


          If you wanted to be more efficient, you could create an object of those matching elements when the callback triggers (assuming none that you care about overlap), and access the appropriate property each time, for example:



          waitForKeyElements (
          "label",
          () => {
          const parentsByTextContent = Array.prototype.reduce.call(
          document.querySelectorAll('label > span'),
          (a, e) => {
          a[e.textContent] = e.parentElement;
          return a;
          },
          {}
          );
          parentsByTextContent['Male'].click();
          parentsByTextContent['Mr.'].click();
          }
          );





          share|improve this answer
























          • worked perfectly. thank you very much. Also going to try the second approach

            – theCrabNebula
            Jan 19 at 5:06
















          1














          You can make a function that, given a <span>'s text to find, .finds the appropriate element and clicks its parent. Once waitForKeyElements triggers, call that function with Male and Mr.:



          const findAndClickParent = text => [...document.querySelectorAll('label > span')]
          .find(e => e.textContent === text)
          .parentElement
          .click();

          waitForKeyElements (
          "label",
          () => {
          findAndClickParent('Male');
          findAndClickParent('Mr.');
          }
          );


          If you wanted to be more efficient, you could create an object of those matching elements when the callback triggers (assuming none that you care about overlap), and access the appropriate property each time, for example:



          waitForKeyElements (
          "label",
          () => {
          const parentsByTextContent = Array.prototype.reduce.call(
          document.querySelectorAll('label > span'),
          (a, e) => {
          a[e.textContent] = e.parentElement;
          return a;
          },
          {}
          );
          parentsByTextContent['Male'].click();
          parentsByTextContent['Mr.'].click();
          }
          );





          share|improve this answer
























          • worked perfectly. thank you very much. Also going to try the second approach

            – theCrabNebula
            Jan 19 at 5:06














          1












          1








          1







          You can make a function that, given a <span>'s text to find, .finds the appropriate element and clicks its parent. Once waitForKeyElements triggers, call that function with Male and Mr.:



          const findAndClickParent = text => [...document.querySelectorAll('label > span')]
          .find(e => e.textContent === text)
          .parentElement
          .click();

          waitForKeyElements (
          "label",
          () => {
          findAndClickParent('Male');
          findAndClickParent('Mr.');
          }
          );


          If you wanted to be more efficient, you could create an object of those matching elements when the callback triggers (assuming none that you care about overlap), and access the appropriate property each time, for example:



          waitForKeyElements (
          "label",
          () => {
          const parentsByTextContent = Array.prototype.reduce.call(
          document.querySelectorAll('label > span'),
          (a, e) => {
          a[e.textContent] = e.parentElement;
          return a;
          },
          {}
          );
          parentsByTextContent['Male'].click();
          parentsByTextContent['Mr.'].click();
          }
          );





          share|improve this answer













          You can make a function that, given a <span>'s text to find, .finds the appropriate element and clicks its parent. Once waitForKeyElements triggers, call that function with Male and Mr.:



          const findAndClickParent = text => [...document.querySelectorAll('label > span')]
          .find(e => e.textContent === text)
          .parentElement
          .click();

          waitForKeyElements (
          "label",
          () => {
          findAndClickParent('Male');
          findAndClickParent('Mr.');
          }
          );


          If you wanted to be more efficient, you could create an object of those matching elements when the callback triggers (assuming none that you care about overlap), and access the appropriate property each time, for example:



          waitForKeyElements (
          "label",
          () => {
          const parentsByTextContent = Array.prototype.reduce.call(
          document.querySelectorAll('label > span'),
          (a, e) => {
          a[e.textContent] = e.parentElement;
          return a;
          },
          {}
          );
          parentsByTextContent['Male'].click();
          parentsByTextContent['Mr.'].click();
          }
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 3:41









          CertainPerformanceCertainPerformance

          83.3k144067




          83.3k144067













          • worked perfectly. thank you very much. Also going to try the second approach

            – theCrabNebula
            Jan 19 at 5:06



















          • worked perfectly. thank you very much. Also going to try the second approach

            – theCrabNebula
            Jan 19 at 5:06

















          worked perfectly. thank you very much. Also going to try the second approach

          – theCrabNebula
          Jan 19 at 5:06





          worked perfectly. thank you very much. Also going to try the second approach

          – theCrabNebula
          Jan 19 at 5:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263861%2fclicking-multiple-buttons-automatically-with-the-addition-of-waitforkeyelements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage