User:Polygnotus/Scripts/ReplyButtonsv4.js
Appearance
From Wikipedia, the free encyclopedia
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.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/ReplyButtonsv4.
(function(){ if(mw.config.get('wgDiscussionToolsFeaturesEnabled')){ mw.loader.using(['ext.discussionTools.ReplyWidget','mediawiki.api'],()=>{ // Improved configuration loading with caching asyncfunctionloadToolsConfig(){ constusername=mw.config.get('wgUserName'); if(!username)return[]; constapi=newmw.Api(); try{ constresult=awaitapi.get({ action:'query', prop:'revisions', titles:`User:${username}/ReplyButtonsJSON`, rvslots:'*', rvprop:'content', formatversion:'2', uselang:'content',// Enable caching smaxage:'86400',// Cache for 1 day maxage:'86400'// Cache for 1 day }); if(result.query.pages[0].missing){ return[]; } constcontent=result.query.pages[0].revisions[0].slots.main.content; returnJSON.parse(content); }catch(error){ console.error('Error loading reply buttons configuration:',error); return[]; } } // Helper function to create tools from config functioncreateToolFromConfig(config){ // Add error handling for icon URLs if(config.icon?.startsWith('http')){ try{ addCustomIconCSS(config.name,config.icon); }catch(error){ console.warn(`Failed to add custom icon for ${config.name}:`,error); config.icon='help';// Fallback icon } } // Create command with improved error handling constCommandClass=function(){ ve.ui.Command.call(this,config.name); }; OO.inheritClass(CommandClass,ve.ui.Command); CommandClass.prototype.execute=function(){ try{ consttarget=ve.init.target; if(!target){ console.warn('Visual editor target not found'); returnfalse; } constsurface=target.getSurface(); constsurfaceModel=surface.getModel(); constcontent=typeofconfig.insertContent==='function' ?config.insertContent() :config.insertText.replace(/'\s*\+\s*'/g,''); surfaceModel.getFragment() .collapseToEnd() .insertContent(content) .collapseToEnd() .select(); returntrue; }catch(error){ console.error(`Error executing command ${config.name}:`,error); returnfalse; } }; ve.ui.commandRegistry.register(newCommandClass()); // Create tool with improved validation constToolClass=function(){ ve.ui.Tool.apply(this,arguments); }; OO.inheritClass(ToolClass,ve.ui.Tool); ToolClass.static.name=config.name; ToolClass.static.title=config.title||config.name; ToolClass.static.commandName=config.name; ToolClass.static.icon=config.icon?.startsWith('http') ?'custom-'+config.name :(config.icon||'help'); ve.ui.toolFactory.register(ToolClass); // Add to toolbar groups with improved module detection constreplyWidget=mw.loader.moduleRegistry['ext.discussionTools.ReplyWidget'] ?.packageExports?.['dt-ve/CommentTarget.js']; constnewTopicWidget=mw.loader.moduleRegistry['ext.discussionTools.NewTopic'] ?.packageExports?.['dt-ve/NewTopicTarget.js']; if(replyWidget?.static?.toolbarGroups){ consttoolbarGroup=replyWidget.static.toolbarGroups[3]; if(toolbarGroup&&Array.isArray(toolbarGroup.include)){ toolbarGroup.include.push(config.name); } } if(newTopicWidget?.static?.toolbarGroups){ letcustomGroup=newTopicWidget.static.toolbarGroups.find(g=>g.name==='custom'); if(!customGroup){ customGroup={ name:'custom', include:[], demote:['custom'] }; newTopicWidget.static.toolbarGroups.push(customGroup); } if(Array.isArray(customGroup.include)){ customGroup.include.push(config.name); } } } // Function to add custom CSS with error handling functionaddCustomIconCSS(name,iconUrl){ conststyleId=`custom-icon-${name}`; if(!document.getElementById(styleId)){ conststyle=document.createElement('style'); style.id=styleId; style.textContent=` .oo-ui-icon-custom-${name} { background-image: url(${iconUrl}) !important; background-size: contain !important; background-position: center !important; background-repeat: no-repeat !important; } `; document.head.appendChild(style); } } // Initialize tools with improved error handling loadToolsConfig().then(tools=>{ if(Array.isArray(tools)&&tools.length>0){ tools.forEach(config=>{ try{ createToolFromConfig(config); }catch(error){ console.error(`Failed to create tool from config:`,config,error); } }); } }).catch(error=>{ console.error('Failed to load tools configuration:',error); }); }); } })();