Skip to content

Instantly share code, notes, and snippets.

@aglobus
Last active August 29, 2015 14:11
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 aglobus/47ded4e65db838a07b95 to your computer and use it in GitHub Desktop.
Save aglobus/47ded4e65db838a07b95 to your computer and use it in GitHub Desktop.
HN Comments
(function() {
var loaded = function(event) {
var response = JSON.parse(event.target.responseText);
var hits = response.hits;
if (hits.length === 0) {
alert('No results found on HN.');
return true;
}
document.location.href = 'https://news.ycombinator.com/item?id=' + hits[0].objectID;
};
var d = document;
var b = d.body;
var s = d.createElement('script');
var url = d.location.href;
var api = 'https://hn.algolia.com/api/v1/search_by_date?query=';
var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
var domain = matches && matches[1];
if (domain === 'hn.algolia.com')
return;
if (domain != 'news.ycombinator.com') {
var queryUrl = url.split('?')[0].split('#')[0];
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', loaded, false);
xhr.open('GET', api + encodeURIComponent(queryUrl) + '&restrictSearchableAttributes=url', true);
xhr.send();
} else {
var link = document.querySelector('.title a');
if (link) {
link = link.href;
document.location.href = 'https://hn.algolia.com/?q=' + link;
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment