MediaWiki:Common.js: Difference between revisions
Appearance
Colabs Admin (talk | contribs) Created page with "→Any JavaScript here will be loaded for all users on every page load.: $(document).ready(function() { $('.mw-highlight').each(function() { var $this = $(this); // Create button var $button = $('<button class="copy-button">Copy</button>'); $button.css({ 'position': 'absolute', 'top': '5px', 'right': '5px', 'font-size': '0.8em', 'cursor': 'pointer' });..." |
Colabs Admin (talk | contribs) No edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
/* Add STICKY copy icon to syntaxhighlight blocks */ | |||
$('.mw-highlight').each(function() { | mw.hook('wikipage.content').add(function($content) { | ||
var $ | var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>'; | ||
var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>'; | |||
var $button = $('< | |||
$content.find('.mw-highlight').each(function() { | |||
var $block = $(this); | |||
if ($block.find('.copy-button').length > 0) return; | |||
' | |||
var $button = $('<button>') | |||
.addClass('copy-button') | |||
.html(copyIcon) | |||
.attr('title', 'Copy to clipboard') | |||
.on('click', function() { | |||
var code = $block.find('pre').text(); | |||
navigator.clipboard.writeText(code).then(function() { | |||
$button.html(checkIcon); | |||
setTimeout(function() { $button.html(copyIcon); }, 2000); | |||
}); | |||
}); | }); | ||
// Prepend the button so it sits at the top of the container | |||
$block.prepend($button); | |||
}); | }); | ||
}); | }); | ||
Latest revision as of 10:06, 6 May 2026
/* Any JavaScript here will be loaded for all users on every page load. */
/* Add STICKY copy icon to syntaxhighlight blocks */
mw.hook('wikipage.content').add(function($content) {
var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#28a745" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
$content.find('.mw-highlight').each(function() {
var $block = $(this);
if ($block.find('.copy-button').length > 0) return;
var $button = $('<button>')
.addClass('copy-button')
.html(copyIcon)
.attr('title', 'Copy to clipboard')
.on('click', function() {
var code = $block.find('pre').text();
navigator.clipboard.writeText(code).then(function() {
$button.html(checkIcon);
setTimeout(function() { $button.html(copyIcon); }, 2000);
});
});
// Prepend the button so it sits at the top of the container
$block.prepend($button);
});
});