forked from jerone/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithub_Reply_Comments.user.js
More file actions
156 lines (128 loc) · 5.9 KB
/
Copy pathGithub_Reply_Comments.user.js
File metadata and controls
156 lines (128 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// ==UserScript==
// @name Github Reply Comments
// @namespace https://github.com/jerone/UserScripts
// @description Easy reply to Github comments
// @author jerone
// @copyright 2016+, jerone (http://jeroenvanwarmerdam.nl)
// @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
// @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @homepage https://github.com/jerone/UserScripts/tree/master/Github_Reply_Comments
// @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Reply_Comments
// @downloadURL https://github.com/jerone/UserScripts/raw/master/Github_Reply_Comments/Github_Reply_Comments.user.js
// @updateURL https://github.com/jerone/UserScripts/raw/master/Github_Reply_Comments/Github_Reply_Comments.user.js
// @supportURL https://github.com/jerone/UserScripts/issues
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCYMHWQ7ZMBKW
// @version 1.0.2
// @icon https://github.githubassets.com/pinned-octocat.svg
// @grant none
// @include https://github.com/*
// @include https://gist.github.com/*
// @require https://unpkg.com/turndown@5.0.3/dist/turndown.js
// @require https://unpkg.com/turndown-plugin-gfm@1.0.2/dist/turndown-plugin-gfm.js
// @require https://unpkg.com/turndown-plugin-github-code-snippet@1.0.2/turndown-plugin-github-code-snippet.js
// ==/UserScript==
(function () {
String.format = function (string) {
var args = Array.prototype.slice.call(arguments, 1, arguments.length);
return string.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] !== "undefined" ? args[number] : match;
});
};
var turndownService = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced', hr: '***' });
turndownService.use(turndownPluginGfm.gfm);
turndownService.use(turndownPluginGithubCodeSnippet);
function getCommentTextarea(replyBtn) {
var newComment = replyBtn;
while (newComment && !newComment.classList.contains("js-quote-selection-container")) {
newComment = newComment.parentNode;
}
var inlineComment = newComment.querySelector(".js-inline-comment-form-container");
if (inlineComment) {
inlineComment.classList.add("open");
}
var textareas = newComment.querySelectorAll(":scope > :not(.last-review-thread) .comment-form-textarea");
return textareas[textareas.length - 1];
}
function getCommentMarkdown(comment) {
var commentText = "";
// Use raw comment when available.
var commentForm = comment.querySelector(".comment-form-textarea");
if (commentForm) {
commentText = commentForm.value;
}
// Convert comment HTML to markdown.
if (!commentText) {
// Clone it, so we can alter the HTML a bit, without modifing the page.
var commentBody = comment.querySelector(".comment-body").cloneNode(true);
// Remove 'Toggle code wrap' buttons from https://greasyfork.org/en/scripts/18789-github-toggle-code-wrap
Array.prototype.forEach.call(commentBody.querySelectorAll(".ghd-wrap-toggle"), function (ghd) {
ghd.remove();
});
// GitHub add an extra new line, which is converted by Turndown.
Array.prototype.forEach.call(commentBody.querySelectorAll("pre code"), function (pre) {
pre.innerHTML = pre.innerHTML.replace(/\n$/g, '');
});
commentText = turndownService.turndown(commentBody.innerHTML);
}
return commentText;
}
function addReplyButtons() {
Array.prototype.forEach.call(document.querySelectorAll(".comment, .review-comment"), function (comment) {
var oldReply = comment.querySelector(".GithubReplyComments, .GithubCommentEnhancerReply");
if (oldReply) {
oldReply.parentNode.removeChild(oldReply);
}
var header = comment.querySelector(":scope > :not(.minimized-comment) .timeline-comment-header"),
actions = comment.querySelector(":scope > :not(.minimized-comment) .timeline-comment-actions");
if (!header) {
header = actions;
}
if (!actions) {
if (!header) {
return;
}
actions = document.createElement("div");
actions.classList.add("timeline-comment-actions");
header.insertBefore(actions, header.firstElementChild);
}
var reply = document.createElement("button");
reply.setAttribute("type", "button");
reply.setAttribute("title", "Reply to this comment");
reply.setAttribute("aria-label", "Reply to this comment");
reply.classList.add("GithubReplyComments", "btn-link", "timeline-comment-action", "tooltipped", "tooltipped-ne");
reply.addEventListener("click", function (e) {
e.preventDefault();
var timestamp = comment.querySelector(".timestamp");
var commentText = getCommentMarkdown(comment);
commentText = commentText.trim().split("\n").map(function (line) {
return "> " + line;
}).join("\n");
var newComment = getCommentTextarea(this);
var text = newComment.value.length > 0 ? "\n" : "";
text += String.format('[**@{0}**]({1}/{0}) commented on [{2}]({3} "{4} - Replied by Github Reply Comments"):\n{5}\n\n',
comment.querySelector(".author").textContent,
location.origin,
timestamp.firstElementChild.getAttribute("title"),
timestamp.href,
timestamp.firstElementChild.getAttribute("datetime"),
commentText);
newComment.value += text;
newComment.setSelectionRange(newComment.value.length, newComment.value.length);
newComment.focus();
});
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.classList.add("octicon", "octicon-mail-reply");
svg.setAttribute("height", "16");
svg.setAttribute("width", "16");
reply.appendChild(svg);
var path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", "M6 2.5l-6 4.5 6 4.5v-3c1.73 0 5.14 0.95 6 4.38 0-4.55-3.06-7.05-6-7.38v-3z");
svg.appendChild(path);
actions.appendChild(reply);
});
}
// init;
addReplyButtons();
// on pjax;
document.addEventListener('pjax:end', addReplyButtons);
})();