Check running of execCommand in firefox ('Copy' command)

Asked

Viewed 130 times

3

I’m having trouble checking if firefox ran the.execCommand element command ('Copy', false, null).

This check is being done correctly in Chrome (which does not allow) and IE (which allows).

Could you help me, please?

Follows the relevant part of the Code:

editorDoc.body.focus();
editorDoc.execCommand ('SelectAll', false, null);
if( ! editorDoc.execCommand ('Copy', false, null) )
    alert('As opções de segurança do seu navegador impedem a cópia do conteúdo de maneira automática. Por favor, utilize [CTRL]+[C] em seu teclado.');

You can test the current operation here: https://tmwxd.com.br/minify.php

Obs Importante: In the firefox the text is selected in Chrome the text is selected and the alert is issued, and IE 11 text is selected and copied to clipboard.

  • 1

    There are other ways to do this, see here.

  • Thanks, but I needed a solution for execCommand , since it is a text editor. What’s more, it’s only firefox that "copy" doesn’t work.

  • 1

    I’ve been reading the MDN documentation and so that the execCommand function, it is necessary to use the property designMode with the value On. You already did that?

  • Yes, @Qmechanic73... I am and execCommand is working, as mentioned above. The problem is that FF does not allow auto-scoping. So far td well, because Chrome tb does not allow it. But execCommand should turn false when it does not execute, to make a message possible. Chrome does that, but FF doesn’t.

1 answer

2


Perhaps the mode you are using to verify the execution of a command is not the most appropriate.

According to the page HTML Editing Apis, section Supported commands:

Some commands will be supported on a particular user agent, and others not. All commands defined in this specification must be supported, except optionally the command copy, the command cut, and/or the command Paste. [...]

A command that does absolutely nothing on a user agent in in such a way that execCommand() never has any effect and queryCommandEnabled(), queryCommandIndeterm(), queryCommandState() and queryCommandValue() each returns the same value all the time, should not be supported.

In a particular user agent, all commands must be consistently supported or not. [...] However, agents can treat the same command as supported by some pages and others not, for example if the command is only supported for certain sources, for security reasons.

The recommendation is to use the queryCommandSupported, that returns a value boolean which will indicate if the command is supported or not, if supported, use the method queryCommandEnabled which will also return a value boolean which will indicate whether the command can be executed successfully using execCommand.

You can do something like this:

editorDoc = ...
if(editorDoc.queryCommandSupported('copy')){
    if(editorDoc.queryCommandEnabled('copy')){
        editorDoc.execCommand('copy', false, null);
    } // else {..} não está habilitado
} // else {..} não é suportado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.