User:XXN:na/wikilinker.js
Note: After saving, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
// 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>'; }