Skip to content

Commit

Permalink
Close #17 PR: Implement like / dislike count. Fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceK33z authored and sindresorhus committed Feb 23, 2016
1 parent 277cc0f commit be43ef0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ const isRepo = /^\/[^/]+\/[^/]+/.test(path);
const ownerName = path.split('/')[1];
const repoName = path.split('/')[2];
const isPR = () => /^\/[^/]+\/[^/]+\/pull\/\d+$/.test(location.pathname);
const isIssue = () => /^\/[^/]+\/[^/]+\/issues\/\d+$/.test(location.pathname);
const isReleases = () => isRepo && /^\/[^/]+\/[^/]+\/(releases|tags)/.test(location.pathname);
const getUsername = () => $('meta[name="user-login"]').attr('content');
const uselessContent = {
upvote: {text: ['+1\n'], emoji: [':+1:']},
downvote: {text: ['-1\n'], emoji: [':-1:']}
};

function linkifyBranchRefs() {
$('.commit-ref').each((i, el) => {
Expand All @@ -27,6 +32,62 @@ function linkifyBranchRefs() {
});
}

function commentIsUseless(type, el) {
if (uselessContent[type].text.includes(el.innerText)) {
return true;
}
// check if there is exactly one child element, that has one or two child nodes;
// sometimes a second child node can contain a useless space
// using `childNodes` because this also includes text nodes
if (el.children.length === 1) {
const children = el.children[0].childNodes;
if (children.length === 1 || (children.length === 2 && !children[1].textContent.trim())) {
const onlyChild = children[0];
if (onlyChild.tagName === 'IMG' && uselessContent[type].emoji.includes(onlyChild.title)) {
return true;
}
}
}
}

function renderVoteCount(type, count) {
let iconUrl;
if (type === 'upvote') {
iconUrl = 'https://assets-cdn.github.com/images/icons/emoji/unicode/1f44d.png';
}
if (type === 'downvote') {
iconUrl = 'https://assets-cdn.github.com/images/icons/emoji/unicode/1f44e.png';
}
const $sidebar = $('#partial-discussion-sidebar');
$sidebar.append(`<div class="discussion-sidebar-item">
<h3 class="discussion-sidebar-heading">
${count} <img class="emoji" alt="${type}" height="20" width="20" align="absmiddle" src="${iconUrl}">
</h3>
</div>`);
}

function moveVotes() {
let upCount = 0;
let downCount = 0;
$('.js-comment-body').each((i, el) => {
const isUp = commentIsUseless('upvote', el);
const isDown = commentIsUseless('downvote', el);

if (isUp || isDown) {
el.closest('.js-comment-container').remove();

upCount += isUp ? 1 : 0;
downCount += isDown ? 1 : 0;
}
});
if (upCount > 0) {
renderVoteCount('upvote', upCount);
}
if (downCount > 0) {
renderVoteCount('downvote', downCount);
}
}

function addReleasesTab() {
const $repoNav = $('.js-repo-nav');
let $releasesTab = $repoNav.children('[data-selected-links~="repo_releases"]');
Expand Down Expand Up @@ -102,6 +163,9 @@ document.addEventListener('DOMContentLoaded', () => {
if (isPR()) {
linkifyBranchRefs();
}
if (isPR() || isIssue()) {
moveVotes();
}
});
}
});
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ I use GitHub a lot and notice many dumb annoyances I want to fix. So here be dra

- [Linkifies branch references in pull requests](https://github.com/sindresorhus/refined-github/issues/1)
- [Adds a 'Releases' tab to repos](https://cloud.githubusercontent.com/assets/170270/13136797/16d3f0ea-d64f-11e5-8a45-d771c903038f.png) *(<kbd>g</kbd> <kbd>r</kbd> hotkey)*
- [Hides :+1: :-1: comments and shows their count in the sidebar](https://cloud.githubusercontent.com/assets/170270/13241396/0b708ae8-da1d-11e5-8c01-94eae501034c.png)
- Automagically expands the news feed when you scroll down
- Hides other users starring/forking your repos from the newsfeed
- Removes tooltips
Expand Down

0 comments on commit be43ef0

Please sign in to comment.