Why is document.execCommand(“paste”) not working in Google Chrome?

前端 未结 7 1786
广开言路
广开言路 2020-11-27 06:59

I have a problem with my extension. I want to paste data from the clipboard.

So far, I\'ve got this:

function          


        
7条回答
  •  猫巷女王i
    2020-11-27 07:07

    Calling document.execCommand("paste") is not supported by "reasonable" browsers, because of security concerns as it might enable the script to read sensitive data (like passwords) from the clipboard.

    This is the compatibility matrix of document.execCommand("...") concerning clipboard events:

            | "copy" | "paste" | "cut"
    --------+--------+---------+--------
    IE      |   OK   |   OK    |  n/a
    --------+--------+---------+--------
    Edge    |   OK   |   n/a   |  OK
    --------+--------+---------+--------
    Firefox |   OK   |   n/a   |  OK
    --------+--------+---------+--------
    Chrome  |   OK   |   n/a   |  OK
    

    My two cents to this:

    • The behaviour of Edge, Firefox and Chrome is "reasonable" as they prevent pasting/reading data from the clipboard. They do enable cut, as cut is simply a copy followed by a delete.
    • The behaviour of IE makes no sense to me, as it enables the "risky" paste, but does not execute the cut event.

    You can feature detect the possible commands using the document.queryCommandSupported method.

提交回复
热议问题