Page 1 of 1

blueprint button script problem

Posted: Sat Jun 09, 2018 5:56 am
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.

Re: blueprint button script problem

Posted: Sat Jun 09, 2018 9:57 am
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.

Re: blueprint button script problem

Posted: Sat Jun 09, 2018 10:45 am
by Klonan
Should be fixed now

Re: blueprint button script problem

Posted: Sat Jun 09, 2018 10:51 am
by Bilka
Klonan wrote:Should be fixed now
Math.random is not guaranteed to be unique :lol:

Re: blueprint button script problem

Posted: Sat Jun 09, 2018 12:16 pm
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.

Re: blueprint button script problem

Posted: Sat Jun 09, 2018 1:35 pm
by Optera
Math.random seems to fix it for my browser, no idea if Safari or some other browser will behave properly though.

Re: blueprint button script problem

Posted: Sun Jun 10, 2018 4:09 am
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.