zoukankan      html  css  js  c++  java
  • js获取剪贴板内容

    <html>
    <head>
        <title>读取剪贴板内容</title>

        <script type="text/javascript">
            function getClipboard() {
                if (window.clipboardData) {
                    return (window.clipboardData.getData('Text'));
                }
                else if (window.netscape) {
                    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
                    var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
                    if (!clip) return;
                    var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
                    if (!trans) return;
                    trans.addDataFlavor('text/unicode');
                    clip.getData(trans, clip.kGlobalClipboard);
                    var str = new Object();
                    var len = new Object();
                    try {
                        trans.getTransferData('text/unicode', str, len);
                    }
                    catch (error) {
                        return null;
                    }
                    if (str) {
                        if (Components.interfaces.nsISupportsWString) str = str.value.QueryInterface(Components.interfaces.nsISupportsWString);
                        else if (Components.interfaces.nsISupportsString) str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
                        else str = null;
                    }
                    if (str) {
                        return (str.data.substring(0, len.value / 2));
                    }
                }
                return null;
            }
            function readClipboardData() {
                var str = getClipboard();
                var len = str.split("\n");//获取行数

                document.getElementById("txtContent").value = str;
            }

        </script>

    </head>
    <body>
        <input type="button" value="读取剪贴板内容" onclick="readClipboardData();" />
        <br />
        <textarea rows="20" cols="60" id="txtContent"></textarea>
    </body>
    </html>

  • 相关阅读:
    源码0603-08-掌握-NSURLSession-上传
    源码0603-05-掌握-大文件下载
    源码0603-03-掌握-NSURLSession
    源码0603-01-了解-大文件下载(NSOutputStream)
    源码0602-10-掌握-文件上传11-了解-获得文件的MIMEType
    源码0602-08-掌握-解压缩
    源码0602-06-掌握-小文件下载-大文件下载
    用JS实现的控制页面前进、后退、停止、刷新以及加入收藏等功能
    java一路走来
    CKEditor3.5.3 JAVA下使用
  • 原文地址:https://www.cnblogs.com/beijia/p/clipboardData.html
Copyright © 2011-2022 走看看