2

This is how far I got:

Added permissions to manifest file:

"permissions": ["clipboardRead", "clipboardWrite"]

I read about chrome.experiment.clipboard, but this appears to have been removed in favour of document.execCommand('copy').

I wrote this program to try it out:

main() {
  var copy = new ButtonElement()..text = 'copy';
  var paste = new ButtonElement()..text = 'paste';
  var textarea = new TextAreaElement()..text = 'foo';
  document.body.nodes.addAll([copy, paste, textarea]);      
  copy.onClick.listen((event) => document.execCommand('copy', null, null));
  paste.onClick.listen((event) => document.execCommand('paste', null, null));
}

Ideally, for my application, I'd like to write a function called getClipboardText(), and setClipboardText(String). But first of all I'm trying to just get this basic example to work.

Any ideas on what to try next?

Edit: Updated bug pointed out by amouravski below. Thanks ;)

Fixed - thanks Keith:

class Clipboard {
  static String get text {
    var active = document.activeElement;
    var hidden = new TextAreaElement();
    document.body.append(hidden);
    hidden.focus();
    document.execCommand('paste', null, '');    
    active.focus();
    hidden.remove();
    return hidden.value;
  }

  static set text(String s) {
    var active = document.activeElement;
    var hidden = new TextAreaElement();
    hidden.value = s;
    document.body.append(hidden);    
    hidden.select();
    document.execCommand('copy', null, '');
    active.focus();
    hidden.remove();
  }
}
Greg Lowe
  • 15,430
  • 2
  • 30
  • 33
  • How do you create a permission file? What's the format? Thanks! – user2407014 Mar 05 '14 at 21:44
  • Here's a link to the [docs](http://developer.chrome.com/extensions/manifest). Perhaps post a new question about how to do this, and you will get a better answer. – Greg Lowe Mar 09 '14 at 09:06
  • I am still not clear. Can you post all the files for this example? Including the .html, .dart and the manifest file? Thanks! – user2407014 Mar 11 '14 at 20:58
  • Sorry I don't have them anymore. See if you can find a Dart chrome app example. Try posting a message to dart misc if you can't find this. – Greg Lowe Mar 11 '14 at 21:28

2 Answers2

3

You are not too far off...
In order to copy a user selection from the textarea, the textarea element needs to be focused when the copy command is executed. In your snippet, when you click the copy button, the focus is shifted. Similarly, the target textarea needs to be focused when the paste command is executed.

import 'dart:html';

main() {
  var copy = new ButtonElement()..text = 'copy';
  var paste = new ButtonElement()..text = 'paste';
  var textarea = new TextAreaElement()..text = 'foo';
  var resultTextarea = new TextAreaElement();
  var label = new ParagraphElement()..text = 'paste here too...';
  document.body.nodes.addAll([copy, paste, textarea, resultTextarea, label]);

  // Copy Handler
  void copyText(Event e) {
    //Check whether anything is selected, otherwise select all text.
    if (textarea.selectionStart != null &&
        textarea.selectionStart != textarea.selectionEnd) {
      textarea.focus();
    } else {
      textarea.select();
    }

    document.execCommand('copy', null, "");
  }

  // Paste handler
  void pasteText(Event e) {
    resultTextarea.select(); // Select all text in result text area.
                             // Note: replaces all text. Use focus
                             // to paste at cursor position.
    document.execCommand('paste', null, "");
  }

  // Register button handlers for copy and paste.
  copy.onClick.listen(copyText);
  paste.onClick.listen(pasteText);

  // You can also register a Paste event listener to capture copied text
  // directly from the clipboard.
  document.on['paste'].listen((e) {
    var item = e.clipboardData.items.item(e.clipboardData.items.length-1);

    if(item.type == 'text/plain') {
      item.getAsString().then((clip) => label.text = clip);
    }
  });
}

A couple of notes:

  1. The changes to the document.execCommand method signature are not quite correct. It appears that, at least in Dartium, the last parameter needs to be a string.
  2. You can get pasted text straight from the clipboard by registering an Paste event listener (see bottom of code snippet).
keithschulze
  • 78
  • 1
  • 6
0

The error message is not very descriptive, but what you need to do is give document.execCommand 3 parameters, like so:

... 
  copy.onClick.listen((event) => document.execCommand('copy', null, null));
  paste.onClick.listen((event) => document.execCommand('paste', null, null));
...

See also: http://api.dartlang.org/docs/releases/latest/dart_html/Document.html#execCommand

Juniper Belmont
  • 3,296
  • 2
  • 18
  • 28
  • 1
    Thanks for that. Now for the next hurdle. According to this comment you can only do this from background.js. http://stackoverflow.com/a/15625874/1460491 – Greg Lowe Apr 19 '13 at 07:16
  • I'm not so sure about that, @GregLowe. You should give this a shot in your packaged app. That question was talking about extensions, which work differently from packaged apps. – Juniper Belmont Apr 19 '13 at 07:24
  • Sorry I should have been more clear. Thanks for your fix. That gets rid of the error message (doh - stupid mistake). But the example code still does not copy or paste. I'll update my code example. Thanks again ;) – Greg Lowe Apr 19 '13 at 08:25
  • This (in Dart) used to work for me, but (the only thing I can think of) it does not work in Windows 10. Unfortunately I cannot check on another machine. Has anyone else encountered this? It leaves the Windows clipboard untouched. – Lymp Dec 30 '15 at 15:31