Skip to content

Instantly share code, notes, and snippets.

@kennethrapp
Created December 5, 2012 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennethrapp/4220255 to your computer and use it in GitHub Desktop.
Save kennethrapp/4220255 to your computer and use it in GitHub Desktop.
hacker news random quote userscript
// ==UserScript==
// @name Hacker News Feel
// @namespace kennethrapp1@gmail.com
// @description motivation
// @include *news.ycombinator.com*
// @version 1
// ==/UserScript==
function main(){
var motivations = [
'You\'re fitter than a 5th grader. Keep it up!',
'Hey girl, Ryan Gosling is waiting for you at the finish line.',
'The voice inside your head that says you can\'t do this is a liar.',
'Little by little becomes A LOT.',
'You can throw in the towel, or use it to wipe the sweat off your face.',
'Should. Could. Would. DID.',
'Make your goal progression not perfection.',
'A one hour workout is just 4% of your day',
'The harder you work, the better you get.',
'All achievements require time.',
'I don\'t run, I RUN.',
'That which doesn’t kill us makes us (harder, better, faster) stronger. Some really smart German philosopher says so, and well Kanye.',
'Today is another chance. Make your momma proud.',
'The difference between try and triumph is a little umph.',
'If you\'re tired of starting over, stop giving up.',
'Good habits are as addictive as bad habits but, you know, much more rewarding.',
'The difference between ordinary and extraordinary is that little extra. My mom told me the other day that you\'ve got it.',
'Life begins at the end of your comfort zone.',
'Instead of of giving myself reasons why I can\'t, I give myself reasons why I can.',
'There are no benches in running.',
'Remember you’re doing this for you. And your future you. Robot you.',
'You don\'t have to be great to start, but you have to start to be great. You\'re like, so totally great ... we shouldn\'t even be having this discussion.',
'You can do this. You can do this. You can do this.',
'One push-up is more than no push-ups.',
'A smile is a curve that sets everything straight. Curvy is so in this month.',
'Keep going.',
'Laugh in the face of quitting. Heehee.',
'Go on with your bad-ass self.',
'You are sooo good at sports.',
'Nice arms, you selling two tickets to the gun show?',
'Are you from the moon? Cause your physique is out of this world.',
'You are a ridiculously, ridiculously nice person.',
'The best way to make your dreams come true is to wake up. Good morning, sunshine!'
];
var HN_filter = {
nodes: {},
html: {},
target: null,
controls: [],
// make an html link with a callback attached onclick.
link_control: function(linktext, callback, args){
link = document.createElement('a');
link.href = '#';
link.appendChild(document.createTextNode(linktext));
link.addEventListener('click', function(event){
event.preventDefault();
callback.apply(this, args);
}, true);
//this.controls.push(link);
return link;
},
// random thing from an array
array_random: function(array){
return array[Math.floor(Math.random()*array.length)];
},
//http://stackoverflow.com/questions/4652734/return-html-from-a-user-selection/4652824#4652824
get_selection: function(){
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
// attempt to clean out the html....
var tmp = document.createElement('div');
tmp.innerHTML = html;
text = tmp.textContent;
text = text.replace("%20", " ");
return text;
},
// adds a set of xpath references to the nodes object.
define_nodes: function(nodes_obj){
for(node in nodes_obj){
this.nodes[node] = nodes_obj[node];
}
return this;
},
// get the node matching the xpath reference at an index
get_node: function(index){
var results = document.evaluate(this.nodes[index], document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var element = results.snapshotItem(index);
return element || null;
},
// insert element(s) before the target node (needs to invert the args array)
before: function(){
var ref = this.target;
for (var i = 0; i < arguments.length; i++) {
this.target.parentNode.insertBefore(arguments[i], ref);
}
return this;
},
// insert element(s) after the target node
after: function(){
var ref = this.target;
for (var i = 0; i < arguments.length; i++) {
ref.parentNode.insertBefore(arguments[i], ref.nextSibling);
}
return this;
},
// append element(s) into the target node
into: function(){
var ref = this.target;
for (var i = 0; i < arguments.length; i++) {
ref.appendChild(arguments[i]);
}
return this;
},
// set a target node in the nodes object to use as a reference point for adding stuff
with_node: function(node){
this.target = this.get_node(node);
console.log(this.target);
return this;
}
}
// build new table rows and set ids.
baserow = document.createElement('tr');
basecell = document.createElement('td');
basecell.setAttribute('id','HN_Filter_base');
baserow.appendChild(basecell);
uirow = document.createElement('tr');
uicell = document.createElement('td');
uicell.setAttribute('id','HN_Filter_controls');
uirow.appendChild(uicell);
// get a random quote.
var random_quote = HN_filter.array_random(motivations);
random_quote = document.createTextNode(random_quote);
// this is a useless control that alerts whatever text is selected by the mouse.
var useless_control = HN_filter.link_control("this does nothing useful", function(){
var sel = HN_filter.get_selection();
alert(sel);
});
/* likely overoptimized:
-defined reference points
-distribute html
-add random quote and control.*/
HN_filter.define_nodes({
'hook': '//tr[@style="height:10px"]',
'script_base': '//td[@id="HN_Filter_base"]',
'script_controls': '//td[@id="HN_Filter_controls"]'
}).with_node('hook')
.after(uirow, baserow)
.with_node('script_base')
.into(random_quote)
.with_node('script_controls')
.into(useless_control);
}
//http://erikvold.com/blog/index.cfm/2010/6/14/using-jquery-with-a-user-script
function runner(callback){
var dochead = document.getElementsByTagName('body')[0];
var script = document.createElement("script");
script.setAttribute('type','text/javascript');
script.textContent = "(" + callback.toString() + ")();";
window.addEventListener('load', function() {
dochead.insertBefore(script, dochead.childNodes[0]);
}, false);
}
runner(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment