Skip to content

Instantly share code, notes, and snippets.

@cuylerstuwe
Last active April 5, 2018 22:44
Show Gist options
  • Save cuylerstuwe/6f50c37f7309fe1c7632376c4e2f3a1c to your computer and use it in GitHub Desktop.
Save cuylerstuwe/6f50c37f7309fe1c7632376c4e2f3a1c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name MAction Library
// @namespace salembeats
// @version 1
// @description Fast, lightweight, exception-protected library for common actions in mTurk userscripts.
// @author Cuyler Stuwe (salembeats)
// ==/UserScript==
function MAction() {
}
MAction.prototype.click = function(element) {
if(!element) {return undefined;}
element.click();
};
MAction.prototype.hide = function(element) {
if(!element) {return undefined;}
element.style.display = "none";
};
MAction.prototype.show = function(element) {
if(!element) {return undefined;}
element.style.display = "block";
};
MAction.prototype.remove = function(element) {
if(!element) {return undefined;}
element.remove();
};
MAction.prototype.text = function(element, options = {}) {
if(!element) {return undefined;}
const shouldUseAltTextMethod = ( options.altTextMethod || options.atm || options.altParseMethod || options.apm );
const textMethod = ( shouldUseAltTextMethod ? "textContent" : "innerText" );
const shouldSetText = ( options.text !== undefined || typeof options === "string");
const shouldUseLowercase = ( options.lowercase || options.lower || options.l );
if(shouldSetText) {
const textToSet = ( typeof options === "string" ? options : options.text );
element[textMethod] = ( shouldUseLowercase ? (textToSet || "").toLowerCase() : textToSet );
return element;
}
else {
if(element[textMethod] === undefined) {return undefined;}
else {
return ( shouldUseLowerCase ? element[textMethod].toLowerCase() : element[textMethod] );
}
}
};
const MA = new MAction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment