-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUserRightsDiffHtmlProcessor.js
More file actions
78 lines (71 loc) · 2.59 KB
/
Copy pathUserRightsDiffHtmlProcessor.js
File metadata and controls
78 lines (71 loc) · 2.59 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
import { UserRightsDiffStringProcessor } from './UserRightsDiffStringProcessor';
export class UserRightsDiffHtmlProcessor {
constructor( $, mw ) {
// eslint-disable-next-line no-jquery/variable-pattern
this.$ = $;
this.mw = mw;
}
execute() {
// User:BradV/Scripts/SuperLinks.js
this.onDomNodeInserted( 'mw-logevent-loglines', this.checkLog, this );
// Special:UserRights, Special:Log, Special:Watchlist
this.checkLog( this );
}
onDomNodeInserted( htmlClassString, fn, that ) {
const observer = new MutationObserver( ( mutations ) => {
mutations.forEach( ( mutation ) => {
const htmlWasAdded = mutation.addedNodes.length;
if ( htmlWasAdded ) {
mutation.addedNodes.forEach( ( node ) => {
if ( node.classList && node.classList.contains( htmlClassString ) ) {
fn( that );
}
} );
}
} );
} );
const config = { childList: true, subtree: true };
observer.observe( document.body, config );
}
checkLog( that ) {
// don't run twice on the same page
if ( that.$( '.user-rights-diff' ).length === 0 ) {
// Special:UserRights, Special:Log, BradV SuperLinks
that.$( '.mw-logevent-loglines .mw-logline-rights' ).each( function () {
that.checkLine( this );
} );
// Special:Watchlist
that.$( '.mw-changeslist-log-rights .mw-changeslist-log-entry' ).each( function () {
that.checkLine( this );
} );
}
}
checkLine( el ) {
let text = this.$( el ).text();
// We're going to write this text back to the browser window later. HTML escape it to prevent XSS.
text = this.mw.html.escape( text );
const stringProcessor = new UserRightsDiffStringProcessor();
let from, to;
try {
[ from, to ] = stringProcessor.logEntryStringToArrays( text );
} catch ( err ) {
throw new Error( 'UserRightsDiff.js error. Error was: ' + err + '. Input text was: ' + this.$( el ).text() );
}
let added = to.filter( ( x ) => !from.includes( x ) );
let removed = from.filter( ( x ) => !to.includes( x ) );
added = added.length > 0 ?
'<span class="user-rights-diff" style="background-color:lawngreen">[ADDED: ' + this.permArrayToString( added ) + ']</span>' :
'';
removed = removed.length > 0 ?
'<span class="user-rights-diff" style="background-color:yellow">[REMOVED: ' + this.permArrayToString( removed ) + ']</span>' :
'';
const noChange = added.length === 0 && removed.length === 0 ?
'<span class="user-rights-diff" style="background-color:lightgray">[NO CHANGE]</span>' :
'';
this.$( el ).append( `<br />${ added } ${ removed } ${ noChange }` );
}
permArrayToString( array ) {
array = array.join( ', ' );
return array;
}
}