Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faulty behaviour when browser access is not permitted. #77

Closed
wpp opened this issue Oct 6, 2015 · 7 comments
Closed

Faulty behaviour when browser access is not permitted. #77

wpp opened this issue Oct 6, 2015 · 7 comments
Labels

Comments

@wpp
Copy link

wpp commented Oct 6, 2015

Hello,

This is a bit of an edge-case and might not have a proper solution. But since the project supports IE9+
and I'd qualify this as a "case where you'd like to show some user feedback".

I thought I'd be worth mentioning here.

Steps to reproduce:

  1. Visit http://zenorocha.github.io/clipboard.js/
  2. Click one of the triggers in the examples
  3. Click on deny access, when prompted for clipboard permissions by the browser. (see screenshot)
  4. UI responds as if the copy to clipboard succeeded. (see screenshot)

AFAIK the root cause of the problem is that document.execCommand returns true even if it does not have permissions.

  • OS: Windows 7 (Home Premium)
  • Browser Version: IE 9 (see screenshot)

permission
copied
version

Thanks for a great library with superb documentation.
Cheers.

@zenorocha
Copy link
Owner

Thanks a lot for such detailed explanation @wpp. We'll see what options we have to fix this.

@zenorocha zenorocha added the bug label Oct 8, 2015
@jylertones
Copy link

Found a possible approach - you can use document.execCommand('paste') on another node and see if it pasted:

var didItWork = false;
var checkDiv = document.createElement('textarea');
checkDiv.position = 'absolute';
...
document.body.appendChild(checkDiv);
checkDiv.select();

try {
    document.execCommand('paste');
    didItWork = checkDiv.value == "<coped string>";
} finally {
    document.body.removeChild(checkDiv);
}

@jylertones
Copy link

Also worth noting, I believe this problem happens in both IE9 and IE10. IE11 appears to return the correct value from execCommand.

@zenorocha
Copy link
Owner

Hey guys,

I don't think we have a proper solution for this. For now, we'll describe this as a known bug on the wiki page.

Thanks again for reporting @wpp and @jylertones for trying to help.

@roseforyou
Copy link

//original code:
try { e = document.execCommand(this.action) } catch (n) { e = !1 }

//is OK???
try { e = window.clipboardData ? window.clipboardData.setData('Text', this.selectedText) : document.execCommand(this.action) } catch (n) { e = !1 }

@tmakin
Copy link

tmakin commented Feb 5, 2018

The following error trap is working for me on IE11 inside my success handler

const clipboardData = window['clipboardData'];
if (clipboardData && clipboardData.getData) {
  if (!clipboardData.getData('Text')) {
    // display failure message
    return;
  }
}

@dongyanghe
Copy link

因为之前可能已经有复制内容,会导致误判,所以我加了剪切板的内容对比,这样可能会比较完整:

copyeSuccessFun:  function(e) {
        if (!ClipboardJS.isSupported()) {
            //  copyeErrorFun()
           console.log('复制失败');
            return
        }
        const clipboardData = window['clipboardData'];
        if (clipboardData && clipboardData.getData) {
            if (!clipboardData.getData('Text') || e.text != clipboardData.getData('Text')) {
                //  copyeErrorFun()
                console.log('复制失败');
                return;
            }
        }
        console.log('复制成功');
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants