When writing API code I often refer to the PW admin to get particular page IDs or field names. And when I'm writing client instructions I often need to insert the labels of particular fields.
To make this quicker and more convenient I added some custom JavaScript to the PW admin that copies these things to the clipboard on Alt + click and Ctrl + click. The Page List item and inputfield header are briefly highlighted in yellow to signify that the copying has occurred.
This is tested in Windows and I'm not sure if the Alt key / Ctrl key detection is the same for other operating systems but you could adjust the key detection as needed.
In /site/ready.php
// Add custom JS file to $config->scripts FilenameArray
// This adds the custom JS fairly early in the FilenameArray which allows for stopping
// event propagation so clicks on InputfieldHeader do not also expand/collapse InputfieldContent
$wire->addHookBefore('ProcessController::execute', function(HookEvent $event) {
// Optional: for superuser only
if(!$event->wire()->user->isSuperuser()) return;
$config = $event->wire()->config;
$modified = filemtime($config->paths->templates . 'admin-assets/copy-on-click.js');
$js_url = $config->urls->templates . "admin-assets/copy-on-click.js?m=$modified";
$config->scripts->add($js_url);
});
In /site/templates/admin-assets/copy-on-click.js
$(document).ready(function() {
// Copy a string to the clipboard
function copyToClipboard(string) {
const $temp = $('<input type="text" value="' + string + '">');
$('body').append($temp);
$temp.select();
document.execCommand('copy');
$temp.remove();
}
// Copy page ID when Page List row is Alt + clicked
$(document).on('click', '.PageListItem', function(event) {
if(event.altKey) {
const classes = $(this).attr('class').split(' ');
for(const item of classes) {
if(item.startsWith('PageListID')) {
const id = item.replace('PageListID', '');
copyToClipboard(id);
$(this).effect('highlight', {}, 500);
break;
}
}
}
});
// When InputfieldHeader is clicked
$(document).on('click', '.InputfieldHeader', function(event) {
let text = '';
// If Alt + clicked then copy the input name the label is for, or the ID as a fallback
if(event.altKey) {
event.preventDefault();
event.stopImmediatePropagation();
text = $(this).attr('for');
if(!text) text = $(this).parent().attr('id');
text = text.replace(/^Inputfield_|wrap_Inputfield_|wrap_/, '').trim();
}
// If Ctrl + clicked then copy the label text
else if(event.ctrlKey) {
event.preventDefault();
event.stopImmediatePropagation();
text = $(this).text().trim();
// If AdminOnSteroids is installed use the below instead to exclude text within the AOS field edit link
// text = $(this).clone().find('.aos_EditField').remove().end().text().trim();
}
if(text) {
copyToClipboard(text);
$(this).effect('highlight', {}, 500);
}
});
});
Demo (using the Tracy console only as a convenient place to paste into for demonstration):
Copying the field label is useful for getting the name of config fields too, for when you need them in your API code.