[Guide] Learning to script AMT HITs

Discussion in 'Help & Guides' started by Ethraiel, Jan 18, 2017.

  1. Ethraiel

    Ethraiel Well-Known Turker

    Messages:
    660
    Gender:
    Male
    Ratings:
    +1,482
    If you're interested in learning how to write a script for a HIT on mturk i've made a series of 5 videos going over some of the more common things you might want to do.
    I chose the ProductRnR 8 cent HITs to script in the videos because they have quite a few elements in common with a lot of HITs, it also pops up often enough that you can follow along on that specific HIT.
    I will go over some basics of jquery and some basic functions used often when scripting HITs, but the videos are no replacement for the sites designed to teach you the broader concepts of coding. As @Yuk mentions in their comment below, learning the basics of HTML CSS and how javascript interacts with them is invaluable. Code academy was mentioned (which i also used) so I've included links to their html/css, Javascript, and Jquery lessons (which i highly recommend) and a couple other resources as well.
    *disclaimer: i still consider myself a noobie scripter... so erm... not an expert teacher or anything.


    In part one we learn about metadata, how to get the proper URL for our include/match line, a little jquery, and we'll end up with a one line script that selects all "related" radio buttons when the HIT is loaded.

    Code:
    // ==UserScript==
    // @name RNRscript
    // @version .01
    // @description This is easy
    // @author You
    // @include *mturkcontent*
    // @require https://code.jquery.com/jquery-3.0.0-alpha1.min.js
    // ==/UserScript==
    $("input[value=QueryImage_Related]").click();
    

    In part two we go over the dangers of running a script without a sanity check and i'll show you my way of creating one (there are many ways to do it). We'll learn what exactly a sanity check does, how to use console.log(), and how to call an element using generic selectors and the .eq() function of jquery.

    Code:
    // ==UserScript==
    // @name RNRscript
    // @version .01
    // @description This is easy
    // @author You
    // @include *mturkcontent*
    // @require https://code.jquery.com/jquery-3.0.0-alpha1.min.js
    // ==/UserScript==
    var sanity = "Unrelated or Only Indirectly Related";
    if($("b").eq(1).text() === sanity){
    console.log("Running");
    $("input[value=QueryImage_Related]").click();
    }
    

    In part three we're going to create a keyup() event listener that will click the submit button when "enter" is released. We'll briefly go over the event data using console.log() including finding the keycode of a keyboard event without external sites. *Note that i probably should have included an event.preventDefault(); line at the end of the video, but i plan on going through that in a future video.

    W3 Pages

    Jquery API Pages
    Keycode website used in vid
    Javascript Char Codes (Key Codes) - Cambia Research​
    *please note i added the event.preventDefault() line, i discuss this in the last video
    Code:
    // ==UserScript==
    // @name RNRscript
    // @version .01
    // @description This is easy
    // @author You
    // @include *mturkcontent*
    // @require https://code.jquery.com/jquery-3.0.0-alpha1.min.js
    // ==/UserScript==
    var sanity = "Unrelated or Only Indirectly Related";
    if($("b").eq(1).text() === sanity){
    console.log("Running");
    $("input[value=QueryImage_Related]").click();
    }
    $(document).keyup(function(event) {
    if(event.which == 13){
    event.preventDefault();
    $("#SubmitButton").click();
    }
    });
    

    In part four we're going to hide the instructions using .toggle(), insert an html button element then write a click event listener that will toggle the instructions back to visible.

    Code:
    // ==UserScript==
    // @name RNRscript
    // @version .01
    // @description This is easy
    // @author You
    // @include *mturkcontent*
    // @require https://code.jquery.com/jquery-3.0.0-alpha1.min.js
    // ==/UserScript==
    var sanity = "Unrelated or Only Indirectly Related";
    if($("b").eq(1).text() === sanity){
    console.log("Running");
    $("input[value=QueryImage_Related]").click();
    }
    $(document).keyup(function(event) {
    if(event.which == 13){
    event.preventDefault();
    $("#SubmitButton").click();
    }
    });
    $("#header").toggle();
    var myButton = '<button type="button" id="myButton">Instructions</button>';
    $("#header").before(myButton);
    $("#myButton").click(function(){
    $("#header").toggle();
    });
    

    In the last video we use a couple tree traversal methods to toggle the radio buttons when we click the image above them. We'll use event.preventDefault to stop the default action of clicking on the images and end up with something that could be used to do a few HITs. (because what's the point if you never use it). I end a bit rambly but i just couldn't say goodbye ;)

    Code:
    // ==UserScript==
    // @name RNRscript
    // @version .01
    // @description This is easy
    // @author You
    // @include *mturkcontent*
    // @require https://code.jquery.com/jquery-3.0.0-alpha1.min.js
    // ==/UserScript==
    var sanity = "Unrelated or Only Indirectly Related";
    if($("b").eq(1).text() === sanity){
    console.log("Running");
    $("input[value=QueryImage_Related]").click();
    }
    $(document).keyup(function(event) {
    if(event.which == 13){
    event.preventDefault();
    $("#SubmitButton").click();
    }
    });
    $("#header").toggle();
    var myButton = '<button type="button" id="myButton">Instructions</button>';
    $("#header").before(myButton);
    $("#myButton").click(function(){
    $("#header").toggle();
    });
    $(".imagebox").click(function(event){
    event.preventDefault();
    var THIS = $(this).siblings().children("input");
    if(THIS.eq(0).prop('checked') === true){
    THIS.eq(1).click();
    }
    else{
    THIS.eq(0).click();
    }
    });
    
     
    • Today I Learned Today I Learned x 15
    • Love Love x 11
    • Like Like x 5
    • 5/5 Pay 5/5 Pay x 1
    Last edited: Jan 24, 2017
  2. RicanGuy86

    RicanGuy86 Survey Slinger

    Messages:
    4,452
    Ratings:
    +9,138
    Awesome!
     
    • Like Like x 2
  3. RicanGuy86

    RicanGuy86 Survey Slinger

    Messages:
    4,452
    Ratings:
    +9,138
    I think this guide (when you're through with it, of course) would make an excellent contribution for the Wiki.
     
  4. Ethraiel

    Ethraiel Well-Known Turker

    Messages:
    660
    Gender:
    Male
    Ratings:
    +1,482
    I was talking a bit with @ChrisTurk about that but im actually really bad at bbcode :lol: he had to pretty much re-do the entire Overwatch page for me so if someone wants to create one around this thread that'd be great, but i'd probably just make more work for the admin :p
     
    • Like Like x 1
    • LOL LOL x 1
  5. RicanGuy86

    RicanGuy86 Survey Slinger

    Messages:
    4,452
    Ratings:
    +9,138
    @ChrisTurk is so nice.
     
    • Like Like x 1
  6. Yuk

    Yuk Zing King

    Messages:
    2,585
    Ratings:
    +5,060
    Great guide! Thank you for creating and sharing it with us.

    Something I would like to add: From my own personal experience of learning to script, I didn't even understand a basic page layout of CSS or HTML or anything like that at the start, as I assume 99% of other turkers also fall into. I feel like acquiring a basic understanding of CSS and HTML as a first step is a must for any form of scripting. The website I learned the basics of CSS and HTML from is https://www.codecademy.com/

    They have courses you can select from (for free) where they will walk you through on the subject of whatever course you've selected. Of course, most of those guides are extremely generic and broad and you don't need to know a lot at all to write a script for an mturk HIT. I think I only went through half of the CSS/HTML course and like 25% of the Javascript course before I realized that beyond the basics of the basics what they were teaching me wasn't relevant to making a good mturk script at all.

    It took me literally 3 or 4 hours total sitting down and following their guide before I was able to write my own functioning script (which is easy on a slow weekend). If your videos had existed back then it would've definitely expedited it even more.
    Also, analyzing scripts you already have to see how they work is super super helpful after you've already completed these beginner steps.
     
    • Today I Learned Today I Learned x 3
    • Like Like x 2
    • Love Love x 2
  7. Sunlite

    Sunlite Survey Slinger

    Messages:
    5,028
    Gender:
    Female
    Ratings:
    +7,755
    Thank you so much.
     
    • Love Love x 1
  8. Eisenpower

    Eisenpower Survey Slinger

    Messages:
    4,873
    Gender:
    Male
    Ratings:
    +11,914
    I'm late, but this awesome dude! :emoji_grin:
     
    • Like Like x 1
    • Love Love x 1
  9. MasterNyborg

    MasterNyborg Survey Slinger

    Messages:
    1,982
    Gender:
    Male
    Ratings:
    +3,236
    Rigged together my very first script for a different batch while watching these videos. Thanks so much @Ethraiel!
     
    • Love Love x 3
    • Like Like x 2
  10. Ethraiel

    Ethraiel Well-Known Turker

    Messages:
    660
    Gender:
    Male
    Ratings:
    +1,482
    [​IMG]
     
    • LOL LOL x 6
    • Love Love x 2
    • 5/5 Pay 5/5 Pay x 1
    • Today I Learned Today I Learned x 1
  11. Randomacts

    Randomacts Survey Slinger

    Messages:
    94,624
    Gender:
    Male
    Ratings:
    +124,076
    • Today I Learned Today I Learned x 5
  12. thedorchannel

    thedorchannel Well-Known Turker

    Messages:
    1,916
    Gender:
    Male
    Ratings:
    +3,761
    hai
     
    • LOL LOL x 1
  13. Belkor

    Belkor New Turker

    Messages:
    20
    Gender:
    Male
    Ratings:
    +37
    I've been looking through the JQuery API along with a number of coding websites. The .click() function works great for the radio buttons but is there something that works on sliders? A function that moves the slider all the way to the right for example.
     
  14. Phil Wheeler

    Phil Wheeler New Turker

    Messages:
    13
    Gender:
    Male
    Ratings:
    +1
    Oh swet thanks I'm about to watch these tonight and get to work! luckily I have html css networking and beginning programming experience too
     
  15. Phil Wheeler

    Phil Wheeler New Turker

    Messages:
    13
    Gender:
    Male
    Ratings:
    +1
    I believe I saw something like that I will see if I can find it again.
     
  16. Havoc928

    Havoc928 Active Turker

    Messages:
    854
    Gender:
    Male
    Ratings:
    +818
    I did this in the past and it worked great, for some reason now I get the error with Jquery "$ is not defined"

    Can anyone help with this?
     
  17. Eisenpower

    Eisenpower Survey Slinger

    Messages:
    4,873
    Gender:
    Male
    Ratings:
    +11,914
    I don't use Jquery anymore, but I know Tampermonkey updated and it stopped recognizing it natively.
    Try adding this to the top:
    Code:
    var $ = window.jQuery;
     
  18. Havoc928

    Havoc928 Active Turker

    Messages:
    854
    Gender:
    Male
    Ratings:
    +818
    That worked, thanks. Trying to figure this A9 hit out and it is kicking my ass. Hits all of the yes buttons.
     
    • Like Like x 1
  19. BakaOppai

    BakaOppai New Turker

    Messages:
    5
    Ratings:
    +1
    These are some old posts but I find it hard to believe people didnt get mass banned for this. As a programmer myself this is sooooooo easily detectable, are they that incompetent?
     
    • LOL LOL x 1
  20. limcid

    limcid New Turker

    Messages:
    10
    Gender:
    Male
    Ratings:
    +18
    Well, this isn't cheating. This is about being smart and working efficiently. The worker still has to think and work to complete the HIT.
     
    • Like Like x 2