Skip to content

Instantly share code, notes, and snippets.

@rschrieken rschrieken/advancedseartch.html Secret
Last active Jul 22, 2016

Embed
What would you like to do?
<html>
<title>Advanced seacrch</title>
<script type="text/javascript">
var rows,
search,
feedback,
tag,
score,
comments,
answers,
tot,
processed;
function buildRow(item) {
var row = document.createElement('div'),
a = document.createElement('a');
a.href = item.link;
a.textContent = item.title;
row.appendChild(a);
rows.appendChild(row);
}
function buildApiUrl(urlParts, queryString) {
var url = '',
qp,
queryArr = [];
url = urlParts.join('/');
if (queryString !== undefined) {
for(qp in queryString) {
queryArr.push(qp + '=' + queryString[qp]);
}
url = url + '?' + queryArr.join('&');
}
return url;
}
function handleApiResponse(response, page) {
var i,
resp = JSON.parse(response),
items = resp.items;
if (page === undefined) {
while (rows.firstChild) {
rows.removeChild(rows.firstChild);
}
page = 1;
tot = 0;
processed = 0;
search.disabled = true;
}
if (items.length > 0) {
for(i = 0; i < items.length; i = i + 1) {
if (items[i].comment_count === parseInt(comments.value, 10) &&
items[i].answer_count === parseInt(answers.value, 10) &&
items[i].score === parseInt(score.value, 10)
) {
tot = tot + 1;
buildRow(items[i]);
}
}
processed = processed + items.length;
feedback.textContent = '# results: ' + processed + ' # filtered: ' + tot;
} else {
if (page === 1) {
feedback.textContent = 'no questions found';
}
}
if (resp.has_more) {
if (resp.backoff) {
setTimeout(function() { sendRequest(page + 1); }, resp.backoff * 1000);
} else {
sendRequest(page + 1);
}
} else {
search.disabled = false;
}
}
function sendRequest(page) {
var xhr = new XMLHttpRequest(),
api = [ 'https://api.stackexchange.com',
'2.2',
'search',
'advanced'],
qs = {
'order':'desc',
'sort':'activity',
'pagesize': 100,
'answers': parseInt(answers.value, 10),
'tagged': tag.value,
'site': 'stackoverflow',
'filter': '!9YdnSHlY(',
'key': 'I4Ll0m749uHMyBd9S74DkA(('
};
if (page !== undefined) {
qs.page = page;
}
xhr.addEventListener('load', function () {
handleApiResponse(xhr.responseText, page);
});
xhr.open('GET', buildApiUrl(api, qs));
xhr.send();
}
window.onload = function () {
rows = document.getElementById('rows'),
search = document.getElementById('search'),
feedback = document.getElementById('feedback'),
tag = document.getElementById('tag'),
score = document.getElementById('score'),
comments = document.getElementById('comments'),
answers = document.getElementById('answers'),
search.addEventListener('click', function() { sendRequest(); });
};
</script>
<style>
<!-- language: lang-css -->
label {
width: 100px;
display:inline-block;
}
div {
margin: 10px;
}
div.results
{
margin: 5px;
}
div.results div
{
margin: 4px;
}
</style>
</head>
<body>
<div>
<div>
<label for="tag">Tag</label><input id="tag" type="text" value="haskell" />
</div>
<div>
<label for="score">Score</label><input id="score" type="text" value="0"/>
</div>
<div>
<label for="answers">Answers</label><input id="answers" type="text" value="0"/>
</div>
<div>
<label for="comments">Comments</label><input id="comments" type="text" value="0"/>
</div>
<div>
<button id="search">Search</button>
</div>
</div>
<div id="result" class="results">
<div id="feedback">No results</div>
<div id="rows">
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.