the proper use of execcommand(“paste”) in a chrome extension

后端 未结 3 1722
南方客
南方客 2020-11-29 23:21

I\'m trying to paste clipboard data into a textarea using execcommand(\"paste\") with a chome extension, but i cannot seem to get it to work. permissions are s

相关标签:
3条回答
  • 2020-11-30 00:04

    This is too long for a comment on Alasdair's excellent response, so I'm creating another answer. Alasdair's answer is excellent and worked great for me, but as a newcomer to Chrome extensions it still took me a while to get it working. For anyone in a similar position, here is an expansion on his answer.

    Background/event pages are able to interact with the system clipboard, provided you've requested the appropriate permissions. They are not able to interact with the DOM of pages the user has loaded. Content scripts cannot interact with the system clipboard, but they can interact with the DOM of pages the user has loaded. Take a look at the explanation of the extension architecture for a good overview of all this.

    This essentially means you need to do the copy/paste actions from the system clipboard in your event/background pages, which is what Alasdair has outlined above. Any pasting or copying from the DOM of the page the user is viewing has to occur in your content script. The two scripts are able to communicate pretty easily with message passing.

    I have an extension whose only purpose is to paste, and the architecture came largely from this post. If you want to see the above technique in practice, take a look at the code. In particular, background.html, background.js, and contentscript.js.

    If you're in a real hurry, here is a gist.

    0 讨论(0)
  • Clipboard functionality is a key part of my extension so I've seen all the normal problems. On my background page I expose a copy and a paste function and the page itself contains <textarea id="sandbox"></textarea>;

    function copy(str) {
        var sandbox = $('#sandbox').val(str).select();
        document.execCommand('copy');
        sandbox.val('');
    }
    
    function paste() {
        var result = '',
            sandbox = $('#sandbox').val('').select();
        if (document.execCommand('paste')) {
            result = sandbox.val();
        }
        sandbox.val('');
        return result;
    }
    

    I'm using jQuery for simplicity but you get the idea. Now any time I want to use the clipboard functionality I simply call the relevant function. Other pages in my extension can access this API via chrome.extension.getBackgroundPage() but you can also use chrome.runtime.getBackgroundPage(callback) if your background page is an event page.

    I'm not sure if this is best practice or if such a thing even exists for this functionality yet but this definitely works for me and is very clean.

    0 讨论(0)
  • 2020-11-30 00:14
      function PasteString() {
        var editor = document.getElementById("TemplateSubPage");
        editor.focus();
      //  editor.select();
        document.execCommand('Paste');
    }
    
    function CopyString() {
        var input = document.getElementById("TemplateSubPage");
        input.focus();
       // input..select();
        document.execCommand('Copy');
        if (document.selection) {
            document.selection.empty();
        } else if (window.getSelection) {
            window.getSelection().removeAllRanges();
        }
    }
    

    Hope this will work for you

    0 讨论(0)
提交回复
热议问题