Skip to content

Instantly share code, notes, and snippets.

@headquarters
headquarters / array-indexing
Last active December 10, 2015 07:48
A reminder to myself of how to handle array indexing in a "round robin" fashion, using the modulus operator.
//assume variables currentIndex, nextIndex, arrayLength exist
//moving "forward", positive incrementing
nextIndex = (currentIndex + 1) % arrayLength;
//moving "backward", negative incrementing
if(currentIndex == 0){
nextIndex = arrayLength - 1;
} else {
nextIndex = (currentIndex - 1) % arrayLength;
@headquarters
headquarters / translate-slashdot-rot13
Created April 1, 2013 15:52
Happy April Fool's Day! If you'd like to translate all the ROT13 encrypted articles on Slashdot's homepage today, just run this gist through your browser's console.
var alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var rot13 = function(letter){
var l = letter.toLowerCase();
var index = alphabet.indexOf(l);
var newIndex = (index + 13) % alphabet.length;
return alphabet[newIndex];
@headquarters
headquarters / find-oversized-element.js
Last active December 28, 2018 13:58
Find the offending element causing a horizontal scrollbar at any screen width (requires jQuery).
var screenWidth = window.innerWidth;
var visibleElements = jQuery(":visible");
visibleElements.each(function(){
var $this = jQuery(this);
if($this.width() > screenWidth){
$this.css("border", "1px solid green");
console.log("Screen width is " + screenWidth + " and the following element is " + $this.width(), $this);
}
});
@headquarters
headquarters / log.js
Created July 18, 2013 14:03
Log JavaScript errors in a server-side log file
/**
* Logs JavaScript errors via an AJAX request.
* Written in pure JavaScript so it does not have any dependencies.
*/
window.onerror = function(errorMessage, url, lineNumber){
var ajaxUrl = "/log-js-errors?t=" + (new Date()).getTime();
var data = {
errorMessage: errorMessage,
@headquarters
headquarters / fix-journal-url
Created November 4, 2013 21:51
Zotero does not save exported citations with the appropriate URL, so this appends ".libproxy.lib.unc.edu" to the host name to fix that.
javascript:(function(){window.location=window.location.href.replace(window.location.host,(window.location.host+'.libproxy.lib.unc.edu'));})()
function checkCompatibilityMode(){
var userAgentRegex = /MSIE ([0-9.]+)/;
var userAgentData = userAgentRegex.exec(navigator.userAgent);
if(userAgentData != null && typeof userAgentData[1] == "string"){
//userAgentData[1] will hold the version number, e.g. "10.0"
if(parseInt(userAgentData[1], 10) != document.documentMode){
$('body').append('<div class="warning">Your browser appears to be in Compatibility Mode. This may cause problems when printing. <a href="http://www.howtogeek.com/128289/how-to-disable-compatibility-mode-in-internet-explorer/" target="_blank">Read how to turn off Compatibility Mode</a>.</div>');
}
@headquarters
headquarters / make-google-form-printable.js
Last active December 28, 2015 17:49
Cleans up Google Form "Live View" for printing.
(function(){
//remove the "Edit" button
var editLink = document.getElementsByClassName('ss-edit-link');
if(editLink.length){
editLink[0].remove();
}
//remove the "Google" copyright footer
var copyright = document.getElementsByClassName('ss-footer');
if(copyright.length){
@headquarters
headquarters / add-download-links-to-sakai-videos.js
Last active January 4, 2016 10:49
This bookmarklet adds a download link for videos in Sakai so you can quickly download the files for playback in an external video player. Finding iframes by title isn't ideal, but it works for both places Announcements appear in Sakai. Also, the space after the title for each iframe is required.
@headquarters
headquarters / php-relative-date-class.php
Last active August 29, 2015 13:56
A PHP class to turn a timestamp into more user-friendly relative date.
/**
* A class for turning timestamps into more user-friendly relative dates.
*
$date = new Relative_Date();
echo $date->relative_formatted_date; //"Today"
*/
class Relative_Date {
const DAY = 86400;
@headquarters
headquarters / prevent_widows.rb
Last active September 6, 2015 06:34
Prevent widows in Ruby with this simple function. It replaces only the last space character with a non-breaking space in a string.
def prevent_widows(text)
if text.kind_of? String
trimmed_text = text.rstrip
last_space_index = trimmed_text.rindex(" ")
if !last_space_index.nil?
text[last_space_index] = "&nbsp;"
end
end