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.
// Add a custom button to Wikipedia's Visual Editor// Add this code to your common.js page on Wikipedia// (e.g., https://en.wikipedia.org/wiki/User:YourUsername/common.js)// Wait for the VisualEditor to be readymw.loader.using(['ext.visualEditor.desktopArticleTarget']).then(function(){mw.hook('ve.activationComplete').add(function(){console.log('VE activation hook fired');// Wait a bit for the toolbar to fully initializesetTimeout(function(){addHelloWorldButton();},1000);// Also add the button when the surface is readyif(typeofve!=='undefined'&&ve.init&&ve.init.target){ve.init.target.on('surfaceReady',function(){console.log('Surface ready event fired');addHelloWorldButton();});}});});functionaddHelloWorldButton(){console.log('Attempting to add Hello World button');// Check if our button already exists to avoid duplicatesif($('.oo-ui-tool-name-helloWorldTool').length){console.log('Hello World button already exists');return;}// Create a proper new group for our buttonvar$toolGroup=$('<div>').addClass('ve-ui-toolbar-group-hello oo-ui-widget oo-ui-toolGroup oo-ui-barToolGroup oo-ui-widget-enabled').attr('title','Hello World Button');var$toolsContainer=$('<div>').addClass('oo-ui-toolGroup-tools oo-ui-barToolGroup-tools oo-ui-toolGroup-enabled-tools').appendTo($toolGroup);// Create the button itself matching other buttons' structurevar$button=$('<span>').addClass('oo-ui-widget oo-ui-iconElement oo-ui-tool-with-icon oo-ui-tool oo-ui-tool-name-helloWorldTool oo-ui-widget-enabled').appendTo($toolsContainer);// Create the link elementvar$link=$('<a>').addClass('oo-ui-tool-link').attr('role','button').attr('tabindex','0').attr('title','Hello World Button').appendTo($button);// Add the icon structure$('<span>').addClass('oo-ui-tool-checkIcon oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement oo-ui-iconElement-icon oo-ui-icon-check oo-ui-labelElement-invisible oo-ui-iconWidget').appendTo($link);$('<span>').addClass('oo-ui-iconElement-icon oo-ui-icon-star').appendTo($link);$('<span>').addClass('oo-ui-tool-title').text('Hello').appendTo($link);// Add click event to insert text at cursor position instead of showing alert$button.on('click',function(e){e.preventDefault();e.stopPropagation();// Get the current visual editor surfacevarsurface=ve.init.target.getSurface();// Insert 'hello world' at the cursor positionvarfragment=surface.getModel().getFragment();fragment.insertContent('hello world',true);});// Insert our group at an appropriate ___location in the toolbar// Find the structure or insert group to add aftervar$insertPosition=$('.ve-ui-toolbar-group-structure, .ve-ui-toolbar-group-format').last();if($insertPosition.length){$toolGroup.insertAfter($insertPosition);console.log('Button added successfully after',$insertPosition.attr('class'));}else{// Fallback: add to main toolbar$('.oo-ui-toolbar-tools').first().append($toolGroup);console.log('Button added to main toolbar');}}