1

pyperclip.py を使用して、フォームを使用して Web アプリで電子メール アドレスのリストを取得しているため、ユーザーはクリップボードを介してローカルに貼り付けることができます。ローカルで完璧に機能します。ただし、サーバー (Apache2 を使用する Linux 14.04) で実行し、ブラウザーを介してクライアント システムからアクセスすると、コピーされません。クライアントのシステムのクリップボードにコピーするにはどうすればよいですか?

現在、私はそれを機能させようとしているだけなので、1行しか使用していません。xclip と Python 3.4 で pyperclip 1.5.15 を使用しています。サーバーは Linux 14.04 を実行しており、クライアントは Google Chrome と IE を使用して Windows 8 と Windows 10 で問題を検出しました。他の OS は現在テストされていません。

pyperclip.copy("HELLO") 
4

1 に答える 1

2

この件に関する詳細をあまり見つけることができなかったので、質問に答えようと思いました。残念ながら、ブラウザーが pyperclip をサポートしているようには見えないため、HTML + Javascript の回避策が必要です (pyperclip を意味します)。最初に、Django テンプレート変数をそこから HTML 属性として追加します。Javascript を使用してコピー機能を処理できます。以下はこれを行う方法の例です。スタックオーバーフローが例に奇妙なフォーマットを与えていたため、事前に申し訳ありません. また、email_list_clipboard の ID を持つ以下のフォームがあることも前提としています。これが、同様の問題に遭遇する可能性のある他の人に役立つことを願っています!

例:

    <html email-list="{{request.session.email_list}}">
    <script>
        $(document).ready(function () {
            function copyTextToClipboard(text) {
                var textArea = document.createElement("textarea");

                // Place in top-left corner of screen regardless of scroll position.
                textArea.style.position = 'fixed';
                textArea.style.top = 0;
                textArea.style.left = 0;

                textArea.style.width = '2em';
                textArea.style.height = '2em';

                // We don't need padding, reducing the size if it does flash render.
                textArea.style.padding = 0;

                textArea.style.border = 'none';
                textArea.style.outline = 'none';
                textArea.style.boxShadow = 'none';

                textArea.style.background = 'transparent';

                textArea.value = text;

                document.body.appendChild(textArea);

                textArea.select();

                try {
                    var successful = document.execCommand('copy');
                    var msg = successful ? 'successful' : 'unsuccessful';
            console.log('Copying text command was ' + msg);
                } catch (err) {
                    console.log('Oops, unable to copy');
                }

                document.body.removeChild(textArea);
            }

            // set things up so my function will be called when field_three changes
            $('#email_list_clipboard').click(function (click) {
                event.preventDefault();
                copyTextToClipboard(document.documentElement.getAttribute("email-list"));
    });

</script>
于 2015-10-21T16:00:26.403 に答える