Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permission Issue with navigator.clipboard.writeText in Chart view #1513

Closed
FMorschel opened this issue Apr 4, 2024 · 7 comments
Closed

Permission Issue with navigator.clipboard.writeText in Chart view #1513

FMorschel opened this issue Apr 4, 2024 · 7 comments
Labels
feature request New feature or request

Comments

@FMorschel
Copy link

FMorschel commented Apr 4, 2024

Please describe feature/problem details and the solution you'd like.
After looking at #511 I've done some testing and it seems that there is a bug where I can not run a navigator.clipboard.writeText. It says there is no permission for it. I'd like to be able to create a column in my table with a button for me to copy the data for that specific row in a json format.

Describe alternatives you've considered
Adding a new line to the table when clicking that and showing the full text as I want to copy so I can manually select it and copy it with the CTRL+C command.

Are you using the free version/paid version/trial:
I'm using the free version.

@FMorschel FMorschel added the feature request New feature or request label Apr 4, 2024
@FMorschel
Copy link
Author

FMorschel commented Apr 4, 2024

Testing script:

var html = `
<div id="container"></div>

<script>
  // get the container element
  let container = document.getElementById("container");

  // Create a new button element
  var button = document.createElement('button');
  button.innerHTML = 'Copy to clipboard';
  
  // Add an event listener for the click event
  button.addEventListener('click', function() {
      // Use the Clipboard API to write text
      navigator.clipboard.writeText('test').then(function() {

        // Create a new paragraph element
        var p = document.createElement('p');
        p.innerHTML = 'Text copied to clipboard';

        // Append the paragraph to the body of the document
        document.body.appendChild(p);
    }).catch(function(error) {

        // Create a new paragraph element
        var p = document.createElement('p');
        p.innerHTML = 'Failed to copy text' + error;

        // Append the paragraph to the body of the document
        document.body.appendChild(p);
    });
  });
  
  // Append the button to the body of the container
  container.appendChild(button);
</script>
`

tc.chartHTML(html);

Exact error:
Failed to copy textNotAllowedError: Write permission denied.

@rangav
Copy link
Collaborator

rangav commented Apr 5, 2024

Thanks @FMorschel for reporting the bug, I will look into it.

@rangav
Copy link
Collaborator

rangav commented Apr 9, 2024

In Iframe clipboard is not allowed. Use below code instead.

parent.postMessage( {command: 'copy-text', "text": "textdatatocopy"});

Below is updated code

var html = `
<div id="container"></div>

<script>
  // get the container element
  let container = document.getElementById("container");

  // Create a new button element
  var button = document.createElement('button');
  button.innerHTML = 'Copy to clipboard';
  
  // Add an event listener for the click event
  button.addEventListener('click', function() {
      // Use the Clipboard API to write text
      try{
      parent.postMessage({command:'copy-text', "text": "textdata"});
      

        // Create a new paragraph element
        var p = document.createElement('p');
        p.innerHTML = 'Text copied to clipboard';

        // Append the paragraph to the body of the document
        document.body.appendChild(p);
    } catch(error) {

        // Create a new paragraph element
        var p = document.createElement('p');
        p.innerHTML = 'Failed to copy text' + error;

        // Append the paragraph to the body of the document
        document.body.appendChild(p);
    }
  });
  
  // Append the button to the body of the container
  container.appendChild(button);
</script>
`

tc.chartHTML(html);

@rangav rangav closed this as completed Apr 9, 2024
@FMorschel
Copy link
Author

It worked! Thanks! Could this be mentioned in the docs? I think this could be a great help for the extension users.

@rangav
Copy link
Collaborator

rangav commented Apr 9, 2024

Sure, also feel free to submit PR

https://docs.thunderclient.com/testing/chart-view

@FMorschel
Copy link
Author

Done that!

@rangav
Copy link
Collaborator

rangav commented Apr 10, 2024

Thanks for the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants