8

Chrome拡張機能を介してテキスト変数をクリップボードに書き込みたいのですが、ユーザーがショートキーを押すと発生します。クリップボードへの書き込みを除くすべての部分を実行しました。

「[google-chrome-extension] Clipboard」というキーワードを使用して、StackOverflow 全体を検索しました。

だから私が言いたいのは、私は以下に関連するすべてを見てきました:

  • 追加clipboardReadclipboardWrite許可 (既に行われています)
  • <textarea>、呼び出し 、document.execCommand('Copy'); または document.execCommand("Copy", false, null);

StackOverflow で拡張機能を試してみましたがtextarea、テキストを StackOverflow の wmd-input 部分に挿入しtextarea、それを選択してコピーを呼び出しました。何も、何も、何も...

すべてが試みられました。アドバイスしてください...何が欠けていますか?

4

4 に答える 4

10

https://stackoverflow.com/a/12693636に基づく

function directCopy(str){
    //based on https://stackoverflow.com/a/12693636
        document.oncopy = function(event) {
    event.clipboardData.setData("Text", str);
    event.preventDefault();
        };
    document.execCommand("Copy");
        document.oncopy = undefined;
}
于 2013-08-15T17:27:28.070 に答える
5

次のコードを試すことができます。テキストをクリップボードに書き込みます

例として、私はSampleクリップボードに書きました

出力

ここに画像の説明を入力

マニフェスト.json

マニフェスト ファイルは、すべての Chrome 拡張機能のキーであり、すべての権限が付与されていることが保証されています

 {
  "name": "Copy to ClipBoard Demo",
  "description" : "This is used for demonstrating Copy to Clip Board Functionality",
  "version": "1",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "permissions":["clipboardWrite"],
    "manifest_version": 2
}

popup.html

入力ボックスとボタンを備えた簡単なブラウザ アクション HTML ファイル

<html>

    <head>
        <script src="popup.js"></script>
    </head>

    <body>
        <input type="text" id="text" placeHolder="Enter Text To Copy"></input>
        <button id="copy">Copy</button>
    </body>

</html>

popup.js

<input>コンテンツをクリップボードにコピーします

function copy() {

    //Get Input Element
    var copyDiv = document.getElementById('text');

    //Give the text element focus
    copyDiv.focus();

    //Select all content
    document.execCommand('SelectAll');

    //Copy Content
    document.execCommand("Copy", false, null);
}

//Add Event Listeners to Button Click
document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("copy").onclick = copy;
});

また

function copy(){

    //Get Input Element
    document.getElementById("text").select();

    //Copy Content
    document.execCommand("Copy", false, null);
}

//Add Event Listeners to Button Click
document.addEventListener("DOMContentLoaded", function () {
    document.getElementById("copy").onclick = copy;
});
于 2012-12-16T07:15:07.457 に答える
2

使用例:

copyStringToClipboard("abc123");

    function copyStringToClipboard (str) {
      // Create new element
      var el = document.createElement('textarea');
      // Set value (string to be copied)
      el.value = str;
      // Set non-editable to avoid focus and move outside of view
      el.setAttribute('readonly', '');
      el.style = {position: 'absolute', left: '-9999px'};
      document.body.appendChild(el);
      // Select text inside element
      el.select();
      // Copy text to clipboard
      document.execCommand('copy');
      // Remove temporary element
      document.body.removeChild(el);
    }
于 2018-12-14T16:35:50.490 に答える
1

コンテンツ スクリプトからコピーしようとしているようです。this answerからのjoelptとJeff Granの回答を基に、コンテンツ スクリプトから任意のテキストをコピーする方法を次に示します。

"permissions": [
    "clipboardWrite",...

main.js または任意のバックグラウンド スクリプトで:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
  var copyFrom = document.createElement("textarea");
  copyFrom.textContent = request.text;
  var body = document.getElementsByTagName('body')[0];
  body.appendChild(copyFrom);
  copyFrom.select();
  document.execCommand('copy');
  body.removeChild(copyFrom);
})

コンテンツ スクリプトから:

chrome.runtime.sendMessage({text:textToCopy});
于 2015-12-04T21:16:00.293 に答える