Revision as of 20:21, 16 December 2016 | | Revision as of 20:22, 16 December 2016 |
Line 22: | | Line 22: |
| | | | |
| function wikilinkbullist() { | | | function wikilinkbullist() { |
? | var text1 = document.getElementById('wpTextbox1'); | | + | var text = document.getElementById('wpTextbox1'); |
| selectedText1 = text.value.substring(text.selectionStart, text.selectionEnd); | | | selectedText1 = text.value.substring(text.selectionStart, text.selectionEnd); |
| modifiedText1 = text.value.substring(text.selectionStart, text.selectionEnd).replace(/\*(.+)\n/g, '*[[$1]]\n'); | | | modifiedText1 = text.value.substring(text.selectionStart, text.selectionEnd).replace(/\*(.+)\n/g, '*[[$1]]\n'); |
Revision as of 20:22, 16 December 2016
// WikiLinker
// Add wikilinks for selected text which is a comma-separated or bulleted list
// https://wiki.wargaming.net/en/index.php?title=User:XXN:na/wl.js&action=history
if ( mw.config.get('wgAction')==='edit'||mw.config.get('wgAction')==='submit' ) {
function wikilinker() {
var text = document.getElementById('wpTextbox1');
selectedText = text.value.substring(text.selectionStart, text.selectionEnd);
modifiedText = text.value.substring(text.selectionStart, text.selectionEnd).replace(/, /g, ']], [[' );
modifiedText = modifiedText.replace(/(.+)/g, '[[$1]]' );
// https://burnignorance.com/php-programming-tips/how-to-use-a-variable-in-replace-function-of-javascript/
// I want to replace a variable with a variable – but as a rule in replace function we have to pass a regular exprssion
// so we cannot write string.replace(var1, var2);
// we have to convert the var1 to a regular expression
var sRegExInput = new RegExp(selectedText, "g");
text1 = text.value.replace(selectedText, modifiedText);
$("#wpTextbox1").val(text1);
}
function wikilinkbullist() {
var text = document.getElementById('wpTextbox1');
selectedText1 = text.value.substring(text.selectionStart, text.selectionEnd);
modifiedText1 = text.value.substring(text.selectionStart, text.selectionEnd).replace(/\*(.+)\n/g, '*[[$1]]\n');
//modifiedText = modifiedText.replace(/\*(.+)/g, '*[[$1]]');
var sRegExInput = new RegExp(selectedText1, "g");
text1 = text.value.replace(selectedText1, modifiedText1);
$("#wpTextbox1").val(text1);
}
//firstHeading
//wikiEditor-ui-toolbar
document.getElementById('firstHeading').innerHTML+='<table><tr>\
<td align="center" colspan="2">\
<button type="button" onClick="wikilinker()">Wikify selected text (comma-separated list)</button>\
<button type="button" onClick="wikilinkbullist()">Wikify bulleted list</button>\
</td></tr></table>';
}