| 1 |
$( () => { |
| 2 |
|
| 3 |
const classes = { |
| 4 |
singleQuoteString: 's1', doubleQuoteString: 's2' |
| 5 |
}; |
| 6 |
|
| 7 |
function addLink( element, page ) { |
| 8 |
const link = document.createElement( 'a' ); |
| 9 |
link.href = mw.util.getUrl( page ); |
| 10 |
|
| 11 |
const firstChild = element.firstChild; |
| 12 |
if ( !( firstChild instanceof Text ) ) { |
| 13 |
throw new TypeError( 'Expected Text object' ); |
| 14 |
} |
| 15 |
link.appendChild( firstChild ); |
| 16 |
element.appendChild( link ); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
const parametersToLink = { |
| 21 |
require: ( title ) => title.getNamespaceId() === 828, |
| 22 |
'mw.loadData': ( title ) => title.getNamespaceId() === 828, |
| 23 |
'mw.loadJsonData': () => true |
| 24 |
}; |
| 25 |
|
| 26 |
const stringNodes = Array.from( document.getElementsByClassName( classes.singleQuoteString ) ) |
| 27 |
.concat( Array.from( document.getElementsByClassName( classes.doubleQuoteString ) ) ); |
| 28 |
|
| 29 |
stringNodes.forEach( ( node ) => { |
| 30 |
if ( !node.nextElementSibling || |
| 31 |
!node.nextElementSibling.firstChild || |
| 32 |
!node.nextElementSibling.firstChild.nodeValue || |
| 33 |
node.nextElementSibling.firstChild.nodeValue.indexOf( ')' ) !== 0 ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
if ( !node.previousElementSibling || !node.previousElementSibling.firstChild || |
| 37 |
node.previousElementSibling.firstChild.nodeValue !== '(' ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
Object.keys( parametersToLink ).forEach( ( invocation ) => { |
| 41 |
const parts = invocation.split( '.' ); |
| 42 |
let partIdx = parts.length - 1; |
| 43 |
let curNode = node.previousElementSibling && node.previousElementSibling.previousElementSibling; |
| 44 |
while ( partIdx >= 0 ) { |
| 45 |
if ( !curNode || curNode.firstChild.nodeValue !== parts[ partIdx ] ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
if ( partIdx === 0 ) { |
| 49 |
break; |
| 50 |
} |
| 51 |
const prev = curNode.previousElementSibling; |
| 52 |
if ( !prev || prev.firstChild.nodeValue !== '.' ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
curNode = prev.previousElementSibling; |
| 56 |
partIdx--; |
| 57 |
} |
| 58 |
const page = node.firstChild.nodeValue.slice( 1, -1 ); |
| 59 |
const condition = parametersToLink[ invocation ]; |
| 60 |
const title = mw.Title.newFromText( page ); |
| 61 |
if ( title && condition( title ) ) { |
| 62 |
addLink( node, title.toText() ); |
| 63 |
} |
| 64 |
} ); |
| 65 |
} ); |
| 66 |
|
| 67 |
} ); |