Created
March 29, 2017 16:41
-
-
Save Darker/51825bab274b482e6ede4f19336d5120 to your computer and use it in GitHub Desktop.
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
const RICKROLL = "https://www.youtube.com/watch?v=oHg5SJYRHA0"; | |
const RICKROLL_DAY = 1; | |
const RICKROLL_MONTH = 4; | |
if(Storage && window.localStorage && window.localStorage instanceof Storage) { | |
// Check if it's 1st of April | |
var date = new Date(); | |
// Note that months are zero indexed | |
if(date.getDate() == RICKROLL_DAY && date.getMonth() == RICKROLL_MONTH-1) { | |
// If local storage does not work, rickroll will simply never occur | |
// this is a safety measure to prevent people being rickrolled forever | |
localStorage.LOCAL_STORAGE_WORKS = true; | |
// This will be called as an on click handler for anchor elements | |
function rickroller(event) { | |
console.info("Rickrolling!"); | |
if(localStorage.RICKROLLED_AT) { | |
console.warn("Rickrolling canceled!"); | |
return true; | |
} | |
// Remember that user was rickrolled | |
localStorage.RICKROLLED_AT = new Date().getTime(); | |
event.preventDefault(); | |
// New tab links go to a new tab | |
if(this.target=="_blank") { | |
window.open(RICKROLL); | |
} | |
else | |
window.location.href = RICKROLL; | |
return false; | |
} | |
if(localStorage.LOCAL_STORAGE_WORKS && !localStorage.RICKROLLED_AT) { | |
var ar=[]; | |
console.info("Initiating rickroll!"); | |
// Get all links as an array | |
ar.push.apply(ar, document.querySelectorAll("a")); | |
ar.forEach((link)=>{ | |
// Skip links that do not lead outside | |
if(link.href.indexOf("http")!=0&&link.href.indexOf("//")!=0) { | |
console.log(link.href, link.href.indexOf("http"), link.href.indexOf("//")); | |
return; | |
} | |
link.addEventListener("click", rickroller); | |
// This was used to test if this code is executing, do not uncoment | |
//link.appendChild(new Text("VOLE")); | |
}); | |
} | |
else { | |
console.info("Already rickrolled!"); | |
} | |
} | |
else { | |
console.info("Not a good day for rickrolling.", date.getDate(),date.getMonth()); | |
} | |
} | |
else { | |
console.warn("Local storage not supported by the browser."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment