88// @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Comment_Enhancer
99// @downloadURL https://github.com/jerone/UserScripts/raw/master/Github_Comment_Enhancer/Github_Comment_Enhancer.user.js
1010// @updateURL https://github.com/jerone/UserScripts/raw/master/Github_Comment_Enhancer/Github_Comment_Enhancer.user.js
11- // @version 1.2
11+ // @version 1.3
1212// @grant none
1313// @run -at document-end
1414// @include https://github.com/*/*/issues/*
1515// @include https://github.com/*/*/pull/*
1616// @include https://github.com/*/*/commit/*
17+ // @include https://github.com/*/*/wiki/*
1718// ==/UserScript==
19+ /* global unsafeWindow */
1820
1921( function ( ) {
2022
129131 forceNewline : true
130132 } ,
131133 "function-table" : {
132- append : '\n\
133- | Header | Header | Header |\n\
134- | :--- | :---: | ---: |\n\
135- | Cell | Cell | Cell |\n\
136- | Cell | Cell | Cell |\n\
137- ' ,
134+ append : "\n" +
135+ "| Header | Header | Header |\n" +
136+ "| :--- | :---: | ---: |\n" +
137+ "| Cell | Cell | Cell |\n" +
138+ "| Cell | Cell | Cell |\n" ,
138139 forceNewline : true
139140 }
140141 } ;
141142
142- Array . forEach ( document . querySelectorAll ( ".comment-form-textarea" ) , function ( commentForm ) {
143- var gollumEditor = document . createElement ( "div" ) ;
144- gollumEditor . innerHTML =
145- '<div class="active" id="gollum-editor-function-bar" style="border:0 none;">' +
146- ' <div id="gollum-editor-function-buttons">' +
147- ' <div class="button-group">' +
148- ' <a href="#" id="function-bold" class="minibutton function-button" title="Bold" tabindex="-1">' +
149- ' <b>B</b>' +
150- ' </a>' +
151- ' <a href="#" id="function-italic" class="minibutton function-button" title="Italic" tabindex="-1">' +
152- ' <em>i</em>' +
153- ' </a>' +
154- ' <a href="#" id="function-strikethrough" class="minibutton function-button" title="Strikethrough" tabindex="-1">' +
155- ' <s>S</s>' +
156- ' </a>' +
157- ' </div>' +
158-
159- ' <div class="button-group">' +
160- ' <div class="select-menu js-menu-container js-select-menu js-composer-assignee-picker">' +
161- ' <span aria-haspopup="true" title="Headers" role="button" class="minibutton select-menu-button icon-only js-menu-target" style="padding:0 18px 0 7px; width:auto; border-bottom-right-radius:3px; border-top-right-radius:3px;">' +
162- ' <b>h#</b>' +
163- ' </span>' +
164- ' <div aria-hidden="false" class="select-menu-modal-holder js-menu-content js-navigation-container js-active-navigation-container" style="top: 26px;">' +
165- ' <div class="select-menu-modal" style="width:auto;">' +
166- ' <div class="select-menu-header">' +
167- ' <span class="select-menu-title">Choose header</span>' +
168- ' <span class="octicon octicon-remove-close js-menu-close"></span>' +
169- ' </div>' +
170- ' <div class="button-group">' +
171- ' <a href="#" id="function-h1" class="minibutton function-button js-menu-close" title="Header 1" tabindex="-1">' +
172- ' <b>h1</b>' +
173- ' </a>' +
174- ' <a href="#" id="function-h2" class="minibutton function-button js-menu-close" title="Header 2" tabindex="-1">' +
175- ' <b>h2</b>' +
176- ' </a>' +
177- ' <a href="#" id="function-h3" class="minibutton function-button js-menu-close" title="Header 3" tabindex="-1">' +
178- ' <b>h3</b>' +
179- ' </a>' +
180- ' <a href="#" id="function-h4" class="minibutton function-button js-menu-close" title="Header 4" tabindex="-1">' +
181- ' <b>h4</b>' +
182- ' </a>' +
183- ' <a href="#" id="function-h5" class="minibutton function-button js-menu-close" title="Header 5" tabindex="-1">' +
184- ' <b>h5</b>' +
185- ' </a>' +
186- ' <a href="#" id="function-h6" class="minibutton function-button js-menu-close" title="Header 6" tabindex="-1">' +
187- ' <b>h6</b>' +
188- ' </a>' +
189- ' </div>' +
190- ' </div>' +
191- ' </div>' +
192- ' </div>' +
193- ' </div>' +
194-
195- ' <div class="button-group">' +
196- ' <a href="#" id="function-link" class="minibutton function-button" title="Link" tabindex="-1">' +
197- ' <span class="octicon octicon-link"></span>' +
198- ' </a>' +
199- ' <a href="#" id="function-image" class="minibutton function-button" title="Image" tabindex="-1">' +
200- ' <span class="octicon octicon-file-media"></span>' +
201- ' </a>' +
202- ' </div>' +
203- ' <div class="button-group">' +
204- ' <a href="#" id="function-ul" class="minibutton function-button" title="Unordered List" tabindex="-1">' +
205- ' <span class="octicon octicon-list-unordered"></span>' +
206- ' </a>' +
207- ' <a href="#" id="function-ol" class="minibutton function-button" title="Ordered List" tabindex="-1">' +
208- ' <span class="octicon octicon-list-ordered"></span>' +
209- ' </a>' +
210- ' <a href="#" id="function-checklist" class="minibutton function-button" title="Task List" tabindex="-1">' +
211- ' <span class="octicon octicon-checklist"></span>' +
212- ' </a>' +
213- ' </div>' +
214-
215- ' <div class="button-group">' +
216- ' <a href="#" id="function-code" class="minibutton function-button" title="Code" tabindex="-1">' +
217- ' <span class="octicon octicon-code"></span>' +
218- ' </a>' +
219- ' <a href="#" id="function-blockquote" class="minibutton function-button" title="Blockquote" tabindex="-1">' +
220- ' <span class="octicon octicon-quote"></span>' +
221- ' </a>' +
222- ' <a href="#" id="function-hr" class="minibutton function-button" title="Horizontal Rule" tabindex="-1">' +
223- ' <span class="octicon octicon-horizontal-rule"></span>' +
224- ' </a>' +
225- ' <a href="#" id="function-table" class="minibutton function-button" title="Table" tabindex="-1">' +
226- ' <span class="octicon octicon-three-bars"></span>' +
227- ' </a>' +
228- ' </div>' +
229-
230- //' <a href="#" id="function-help" class="minibutton function-button" title="Help" tabindex="-1">' +
231- //' <span class="octicon octicon-question"></span>' +
232- //' </a>' +
233- ' </div>' +
234- '</div>' ;
235- commentForm . parentNode . insertBefore ( gollumEditor , commentForm . parentNode . firstChild ) ;
236-
237- Array . forEach ( gollumEditor . querySelectorAll ( ".function-button" ) , function ( button ) {
238- button . addEventListener ( "click" , function ( e ) {
239- e . preventDefault ( ) ;
240-
241- executeAction ( MarkDown [ this . id ] , commentForm ) ;
242-
243- return false ;
244- } ) ;
245- } ) ;
143+ var editorHTML = ( function ( ) {
144+ return '<div id="gollum-editor-function-buttons">' +
145+ ' <div class="button-group">' +
146+ ' <a href="#" id="function-bold" class="minibutton function-button" title="Bold" tabindex="-1">' +
147+ ' <b>B</b>' +
148+ ' </a>' +
149+ ' <a href="#" id="function-italic" class="minibutton function-button" title="Italic" tabindex="-1">' +
150+ ' <em>i</em>' +
151+ ' </a>' +
152+ ' <a href="#" id="function-strikethrough" class="minibutton function-button" title="Strikethrough" tabindex="-1">' +
153+ ' <s>S</s>' +
154+ ' </a>' +
155+ ' </div>' +
246156
247- } ) ;
157+ ' <div class="button-group">' +
158+ ' <div class="select-menu js-menu-container js-select-menu js-composer-assignee-picker">' +
159+ ' <span aria-haspopup="true" title="Headers" role="button" class="minibutton select-menu-button icon-only js-menu-target" style="padding:0 18px 0 7px; width:auto; border-bottom-right-radius:3px; border-top-right-radius:3px;">' +
160+ ' <b>h#</b>' +
161+ ' </span>' +
162+ ' <div aria-hidden="false" class="select-menu-modal-holder js-menu-content js-navigation-container js-active-navigation-container" style="top: 26px;">' +
163+ ' <div class="select-menu-modal" style="width:auto;">' +
164+ ' <div class="select-menu-header">' +
165+ ' <span class="select-menu-title">Choose header</span>' +
166+ ' <span class="octicon octicon-remove-close js-menu-close"></span>' +
167+ ' </div>' +
168+ ' <div class="button-group">' +
169+ ' <a href="#" id="function-h1" class="minibutton function-button js-menu-close" title="Header 1" tabindex="-1">' +
170+ ' <b>h1</b>' +
171+ ' </a>' +
172+ ' <a href="#" id="function-h2" class="minibutton function-button js-menu-close" title="Header 2" tabindex="-1">' +
173+ ' <b>h2</b>' +
174+ ' </a>' +
175+ ' <a href="#" id="function-h3" class="minibutton function-button js-menu-close" title="Header 3" tabindex="-1">' +
176+ ' <b>h3</b>' +
177+ ' </a>' +
178+ ' <a href="#" id="function-h4" class="minibutton function-button js-menu-close" title="Header 4" tabindex="-1">' +
179+ ' <b>h4</b>' +
180+ ' </a>' +
181+ ' <a href="#" id="function-h5" class="minibutton function-button js-menu-close" title="Header 5" tabindex="-1">' +
182+ ' <b>h5</b>' +
183+ ' </a>' +
184+ ' <a href="#" id="function-h6" class="minibutton function-button js-menu-close" title="Header 6" tabindex="-1">' +
185+ ' <b>h6</b>' +
186+ ' </a>' +
187+ ' </div>' +
188+ ' </div>' +
189+ ' </div>' +
190+ ' </div>' +
191+ ' </div>' +
192+
193+ ' <div class="button-group">' +
194+ ' <a href="#" id="function-link" class="minibutton function-button" title="Link" tabindex="-1">' +
195+ ' <span class="octicon octicon-link"></span>' +
196+ ' </a>' +
197+ ' <a href="#" id="function-image" class="minibutton function-button" title="Image" tabindex="-1">' +
198+ ' <span class="octicon octicon-file-media"></span>' +
199+ ' </a>' +
200+ ' </div>' +
201+ ' <div class="button-group">' +
202+ ' <a href="#" id="function-ul" class="minibutton function-button" title="Unordered List" tabindex="-1">' +
203+ ' <span class="octicon octicon-list-unordered"></span>' +
204+ ' </a>' +
205+ ' <a href="#" id="function-ol" class="minibutton function-button" title="Ordered List" tabindex="-1">' +
206+ ' <span class="octicon octicon-list-ordered"></span>' +
207+ ' </a>' +
208+ ' <a href="#" id="function-checklist" class="minibutton function-button" title="Task List" tabindex="-1">' +
209+ ' <span class="octicon octicon-checklist"></span>' +
210+ ' </a>' +
211+ ' </div>' +
212+
213+ ' <div class="button-group">' +
214+ ' <a href="#" id="function-code" class="minibutton function-button" title="Code" tabindex="-1">' +
215+ ' <span class="octicon octicon-code"></span>' +
216+ ' </a>' +
217+ ' <a href="#" id="function-blockquote" class="minibutton function-button" title="Blockquote" tabindex="-1">' +
218+ ' <span class="octicon octicon-quote"></span>' +
219+ ' </a>' +
220+ ' <a href="#" id="function-hr" class="minibutton function-button" title="Horizontal Rule" tabindex="-1">' +
221+ ' <span class="octicon octicon-horizontal-rule"></span>' +
222+ ' </a>' +
223+ ' <a href="#" id="function-table" class="minibutton function-button" title="Table" tabindex="-1">' +
224+ ' <span class="octicon octicon-three-bars"></span>' +
225+ ' </a>' +
226+ ' </div>' +
227+ '</div>' ;
228+ } ) ( ) ;
248229
249230 // Source: https://github.com/gollum/gollum/blob/9c714e768748db4560bc017cacef4afa0c751a63/lib/gollum/public/gollum/javascript/editor/gollum.editor.js#L516
250231 function executeAction ( definitionObject , commentForm ) {
333314 }
334315 }
335316
317+ function isWiki ( ) {
318+ return / \/ w i k i \/ / . test ( location . href ) ;
319+ }
320+
321+ var functionButtonClick = function ( e ) {
322+ e . preventDefault ( ) ;
323+ executeAction ( MarkDown [ this . id ] , this . commentForm ) ;
324+ return false ;
325+ } ;
326+
327+ function addToolbar ( ) {
328+ if ( isWiki ( ) ) {
329+ // Override existing language with improved & missing functions and remove existing click events;
330+ unsafeWindow . $ . GollumEditor . defineLanguage ( "markdown" , MarkDown ) ;
331+ unsafeWindow . $ ( ".function-button:not(#function-help)" ) . unbind ( "click" ) ;
332+
333+ // Remove existing click events when changing languages;
334+ document . getElementById ( "wiki_format" ) . addEventListener ( "change" , function ( ) {
335+ unsafeWindow . $ ( ".function-button:not(#function-help)" ) . unbind ( "click" ) ;
336+
337+ Array . forEach ( document . querySelectorAll ( ".comment-form-textarea .function-button" ) , function ( button ) {
338+ button . removeEventListener ( "click" , functionButtonClick ) ;
339+ } ) ;
340+ } ) ;
341+ }
342+
343+ Array . forEach ( document . querySelectorAll ( ".comment-form-textarea" ) , function ( commentForm ) {
344+ if ( commentForm . classList . contains ( "GithubCommentEnhancer" ) ) { return ; }
345+ commentForm . classList . add ( "GithubCommentEnhancer" ) ;
346+
347+ var gollumEditor ;
348+ if ( isWiki ( ) ) {
349+ gollumEditor = document . getElementById ( "gollum-editor-function-bar" ) ;
350+ var temp = document . createElement ( "div" ) ;
351+ temp . innerHTML = editorHTML ;
352+ temp . firstChild . appendChild ( document . getElementById ( "function-help" ) ) ; // restore the help button;
353+ gollumEditor . replaceChild ( temp . firstChild , document . getElementById ( "gollum-editor-function-buttons" ) ) ;
354+ } else {
355+ gollumEditor = document . createElement ( "div" ) ;
356+ gollumEditor . innerHTML = editorHTML ;
357+ gollumEditor . id = "gollum-editor-function-bar" ;
358+ gollumEditor . style . border = "0 none" ;
359+ gollumEditor . classList . add ( "active" ) ;
360+ commentForm . parentNode . insertBefore ( gollumEditor , commentForm . parentNode . firstChild ) ;
361+ }
362+
363+ Array . forEach ( gollumEditor . querySelectorAll ( ".function-button" ) , function ( button ) {
364+ button . commentForm = commentForm ; // remove event listener doesn't accept `bind`;
365+ button . addEventListener ( "click" , functionButtonClick ) ;
366+ } ) ;
367+ } ) ;
368+ }
369+
370+ // init;
371+ addToolbar ( ) ;
372+
373+ // on pjax;
374+ unsafeWindow . $ ( document ) . on ( "pjax:success" , addToolbar ) ;
375+
376+ // on page update;
377+ unsafeWindow . $ . pageUpdate ( function ( ) {
378+ window . setTimeout ( function ( ) {
379+ addToolbar ( ) ;
380+ } , 1 ) ;
381+ } ) ;
382+
336383} ) ( ) ;
0 commit comments