在Postman Visualizer中复制到剪贴板javascript不工作

68de4m5k  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(228)

我想从根本上了解为什么它不起作用,以及是否有任何变通办法。
在我的预请求脚本中:

let template = `
<html>
    <body>

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

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

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

按钮不起任何作用

kfgdxczn

kfgdxczn1#

它看起来像导航器API是故意从 Postman 删除。
https://github.com/postmanlabs/postman-app-support/issues/5722#issuecomment-451657262
谢谢丹尼的变通办法!
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())

相关问题