Skip to content

Instantly share code, notes, and snippets.

@RStrydom
Created June 4, 2014 11:44
Show Gist options
  • Save RStrydom/0f4cf8aba7a589664715 to your computer and use it in GitHub Desktop.
Save RStrydom/0f4cf8aba7a589664715 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Resource Guru Task Complete
// @namespace resource_guru_task_complete
// @description Provides the ability to check off completed tasks
// @include https://firstview1.resourceguruapp.com/*
// @version 1
// @grant none
// ==/UserScript==
function addCss(cssString) {
var head = document.getElementsByTagName('head')[0];
// return unless (head);
var newCss = document.createElement('style');
newCss.type = "text/css";
newCss.innerHTML = cssString;
head.appendChild(newCss);
}
/**
* Checks a given class attribute for the presence of a given class
*/
var checkForClass = function(element, nameOfClass) {
if (typeof element == 'string') { element = document.getElementById(element); }
if (element.className == '') {
return false;
} else {
return new RegExp('\\b' + nameOfClass + '\\b').test(element.className);
}
}
/**
* Adds a class to an element's class attribute
*/
var addClass = function(element, nameOfClass) {
if (typeof element == 'string') { element = document.getElementById(element); }
if (!checkForClass(element, nameOfClass)) {
element.className += (element.className ? ' ' : '') + nameOfClass;
return true;
} else {
return false;
}
}
/**
* Removes a class from an element's class attribute
*/
var removeClass = function(element, nameOfClass) {
if (typeof element == 'string') { element = document.getElementById(element); }
if (checkForClass(element, nameOfClass)) {
element.className = element.className.replace(
(element.className.indexOf(' ' + nameOfClass) >= 0 ? ' ' + nameOfClass : nameOfClass),
'');
return true;
} else {
return false;
}
}
/**
* Replaces a class with another if the class is present
*/
var replaceClass = function(element, oldClass, newClass) {
if (typeof element == 'string') { element = document.getElementById(element); }
if (checkForClass(element, oldClass)) {
removeClass(element, oldClass);
addClass(element, newClass);
return true;
} else {
return false;
}
}
/**
* Toggles the specified class on and off
*/
var toggleClass = function(element, nameOfClass) {
if (typeof element == 'string') { element = document.getElementById(element); }
if (checkForClass(element, nameOfClass)) {
removeClass(element, nameOfClass);
} else {
addClass(element, nameOfClass);
}
return true;
}
addCss ('.markComplete { left: 38px; position: relative; top: 27px; }');
addCss ('tr.involves_user_resource:hover td { background-color: inherit !important; }');
addCss ('tr.involves_user_resource.complete { background-color: #B4D12E; !important}');
addCss ('tr.involves_user_resource.complete td { background-color: #B4D12E; !important}');
addCss ('tr.involves_user_resource.complete:hover td { background-color: #B4D12E; !important}');
var tasklist = document.getElementById('day_view');
var checkboxNum;
var tasks = tasklist.getElementsByClassName('involves_user_resource');
var check=document.createElement("input");
check.type="checkbox";
check.onclick = false;
check.className = "markComplete";
for (var i = 0; i < tasks.length; i++) {
checkbox = check.cloneNode();
checkbox.value = "completed_" + i;
tasks[i].appendChild(checkbox);
checkbox.onclick = MarkComplete;
}
function MarkComplete() {
checkboxNum = this.value.slice(10);
if (this.checked) {
toggleClass(tasks[checkboxNum], 'complete');
}
else {
toggleClass(tasks[checkboxNum], 'complete');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment