blueprint button script problem

Discussions related to the forums itself. Call for moderators. Trash Posts area.
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2920
Joined: Sat Jun 11, 2016 6:41 am
Contact:

blueprint button script problem

Post by Optera »

Hi blueprint tags seem to be kinda broken with Firefox.

In his thread I started only the very first bp button actually copies its string to the clipboard.
The other buttons don't copy their content to clipboard even though their code seems identical apart from the string.
Bilka
Factorio Staff
Factorio Staff
Posts: 3309
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: blueprint button script problem

Post by Bilka »

Well, that's funny. They copied the code from the wiki, but didn't do it correctly :lol:

To the person who will be tasked to fix this: Use a class, not an ID! Element IDs should be unique within the webpage, otherwise you run into situations like this where a browser can only ever get the first element with that ID. If you have to use an ID for some reason, make it unique by appending a number or something similar.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5274
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: blueprint button script problem

Post by Klonan »

Should be fixed now
Bilka
Factorio Staff
Factorio Staff
Posts: 3309
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: blueprint button script problem

Post by Bilka »

Klonan wrote:Should be fixed now
Math.random is not guaranteed to be unique :lol:
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Bilka
Factorio Staff
Factorio Staff
Posts: 3309
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: blueprint button script problem

Post by Bilka »

Here is an idea on how to script it so that you never have problems. The current solution seems very hacky to me.

Current implementation (guess, since I cant see the direct code):

Code: Select all

[bp]{TEXT}[/bp]

<button style="background-color:rgb(19, 93, 144); color:rgb(221, 221, 221); line-height:46px; width:180px; cursor:pointer;"><img src="https://wiki.factorio.com/images/Blueprint.png" style="vertical-align:middle;"> Copy blueprint string</button>
<script>
    var button = document.getElementById("BPbutton");
    button.id = button.id + '_' + Math.random().toString(36).substr(2, 9);
    button.addEventListener("click", function (event) {
    var copyTarget = document.createElement("input");
    copyTarget.setAttribute("value", {TEXT});
    document.body.appendChild(copyTarget);
    copyTarget.select();
    document.execCommand("copy");
    document.body.removeChild(copyTarget);
    });
</script>
From what I can find in the phpbb docs, you have no limitations on what you put into the html field for the custom bbcodes, so you can just put the function directly on the button:

Code: Select all

[bp]{TEXT}[/bp]

<button style="background-color:rgb(19, 93, 144); color:rgb(221, 221, 221); line-height:46px; width:180px; cursor:pointer;" onclick="var copyTarget = document.createElement('input'); copyTarget.setAttribute('value', {TEXT}); document.body.appendChild(copyTarget); copyTarget.select(); document.execCommand('copy'); document.body.removeChild(copyTarget);"><img src="https://wiki.factorio.com/images/Blueprint.png" style="vertical-align:middle;"> Copy blueprint string</button>
This way, you dont rely on browser quirks and random values not being the same.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2920
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: blueprint button script problem

Post by Optera »

Math.random seems to fix it for my browser, no idea if Safari or some other browser will behave properly though.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3716
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: blueprint button script problem

Post by DaveMcW »

Math.random() generates a 64-bit number, which is big enough that a birthday collision is unlikely on this forum.

Of course it is not a good habit to use random ids, it does not scale well past a few hundred blueprints per page.
Post Reply

Return to “This Forum”