Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
1.01
- pagesize=100
- Fixed incorrect behavior when same question appeared in both "linked"
and "related" lists.
- Don't attempt query if no links found (e.g. on /questions/linked/*).
- Low quota warning (< 100) in console.

1.00
- Initial revision
  • Loading branch information
JC3 committed May 30, 2017
1 parent a7bb7c0 commit 08bdd3a
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions SidebarAnswerStatus.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Sidebar Answer Status
// @namespace https://stackexchange.com/users/305991/jason-c
// @version 1.0
// @version 1.01
// @description Show answer status of questions in sidebar.
// @author Jason C
// @match http*://*.stackexchange.com/questions/*
Expand All @@ -24,26 +24,34 @@
.then(getAnswerStatus)
.then(showIfAnswered);

/* Get sidebar question data. Returns a promise. Data is an object whose
* keys are question IDs and whose values are:
/* Get sidebar question data. Returns a promise. Data is:
*
* { questions: A set of empty objects keyed by ID,
* items: An array of items }
*
* Each item is:
*
* { id: question id,
* votes: the score dom element,
* link: the question link dom element }
*/
function getSidebarQuestions () {

var qs = {};
var qs = {
questions: {},
items: []
};

$('div.linked div.answer-votes, div.related div.answer-votes').each(function (_, obj) {
var url = $(obj).parent().attr('href');
var qid = /\/q\/([0-9]*)/.exec(url);
if (qid) {
qs[qid[1]] = {
qs.questions[qid[1]] = {};
qs.items.push({
id: qid[1],
votes: $(obj),
link: $(obj).parent()
};
});
}
});

Expand All @@ -52,22 +60,28 @@
}

/* Fill in sidebar question data with answer status from the API. Returns
* a promise. Each element will have an additional 'status' property whose
* value is the corresponding item from the SE API, containing at least:
* a promise. Each element in 'questions' will have an additional 'status'
* property whose value is the corresponding item from the SE API, containing
* at least:
*
* { is_answered: boolean,
* answer_count: number of answers,
* question_id: id of the question }
*/
function getAnswerStatus (qs) {

if (qs.items.length === 0)
return $.when(qs);

var site = document.location.host;
var ids = Object.keys(qs).join(';');
var url = `//api.stackexchange.com/2.2/questions/${ids}?order=desc&sort=activity&site=${site}&filter=!4(YqyYcHA.0whnoIN`;
var ids = Object.keys(qs.questions).join(';');
var url = `//api.stackexchange.com/2.2/questions/${ids}?pagesize=100&order=desc&sort=activity&site=${site}&filter=!4(YqyYcHA.0whnoIN`;

return $.getJSON(url).then(function (r) {
if (r.quota_remaining < 100)
console.log(`Sidebar Answer Status: API query getting low (${r.quota_remaining})`);
for (var item of r.items)
qs[item.question_id].status = item;
qs.questions[item.question_id].status = item;
return qs;
});

Expand All @@ -77,12 +91,13 @@
*/
function showIfAnswered (qs) {

for (var q of Object.values(qs)) {
if (q.status.is_answered) { // change to q.status.answer_count > 0 if you'd prefer.
for (var q of Object.values(qs.items)) {
var status = qs.questions[q.id].status;
if (status.is_answered) { // change to q.status.answer_count > 0 if you'd prefer.
q.votes.css('border', `1px solid ${q.votes.css('color')}` /*'1px solid black'*/);
q.link.attr('title', `Answered (${q.status.answer_count})`);
q.link.attr('title', `Answered (${status.answer_count})`);
} else {
q.link.attr('title', `Unanswered (${q.status.answer_count})`);
q.link.attr('title', `Unanswered (${status.answer_count})`);
}
}

Expand Down

0 comments on commit 08bdd3a

Please sign in to comment.