patrickhener / goshs

A SimpleHTTPServer written in Go, enhanced with features and with a nice design - https://goshs.de
MIT License
293 stars 22 forks source link

Added copy to clipboard button for the clipboard #46

Closed marduc812 closed 1 year ago

marduc812 commented 1 year ago

Added a small button next to the delete clipboard option to copy the text to clipboard. In many occasions the text I had to copy was large and it is tiring to manually scroll across the whole view to select it.

Screenshot 2023-06-27 at 19 51 23

The last return false on the copyToClipboard function is just to prevent the page from reloading, instead of passing the event and using preventDefault, since it is not needed.

patrickhener commented 1 year ago

Thanks for contributing <3 This is a good addition to the clipboard.

marduc812 commented 1 year ago

I noticed that the main.min.js needs to recompile. I was testing it locally and it was working because it was loading the main.js file directly. I will send a new update.

marduc812 commented 1 year ago

@patrickhener So the reason that it didn't work was because I tried to access it from http instead of https. Apparently the clipboard method works only for dev or if the app is served over https. Instead something like that could be used. https://stackoverflow.com/a/71876238

For some reason I am not able to update the js. It looks like they are somehow cached on a weird way. I tried clearing my cache, accessing from a private window and restarting the server, but the old version is constantly served. Could you update the function in main.js and in the main.min.js? Sorry for the extra effort.

function copyToClipboard(id) {
  const textSelected = document.getElementById('card-body-' + id).innerText;
  const textArea = document.createElement("textarea");
  textArea.value = textSelected;
  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();
  try {
    document.execCommand('copy');
  } catch (err) {
    console.error('Unable to copy to clipboard', err);
  }
  document.body.removeChild(textArea);
  return false;
}
patrickhener commented 1 year ago

I will need to allocate some spare time, which is not now. I will look into it though.