| 1 |
const |
| 2 |
Parser = require( 'ext.discussionTools.init' ).Parser, |
| 3 |
modifier = require( 'ext.discussionTools.init' ).modifier, |
| 4 |
utils = require( 'ext.discussionTools.init' ).utils, |
| 5 |
debugHighlighter = require( './debughighlighter.js' ), |
| 6 |
parser = new Parser( require( 'ext.discussionTools.init' ).parserData ), |
| 7 |
result = parser.parse( |
| 8 |
document.getElementById( 'mw-content-text' ), |
| 9 |
mw.Title.newFromText( mw.config.get( 'wgRelevantPageName' ) ) |
| 10 |
), |
| 11 |
comments = result.getCommentItems(), |
| 12 |
threads = result.getThreads(), |
| 13 |
timestampRegexps = parser.getLocalTimestampRegexps(), |
| 14 |
debug = +( new URL( location.href ).searchParams.get( 'dtdebug' ) ), |
| 15 |
DEBUG_HIGHLIGHT = 1, |
| 16 |
DEBUG_VOTE = 2, |
| 17 |
DEBUG_VOTE_PERMISSIVE = 4; |
| 18 |
|
| 19 |
|
| 20 |
if ( debug & DEBUG_HIGHLIGHT ) { |
| 21 |
debugHighlighter.markThreads( threads ); |
| 22 |
|
| 23 |
comments.forEach( ( comment ) => { |
| 24 |
comment.signatureRanges.forEach( ( signatureRange ) => { |
| 25 |
const node = signatureRange.endContainer; |
| 26 |
const match = parser.findTimestamp( node, timestampRegexps ); |
| 27 |
if ( !match ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
const signature = parser.findSignature( node ).nodes; |
| 31 |
const emptySignature = signature.length === 1 && signature[ 0 ] === node; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
debugHighlighter.markTimestamp( parser, node, match ); |
| 36 |
if ( !emptySignature ) { |
| 37 |
debugHighlighter.markSignature( signature ); |
| 38 |
} |
| 39 |
} ); |
| 40 |
} ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
if ( ( debug & DEBUG_VOTE ) || ( debug & DEBUG_VOTE_PERMISSIVE ) ) { |
| 45 |
threads.forEach( ( thread ) => { |
| 46 |
const firstComment = thread.replies[ 0 ]; |
| 47 |
|
| 48 |
if ( firstComment && firstComment.type === 'comment' ) { |
| 49 |
|
| 50 |
if ( !( debug & DEBUG_VOTE_PERMISSIVE ) && firstComment.level <= 1 ) { |
| 51 |
|
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
const firstVote = firstComment.level === 1 ? |
| 56 |
|
| 57 |
firstComment.replies[ 0 ] : |
| 58 |
firstComment; |
| 59 |
|
| 60 |
if ( !firstVote ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
let lastReply; |
| 65 |
const level = firstVote.level; |
| 66 |
firstVote.parent.replies.forEach( ( reply ) => { |
| 67 |
if ( reply.type === 'comment' && reply.level <= level ) { |
| 68 |
lastReply = reply; |
| 69 |
} |
| 70 |
} ); |
| 71 |
|
| 72 |
const listItem = modifier.addSiblingListItem( |
| 73 |
utils.closestElement( lastReply.range.endContainer, [ 'li', 'dd', 'p' ] ) |
| 74 |
); |
| 75 |
if ( listItem && listItem.tagName.toLowerCase() === 'li' ) { |
| 76 |
$( listItem ) |
| 77 |
|
| 78 |
.css( 'list-style', 'none' ) |
| 79 |
.append( |
| 80 |
'[ ', |
| 81 |
$( '<a>' ).text( 'add comment' ), |
| 82 |
' ]' |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
} ); |
| 87 |
} |