Hey, everybody!
A while ago, I did
a patch to add a "Copy" button to
[code] blocks. Thinking about it a little more, I think a "[Select]" link (like the one I've seen in later SMF versions) might actually make more sense...
After coming up with my own implementation of this, I took a peek at what the SMF devs did for 2.x and noticed that they're special-casing things for different browsers. I'd like to know if that special-casing is still necessary these days, so I'd appreciate some help with testing this on different browsers.
Basically, go to any forum page that contains
[code] blocks (like the page you're currently on), and then open up the developer console in your browser and paste/run the following code:
function addSelectionLinksToCodeBlocks() {
const allCodeDivs = document.querySelectorAll("div.codeheader + div.code");
for (const codeDiv of allCodeDivs) {
const codeHeaderDiv = codeDiv.previousElementSibling;
if (!codeHeaderDiv.querySelector("a.selectionLink")) {
const selectionLink = document.createElement("a");
selectionLink.onclick = () => {
const range = document.createRange();
const selection = document.getSelection();
range.selectNodeContents(codeDiv);
selection.removeAllRanges();
selection.addRange(range);
return false;
};
selectionLink.className = "selectionLink";
selectionLink.href = "javascript:void(0);";
selectionLink.textContent = "[Select]";
codeHeaderDiv.appendChild(document.createTextNode(" "));
codeHeaderDiv.appendChild(selectionLink);
}
}
}
addSelectionLinksToCodeBlocks();
(Make sure to copy it in its entirety and to hit enter after pasting it.)
You should now see that each
[code] block has a little "[Select]" link at the top. Click on that link and check if the code for that block gets correctly selected or not.
If it doesn't work for you, then please post your browser details (just the name and version will do).
If you have thoughts about preferring the "[Select]" link to the "Copy" button (or vice versa), feel free to leave those, too.
Thanks.
(I'm not really looking for code critiques. If you're a programmer and something looks odd to you, probably I'm either sticking to some established SMF 1.x idiom, or it's because I had to write this code in a particular way to get Cloudflare's WAF to stop blocking me from posting it.)