Storing clipboard data into a variable with JavaScript and the Google Chrome API

后端 未结 1 902
一整个雨季
一整个雨季 2020-12-18 15:47

I am wondering if it is possible to store the contents of the clipboard in a string variable.

The following source states that this is not possible using pure JS, b

1条回答
  •  时光说笑
    2020-12-18 16:39

    There was an experimental clipboard api a while ago, I guess they removed it though. You could always paste the contents into a textarea/contenteditable and get it's value:

    function getClipboard() {    
        var el = document.createElement('textarea');
        document.body.appendChild(el);
        el.focus();
        document.execCommand('paste');
        var value = el.value;
        document.body.removeChild(el)
        return value;
    }
    console.log(getClipboard());
    

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