Skip to content

Instantly share code, notes, and snippets.

@cuylerstuwe
Last active April 5, 2018 23:05
Show Gist options
  • Select an option

  • Save cuylerstuwe/3dd0c6c765a553c1184bc1d6d075bb9a to your computer and use it in GitHub Desktop.

Select an option

Save cuylerstuwe/3dd0c6c765a553c1184bc1d6d075bb9a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name MSelect Library
// @namespace salembeats
// @version 1.1
// @description Fast, lightweight selector library for MTurk userscripts.
// @author Cuyler Stuwe (salembeats)
// ==/UserScript==
function MSelect() {
this.lastClickedElement = null;
document.body.addEventListener("click", e => {
this.lastClickedElement = e.target;
});
}
MSelect.prototype.$ = function(selector) {
return document.querySelector(selector);
};
MSelect.prototype.$$ = function(selector) {
return document.querySelectorAll(selector);
};
MSelect.prototype.$$a = function(selector) {
return Array.from(this.$$(selector));
};
MSelect.prototype.$$$ = function() {
return this.$$("*");
};
MSelect.prototype.$text = function(selector, text, options = {}) {
const elementsFoundBySelector = this.$$(selector);
if(elementsFoundBySelector.length === 0) {return undefined;}
const textCheckMethod = ( ( options.altParseMethod || options.apm ) ? "textContent" : "innerText" );
const textToCheck = ( ( options.caseInsensitive || options.ci ) ? (text || "").toLowerCase() : text );
for(const el of elementsFoundBySelector) {
const elementText = el[textCheckMethod];
const elementTextToCompare = ( (options.caseInsensitive || options.ci) ? (elementText || "").toLowerCase() : elementText );
if( ( options.strict && (elementTextToCompare || "").trim() === (textToCheck || "").trim() ) || (!options.strict && (elementTextToCompare || "").includes(textToCheck)) ) {return el;}
}
return undefined;
};
MSelect.prototype.$$aText = function(selector, text, options = {}) {
const elementsToReturn = [];
const elementsFoundBySelector = this.$$(selector);
if(elementsFoundBySelector.length === 0) {return elementsToReturn;}
const textCheckMethod = ( ( options.altParseMethod || options.apm ) ? "textContent" : "innerText" );
const textToCheck = ( (options.caseInsensitive || options.ci) ? (text || "").toLowerCase() : text );
for(const el of elementsFoundBySelector) {
const elementText = el[textCheckMethod];
const elementTextToCompare = ( (options.caseInsensitive || options.ci) ? (elementText || "").toLowerCase() : elementText );
if( ( options.strict && (elementTextToCompare || "").trim() == (textToCheck || "").trim() ) || (!options.strict && (elementTextToCompare || "").includes(textToCheck)) ) {elementsToReturn.push(el);}
}
return elementsToReturn;
};
MSelect.prototype.$rads = function() {
return this.$$("input[type='radio']");
};
MSelect.prototype.$sub = function() {
return this.$("input[type='submit']") || document.getElementById("submit");
};
MSelect.prototype.$0 = function() {
return this.lastClickedElement;
};
const M$ = new MSelect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment