Last active
August 29, 2015 14:16
-
-
Save jehoshua02/e93d7e9361ce73536779 to your computer and use it in GitHub Desktop.
Random Hymn picker script for Tampermonkey or Greasemonkey browser extension. Works for Hymns and Children's Songbook.
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 randomHymn.js | |
// @namespace http://your.homepage/ | |
// @version 0.1 | |
// @description enter something useful | |
// @author You | |
// @match https://www.lds.org/music/library/hymns* | |
// @match https://www.lds.org/music/library/childrens-songbook* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
var randomInt = function (min, max) { | |
return Math.round((max - min) * Math.random() + min); | |
}; | |
var pickRandomHymn = function () { | |
var hymns = document.querySelectorAll('#playlist tbody tr'); | |
var min = 1; | |
var max = hymns.length - 1; | |
var hymn = hymns[randomInt(min, max)]; | |
return hymn; | |
}; | |
var scrollTo = function (element) { | |
var rect = element.getBoundingClientRect() | |
$('body').animate({ | |
scrollTop: rect.top + document.body.scrollTop - 40 | |
}); | |
}; | |
var highlight = function (element) { | |
element.style.backgroundColor = 'yellow'; | |
setTimeout(function () { | |
element.style.backgroundColor = null; | |
}, 5000); | |
}; | |
var styles = function (element, styles) { | |
for (var prop in styles) { | |
element.style[prop] = styles[prop]; | |
} | |
}; | |
// create container | |
var container = document.createElement('div'); | |
styles(container, { | |
position: 'fixed', | |
bottom: 0, | |
left: 0 | |
}); | |
// create button | |
var button = document.createElement('button'); | |
button.innerHTML = 'Random'; | |
button.onclick = function () { | |
var hymn = pickRandomHymn(); | |
scrollTo(hymn); | |
highlight(hymn); | |
}; | |
// add to document | |
container.appendChild(button); | |
document.body.appendChild(container); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment