1. API Information

    The TurkerView API page has instructions as well as contact information for support in getting set up with our new API infrastructure.

    You can also email us directly at hello@turkerview.com if you run into any problems!

    Dismiss Notice

[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,472
    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();
        }
    });
    
    
     
    • Useful / Informative Useful / Informative x 13
    • Love Love x 8
    • Like Like x 4
    • 5/5 Pay 5/5 Pay x 1
    Last edited: Jan 24, 2017
  2. RicanGuy86

    RicanGuy86 Survey Slinger

    Messages:
    3,761
    Ratings:
    +7,333
    Awesome!
     
    • Like Like x 2
  3. RicanGuy86

    RicanGuy86 Survey Slinger

    Messages:
    3,761
    Ratings:
    +7,333
    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,472
    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
     
    • LOL LOL x 1
  5. RicanGuy86

    RicanGuy86 Survey Slinger

    Messages:
    3,761
    Ratings:
    +7,333
    @ChrisTurk is so nice.
     
    • Like Like x 1
  6. Yuk

    Yuk Well-Known Turker

    Messages:
    1,768
    Ratings:
    +3,456
    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.
     
    • Useful / Informative Useful / Informative x 3
    • Like Like x 2
    • Love Love x 1
  7. Sunlite

    Sunlite Survey Slinger

    Messages:
    4,541
    Gender:
    Female
    Ratings:
    +7,137
    Thank you so much.
     
    • Love Love x 1
  8. Eisenpower

    Eisenpower Survey Slinger

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

    MasterNyborg Survey Slinger

    Messages:
    1,978
    Gender:
    Male
    Ratings:
    +3,235
    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,472
    [​IMG]
     
    • LOL LOL x 4
    • Love Love x 2
    • 5/5 Pay 5/5 Pay x 1
    • Useful / Informative Useful / Informative x 1
  11. Randomacts

    Randomacts Survey Slinger

    Messages:
    80,724
    Gender:
    Male
    Ratings:
    +108,858
    • Useful / Informative Useful / Informative x 4
  12. thedorchannel

    thedorchannel Active Turker

    Messages:
    576
    Gender:
    Male
    Ratings:
    +998
    hai
     
    • LOL LOL x 1