Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// ==UserScript==// @name RNLI Medal Link Fixer// @namespace http://en.wikipedia.org/// @version 1.4// @description Update RNLI medal wikilinks to specific pages// @author Aluxosm (using ChatGPT)// @match https://en.wikipedia.org/wiki/*// @grant none// ==/UserScript==(function(){'use strict';// Add the button next to the edit tabmw.util.addPortletLink('p-cactions',// Portlet (action menu)'#',// Link href'Fix RNLI Medals',// Button text'ca-fix-rnli-medals'// ID);// Define the replacementsconstreplacements=[{regex:/\[\[Awards of the Royal National Lifeboat Institution#Medal of the RNLI\|([^|\]]*gold[^|\]]*)\]\]/gi,replacement:'[[RNLI Gold Medal|$1]]'},{regex:/\[\[Awards of the Royal National Lifeboat Institution#Medal of the RNLI\|([^|\]]*silver[^|\]]*)\]\]/gi,replacement:'[[RNLI Silver Medal|$1]]'},{regex:/\[\[Awards of the Royal National Lifeboat Institution#Medal of the RNLI\|([^|\]]*bronze[^|\]]*)\]\]/gi,replacement:'[[RNLI Bronze Medal|$1]]'}];// Attach a click event to the buttondocument.getElementById('ca-fix-rnli-medals').addEventListener('click',function(e){e.preventDefault();// Open edit modeconsteditUrl=mw.util.getUrl(mw.config.get('wgPageName'),{action:'edit'});fetch(editUrl).then(response=>response.text()).then(html=>{// Extract raw content without parsing to HTMLconstmatch=html.match(/<textarea[^>]*id="wpTextbox1"[^>]*>([\s\S]*?)<\/textarea>/);if(match&&match[1]){// Decode the content safelyletcontent=match[1];// Perform replacements, only modifying wikilinksreplacements.forEach(({regex,replacement})=>{content=content.replace(regex,replacement);});// Pre-fill the edit box and summaryconsteditForm=document.createElement('form');editForm.method='POST';editForm.action=editUrl;editForm.innerHTML=` <textarea name="wpTextbox1">${content}</textarea> <input type="hidden" name="wpSummary" value="Updated RNLI medal links to direct pages." /> <input type="hidden" name="action" value="submit" /> `;// Submit the edit form to show changesdocument.body.appendChild(editForm);editForm.submit();}else{alert('Unable to access the edit box!');}});});})();