使用商标将JavaScript复制到剪贴板

    技术2022-07-11  150

    I published a post a year ago detailing how you can copy to the clipboard using JavaScript.  The post was very popular and why would it be?  Copying content from a webpage without needing to use the default browser functions is awesome.  One trend I've seen recently is that when the use copies content from a given post, the copy function (Zero Clipboard, in this example) appends the page title, a link, and a special message telling the recipient to view the post.  Here's how to do it.

    我一年前发表了一篇文章,详细介绍了如何使用JavaScript复制到剪贴板 。 该帖子非常受欢迎,为什么会这样? 从网页复制内容而无需使用默认浏览器功能真是棒极了。 我最近看到的一种趋势是,当用户从给定帖子中复制内容时,复制功能(在此示例中为“零剪贴板”)会附加页面标题,链接和一条特殊消息,告诉收件人查看该帖子。 这是操作方法。

    JavaScript (The JavaScript)

    Assuming the user is using ZeroClipboard functionality:

    假设用户正在使用ZeroClipboard功能:

    //set path ZeroClipboard.setMoviePath('https://davidwalsh.name/demo/ZeroClipboard.swf'); //create client var clip = new ZeroClipboard.Client(); //event clip.addEventListener('mousedown',function() { clip.setText(document.getElementById('box-content').value + ".\n\nRead " + window.title + " at " + window.location + " to learn more!"); }); //glue it to the button clip.glue('copy');

    Appending the special message to the given text is done with basic JavaScript string concatenation of available window properties.

    将特殊消息附加到给定的文本是通过可用窗口属性的基本JavaScript字符串串联完成的。

    This post is more about the idea than it is the execution.  This type of marketing is good for newspaper-style websites that host thousands of posts and want to give themselves credit for the content that's been copied.

    这篇文章更多地是关于这个想法,而不是执行。 这种营销方式对于拥有数千个帖子并且希望对自己复制的内容功不可没的报纸式网站很有用。

    翻译自: https://davidwalsh.name/javascript-clipboard

    相关资源:hardenedpaste:[已淘汰] Hardened Paste是一种浏览器扩展,可以防止“ pastejacking”漏洞-源码
    Processed: 0.015, SQL: 10