0

I'd like to understand fundamentally why it doesn't work, and if there are any workarounds.

In my pre-request script:

let template = `
<html>
    <body>

        <button onclick="myFunction()">Copy text</button>

        <script>
            function myFunction() {
                navigator.clipboard.writeText("COPYTEST");
            }
        </script>

    </body>
</html>
`;
pm.visualizer.set(template);

The button just doesn't do anything

visualizer

Cœur
  • 37,241
  • 25
  • 195
  • 267
SpecialK711
  • 48
  • 1
  • 8
  • This is a method I have used in the past, I didn't add this as an answer as I'm not sure it still works. https://community.postman.com/t/copy-the-request-url-to-the-clipboard/11367/3 You can also right click in the visualize window to inspect any errors. – Danny Dainton Aug 14 '22 at 07:59
  • That works! Thank you. Looks like they either removed navigator from postman, or its broken in the current version. Thank you for the workaround! https://github.com/postmanlabs/postman-app-support/issues/10959 – SpecialK711 Aug 14 '22 at 15:19

1 Answers1

1

It looks like Navigator API was intentionally removed from Postman.

https://github.com/postmanlabs/postman-app-support/issues/5722#issuecomment-451657262

Thank you Danny for the workaround!

https://community.postman.com/t/copy-the-request-url-to-the-clipboard/11367/3

let resolvedURL = pm.request.url.toString();
    
let template = `
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js"></script>
  </head>
  <body>
    <div>
      <div>
        <pre><code style="width:max-content!important;" id="copyText">${resolvedURL}</code></pre>
      </div>
      <button class="copyButton" type="button" data-clipboard-action="copy" data-clipboard-target="#copyText" style="background:green;color:white;">Copy to Clipboard</button>
    </div>
  </body>
</html>
<script>
    var clipboard = new ClipboardJS('.copyButton');
    
    clipboard.on('success', function(e) {
        e.clearSelection();
        e.trigger.textContent = 'Copied!';
        window.setTimeout(function() {
            e.trigger.textContent = 'Copy to Clipboard';
        }, 2000);
    });
    clipboard.on('error', function(e) {
        e.clearSelection();
        e.trigger.textContent = 'Not Copied';
        window.setTimeout(function() {
            e.trigger.textContent = 'Copy to Clipboard';
        }, 2000);
    });
    
</script>`
    
pm.visualizer.set(template, pm.response.json())
SpecialK711
  • 48
  • 1
  • 8