mTurk Parent Window Command Accepter

Updated for the Worker-only (no WWW) world.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         mTurk Parent Window Command Accepter
// @namespace    salembeats
// @version      2.6
// @description  Updated for the Worker-only (no WWW) world.
// @include      https://www.mturk.com/mturk/preview?*
// @include      https://www.mturk.com/mturk/accept?*
// @include      https://www.mturk.com/mturk/continue?*
// @include      https://www.mturk.com/mturk/return?*
// @include      https://worker.mturk.com/projects/*/tasks*
// @icon         http://ez-link.us/sb-png
// @grant        none
// ==/UserScript==

// Targeting just active HITs would be @include https://worker.mturk.com/projects/*/tasks/*?assignment_id=*

(function() {

    let childIFrame = document.querySelector("iframe");
    let childWindow = childIFrame.contentWindow;

    let acceptHITButton = document.querySelector("span[data-react-class*='SubmitAcceptTaskFormButton'] button");

    let secondaryButton = document.querySelector("input[name='authenticity_token']~button.btn.btn-secondary");
    let returnHITButton;
    if(secondaryButton.innerText.trim() === "Return") {returnHITButton = secondaryButton;}

    let reactDetails = JSON.parse(document.querySelector(`div[data-react-class="require('reactComponents/common/ShowModal')['default']"]`).dataset.reactProps).modalOptions;

    window.addEventListener('message', function(event) {
        let receivedObject = event.data;

        if(receivedObject.hasOwnProperty("mTurkParentWindowQuery")) {

            let objectToReplyWith = {};

            if(receivedObject["mTurkParentWindowQuery"] === "accepted") {

                let responseString = "";

                if(returnHITButton) {responseString = "accepted";} else {responseString = "notAccepted";}

                objectToReplyWith["mTurkParentWindowResponse"] = responseString;
            }

            if(receivedObject["mTurkParentWindowQuery"] === "url") {

                objectToReplyWith["urlResponse"] = window.location.href;
            }

            if(receivedObject["mTurkParentWindowQuery"] === "hitDetails") {

                objectToReplyWith["hitDetails"] = JSON.stringify(reactDetails);
            }

            childWindow.postMessage(objectToReplyWith, "*");
        }

        if(receivedObject.hasOwnProperty("mTurkParentWindowAction")) {

            if(receivedObject["mTurkParentWindowAction"] === "accept" && acceptHITButton) {
                acceptHITButton.click();
            }

            if(receivedObject["mTurkParentWindowAction"] === "return" && returnHITButton) {
                returnHITButton.click();
            }

            if(receivedObject.mTurkParentWindowAction === "navigate") {
                window.location.href = receivedObject.url;
            }
        }
    });
})();