javascript - Copy to clipboard on firefox and google chrome -


since have looked around , not find nice solution copying text on firefox or chrome clipboard. however, have tried codes provide firefox in developer site, still not work , there 1 errror permission denied. here code tried last minute.

var copytext = "text copy";   var str      = components.classes["@mozilla.org/supports-string;1"].createinstance(components.interfaces.nsisupportsstring);   str.data     = copytext;  

does have solution deal this? appreciate sharing. thanks.

i found next solution:

on key down handler create "pre" tag. set content copy tag. make selection on tag , return true in handler. call standard handler of chrome , copied selected text.

and if u need u may set timeout function restoring previous selection. implementantions on mootools:

   function enybyclipboard() {         this.saveselection = false;         this.callback = false;         this.pastedtext = false;          this.restoreselection = function () {             if (this.saveselection) {                 window.getselection().removeallranges();                         (var = 0; < this.saveselection.length; i++) {                     window.getselection().addrange(this.saveselection[i]);                 }                 this.saveselection = false;             }         };          this.copytext = function (text) {             var div = $('special_copy');             if (!div) {                 div = new element('pre', {'id' : 'special_copy', 'style': 'opacity: 0;position: absolute;top: -10000px;right: 0;'});                 div.injectinside(document.body);             }             div.set('text', text);             if (document.createrange) {                 var rng = document.createrange();                 rng.selectnodecontents(div);                 this.saveselection = [];                 var selection = window.getselection();                 (var = 0; < selection.rangecount; i++) {                     this.saveselection[i] = selection.getrangeat(i);                 }                 window.getselection().removeallranges();                 window.getselection().addrange(rng);                 settimeout(this.restoreselection.bind(this), 100);             } else                 return alert('copy not work. :(');         };          this.getpastedtext = function () {             if (!this.pastedtext)                  alert('nothing paste. :(');             return this.pastedtext;         };          this.pastetext = function (callback) {             var div = $('special_paste');             if (!div) {                 div = new element('textarea', {'id' : 'special_paste', 'style': 'opacity: 0;position: absolute;top: -10000px;right: 0;'});                 div.injectinside(document.body);                 div.addevent('keyup', function() {                     if (this.callback) {                         this.pastedtext = $('special_paste').get('value');                         this.callback.call(this.pastedtext);                         this.callback = false;                         this.pastedtext = false;                         settimeout(this.restoreselection.bind(this), 100);                     }                 }.bind(this));             }             div.set('value', '');             if (document.createrange) {                 var rng = document.createrange();                 rng.selectnodecontents(div);                 this.saveselection = [];                 var selection = window.getselection();                 (var = 0; < selection.rangecount; i++) {                     this.saveselection[i] = selection.getrangeat(i);                 }                 window.getselection().removeallranges();                 window.getselection().addrange(rng);                 div.focus();                 this.callback = callback;             } else                 return alert('fail paste. :(');         };     } 

usage:

enyby_clip = new enybyclipboard(); //init   enyby_clip.copytext('some_text'); // place in ctrl+c handler , return true;  enyby_clip.pastetext(function callback(pasted_text) {         alert(pasted_text); }); // place in ctrl+v handler , return true; 

on paste create textarea , work same.

sorry bad english - not native language.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -