-
-
Save SamHasler/2909187 to your computer and use it in GitHub Desktop.
Betteridge's Law for Hacker News
This file contains hidden or 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 BetteridgeLinks | |
// @description Lowlight headlines on Hacker News that conform to Betteridge's Law | |
// @version 0.3 | |
// @match http://news.ycombinator.com/* | |
// @author noibl <[email protected]> | |
// ==/UserScript== | |
var questions = ['Is', 'Are', 'Does', 'Do', 'Has', 'Have', 'Did', 'Will', 'Can', 'Could', 'Should']; | |
var selector = '.title a'; | |
var label = 'Probably not'; | |
var className = 'hn-betteridge'; | |
var cssRules = 'a.hn-betteridge:link { color: #828282; } ' | |
+ 'a.hn-betteridge:after {' | |
+ 'color: #600; font-size: 50%; ' | |
+ 'text-transform: uppercase; ' | |
+ 'letter-spacing: 1px; ' | |
+ 'background-color: #fff; ' | |
+ 'content: " ' + label + '" ' | |
+ '}'; | |
var pages = [ | |
'^/$', | |
'^/news$', | |
'^/newest$', | |
'^/x$' | |
]; | |
init(); | |
function init() { | |
var re; | |
for (var i = 0; i < pages.length; i++) { | |
re = new RegExp(pages[i]); | |
if (re.test(window.location.pathname)) { | |
run(); | |
break; | |
} | |
} | |
} | |
function run() { | |
var head = document.getElementsByTagName('head')[0], | |
style = document.createElement('style'), | |
rules = document.createTextNode(cssRules); | |
style.type = 'text/css'; | |
if (style.styleSheet) { style.styleSheet.cssText = rules.nodeValue; } | |
else { style.appendChild(rules); } | |
head.appendChild(style); | |
testLinks(); | |
} | |
function testLinks() { | |
var links = document.querySelectorAll(selector); | |
for (var i = 0; i < links.length; i++) { | |
if (isBetteridge(links[i].innerHTML)) { | |
links[i].className += ' ' + className; | |
} | |
} | |
} | |
function isBetteridge(text) { | |
var re = new RegExp('^(' + questions.join('|') + ').*\\?$'); | |
return re.test(text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment