0

Hi I'm trying to capture the buffer in cut copy paste. I can get the paste, but the copy and cut do not appear to work.

Env: Windows 10 x64: Google Chrome is up to date Version 65.0.3325.181 (Official Build) (64-bit)

index.html

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> 

<html xmlns='http://www.w3.org/1999/xhtml' xmlns:v='urn:schemas-microsoft-com:vml'> 

<body>

<hr/>Clipboard TEST<br/>
<textarea id='idCut'
        rows='8'
        cols='32'>
Cut something from here 
</textarea>

</body>
</html>

manifest.json

{
  "name": "Chrome Extension",
  "description": "Stuff",
  "version": "2.0",

  "permissions": [
    "nativeMessaging",
        "notifications",
        "activeTab",
        "tabs",
        "webRequest",
        "webRequestBlocking",
        "downloads",
        "clipboardWrite",
        "clipboardRead",
        "*://*/*"
    ],

  "content_scripts": [
    {
        "matches": ["http://*/*", "https://*/*"],
        "js": ["content.js"],
        "run_at": "document_end"
    }],

  "browser_action": {
    "default_title": "Make this page do Stuff"
  },
  "manifest_version": 2
}

content.js

function onCut(e)
{
    console.log( 'onCut ENTER' );

    var buf = e.clipboardData.getData('text/plain');

    console.log( 'onCut  EXIT ' + buf );
}

function onCopy(e)
{
    console.log( 'onCopy ENTER' );

    var buf = e.clipboardData.getData('text/plain');

    console.log( 'onCopy  EXIT ' + buf );
}

function onPaste(e)
{
    console.log( 'onPaste ENTER' );

    var buf = e.clipboardData.getData('text/plain');

    console.log( 'onPaste  EXIT ' + buf );
}

document.addEventListener('cut',onCut,true); 
document.addEventListener('copy',onCopy,true); 
document.addEventListener('paste',onPaste,true); 

I then select "something" from the text area and do ctrl X, ctrl C, ctrl V which results in:

content.js:8 onCut ENTER
content.js:12 onCut  EXIT 
content.js:17 onCopy ENTER
content.js:21 onCopy  EXIT 
content.js:26 onPaste ENTER
content.js:30 onPaste  EXIT something

So, Paste gives me the buffer, but the others do not.

My question is why aren't cut and copy showing me the buffer but paste is?

I've read that I need to use background.js and exec the op into a temp dom element like a div, and then native message it out, but if that is true, why is paste working? It seems like the security / impl is not consistent.

Is there a reason this is the way it is?
Or am I missing something?

I just added the html and manifest... Sorry, it didn't seem like it was needed because paste was working. I believe that makes it complete?

Thanks

chup
  • 69
  • 1
  • 7
  • If you provide a working code snippet you most likely will get a proper answer, as of now it is difficult to _see_ what goes wrong. – Asons Mar 29 '18 at 11:15
  • I have 3 functions for the 3 events. Is more code needed? Sorry, not sure what else to provide – chup Mar 29 '18 at 13:32
  • Then you might want to read this: https://stackoverflow.com/help/mcve – Asons Mar 29 '18 at 13:33
  • The clipbord is not easily, cross browser, accessible. Here are some more info: https://w3c.github.io/clipboard-apis/ ... https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript – Asons Mar 29 '18 at 17:54
  • I'm not trying to do anything cross browser. I am only trying to find an explanation / documentation for the results I'm seeing, or acknowledgement of a potential bug. Thanks – chup Mar 29 '18 at 21:09

0 Answers0