Difference between revisions of "User:XXN:na/wikilinker.js"
Revision as of 16:23, 2 November 2016 | Latest revision as of 21:14, 25 September 2017 | |||
25 intermediate revisions by the same user not shown | ||||
Line 1: | Line 1: | |||
? | // | + | // 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 addbullets() { | ||
? | + | var text = document.getElementById('wpTextbox1'); | ||
? | + | selectedText0 = text.value.substring(text.selectionStart, text.selectionEnd); | ||
? | + | modifiedText0 = text.value.substring(text.selectionStart, text.selectionEnd).replace(/(.+)\n/g, '* $1\n' ); | ||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | text0 = text.value.replace(selectedText0, modifiedText0); | ||
? | + | $("#wpTextbox1").val(text0); | ||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
} | } | |||
+ | 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]]' ); | |||
? | function | + | // 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(/\*\s*(\w.+)\n/g, '* [[$1]]\n'); | |||
+ | //modifiedText = modifiedText.replace(/\*(.+)/g, '*[[$1]]'); | |||
+ | //var sRegExInput = new RegExp(selectedText1, "g"); | |||
+ | text2 = text.value.replace(selectedText1, modifiedText1); | |||
+ | $("#wpTextbox1").val(text2); | |||
+ | } | |||
? | + | //firstHeading | ||
? | + | //wikiEditor-ui-toolbar | ||
? | + | document.getElementById('firstHeading').innerHTML+='<table><tr>\ | ||
? | + | <td align="center" colspan="2">\ | ||
? | + | <button type="button" onClick="addbullets()">Add bullets to vertical list</button>\ | ||
? | + | <button type="button" onClick="wikilinker()">Wikify selected text (comma-separated list)</button>\ | ||
? | + | <button type="button" onClick="wikilinkbullist()">Wikify bulleted list</button>\ | ||
? | + | </td></tr></table>'; | ||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
? | + | |||
} | } |
Latest revision as of 21:14, 25 September 2017
// 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 addbullets() { var text = document.getElementById('wpTextbox1'); selectedText0 = text.value.substring(text.selectionStart, text.selectionEnd); modifiedText0 = text.value.substring(text.selectionStart, text.selectionEnd).replace(/(.+)\n/g, '* $1\n' ); text0 = text.value.replace(selectedText0, modifiedText0); $("#wpTextbox1").val(text0); } 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(/\*\s*(\w.+)\n/g, '* [[$1]]\n'); //modifiedText = modifiedText.replace(/\*(.+)/g, '*[[$1]]'); //var sRegExInput = new RegExp(selectedText1, "g"); text2 = text.value.replace(selectedText1, modifiedText1); $("#wpTextbox1").val(text2); } //firstHeading //wikiEditor-ui-toolbar document.getElementById('firstHeading').innerHTML+='<table><tr>\ <td align="center" colspan="2">\ <button type="button" onClick="addbullets()">Add bullets to vertical list</button>\ <button type="button" onClick="wikilinker()">Wikify selected text (comma-separated list)</button>\ <button type="button" onClick="wikilinkbullist()">Wikify bulleted list</button>\ </td></tr></table>'; }