MinecraftColorCodes icon indicating copy to clipboard operation
MinecraftColorCodes copied to clipboard

document fragment?

Open VaiN474 opened this issue 7 years ago • 3 comments

Not sure what's wrong, but using this I'm only getting a document fragment object returned.

the basic example

<script src="MinecraftColorCodes.js"></script>
<script>
var yourMOTD = "§d§lnerd.nu§8: §6§oCreative Rev 28";
var newMOTD = yourMOTD.replaceColorCodes();
console.log(newMOTD);
<script>

logs to the console as DocumentFragment(3) [ span, span, span ] with a ton of details in child elements.

The usage either isn't as straight-forward as the example, or it's not working as described. I don't know JS well enough to troubleshoot this. Any thoughts?

VaiN474 avatar Dec 17 '18 07:12 VaiN474

Hmm, that's weird. I haven't touched this code for a long time so if it did indeed break it probably has to do with a browser API update.

I'll check on this once I get home - that'll be in about an hour.

FoxInFlame avatar Dec 17 '18 07:12 FoxInFlame

It seems it returns an object rather than a string of HTML.

I was able to fiddle with this and figure it out.

There's no way to output the raw HTML from the result. You have to append it to an element.

var yourMOTD = "§d§lnerd.nu§8: §6§oCreative Rev 28";
var newMOTD = yourMOTD.replaceColorCodes();
console.log(newMOTD);

var motd = document.getElementById("motd");
motd.appendChild(newMOTD);
console.log(motd.innerHTML);

This functions as intended, at least as far as the final page content.

VaiN474 avatar Dec 17 '18 08:12 VaiN474

Yeah, it looks like when I updated the code in 3.7, I changed a normal function to a String prototype function.

The normal function in 3.5 and earlier accepted an argument with an element selector, and the function actually replaced the element. The string prototype version looks easier to use but in fact, the fact that a string couldn't be returned was a problem I likely gave up on addressing. I believe the String prototype version is faulty. For the §k effect to work, MinecraftColorCodes has to either return an element itself or append it to the DOM itself. I wonder why 2-years-ago-me never tested this thing...

Thanks for the input @VaiN474.

Fox

FoxInFlame avatar Dec 17 '18 10:12 FoxInFlame