0

During my work I need to get new GUID for some reasons. I've created bookmarklet to generate GUID

Javascript:

function createUUID() { 
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { 
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); 
    return v.toString(16);
  });
}
alert(createUUID())

And now I'm trying to find out how to add a Copy to Clipboard button there. Customization of alert() or confirm() is impossible so I need to call some popup first and then do other things.

Please help to start :)

MaxiGui
  • 4,537
  • 4
  • 9
  • 31
  • The whole point of GUID's is that they are truly unique across multiple systems. You will not achieve this by using Math.random() as it is low quality source of randomness. To generate proper RFC4122 compliant GUIDs you should use uuid module: https://github.com/uuidjs/uuid – Cninroh Dec 04 '20 at 15:29

1 Answers1

0

I tried this, the snippet gives error - so does the code if not a bookmarklet (DOMException - document is not focussed). So perhaps not

I am using Clipboard writeText

(() => {
  const uuid =  'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c =>  {
    var r = Math.random() * 16 | 0,
      v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
  navigator.clipboard.writeText(uuid)
    .then(() => console.log(uuid, "set"))
    .catch((e) => console.log("error setting", uuid, e))
})()

You may be SOL

How do I copy to the clipboard in JavaScript?

mplungjan
  • 134,906
  • 25
  • 152
  • 209