Last active
May 6, 2018 17:56
-
-
Save cuylerstuwe/2661fe710d1520a50cb0f8fa8b23d052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name MUtilities Library | |
// @namespace salembeats | |
// @version 1.8 | |
// @description Simple utilities for common tasks when writing mTurk HIT userscripts. | |
// @author Cuyler Stuwe (salembeats) | |
// @grant none | |
// ==/UserScript== | |
const MUTIL_DEFAULT_SELECTOR_POLL_RATE_MS = 125; | |
function MUtil() { | |
} | |
MUtil.prototype.$wait = async function(selector, pollRate = MUTIL_DEFAULT_SELECTOR_POLL_RATE_MS) { | |
return new Promise((resolve, reject) => { | |
let element = document.querySelector(selector); | |
if(element) { resolve(element); } | |
else { | |
let intervalHandle = setInterval(() => { | |
element = document.querySelector(selector); | |
if(element) { | |
clearInterval(intervalHandle); | |
resolve(element); | |
} | |
}, pollRate); | |
} | |
}); | |
}; | |
MUtil.prototype.isMturkHit = function (optionalGid) { | |
if(window === window.top) {return false;} | |
if(!document.referrer.includes("worker.mturk.com/projects/")) {return false;} | |
if( (optionalGid !== undefined) && (!document.referrer.includes(optionalGid)) ) {return false;} | |
return true; | |
}; | |
MUtil.prototype.isHitAccepted = function() { | |
const assignmentIdElement = document.getElementById("assignmentId"); | |
if(!assignmentIdElement) {return false;} | |
if(!assignmentIdElement.value) {return false;} | |
if(assignmentIdElement.value.length !== 30) {return false;} | |
if(assignmentIdElement.value[0] !== '3') {return false;} | |
return true; | |
}; | |
MUtil.prototype.debug = function(message, style) { | |
if(!(this._isDebugEnabled)) {return;} | |
const timestampedMessage = `[${new Date().toLocaleString("en-US")}]: ${message}`; | |
if(style !== undefined) { | |
console.log(`%c${timestampedMessage}`, style); | |
} | |
else { | |
console.log(timestampedMessage); | |
} | |
}; | |
MUtil.prototype.success = function(message) { | |
this.debug(message, 'color: white; background-color: green;'); | |
}; | |
MUtil.prototype.failure = function(message) { | |
this.debug(message, 'color: white; background-color: red;'); | |
}; | |
MUtil.prototype.warning = function(message) { | |
this.debug(message, 'color: white; background-color: orange;'); | |
}; | |
MUtil.prototype.setDebugLevel = function(shouldEnableDebug) { | |
this._isDebugEnabled = shouldEnableDebug; | |
}; | |
const MU = new MUtil(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment