2011-10-10 2 views
36

का उपयोग कर सभी ब्राउज़रों के लिए क्लिपबोर्ड पर कॉपी करें मैं "क्लिपबोर्ड पर कॉपी करें" सभी ब्राउज़रों पर काम करने की कोशिश कर रहा था लेकिन कोई भाग्य नहीं।जावास्क्रिप्ट

जावास्क्रिप्ट का उपयोग कर रहा हूं और मैं Zero Clipboard का उपयोग नहीं करना चाहता हूं।

कृपया हमें बताएं कि मेरे कोड में क्या गलत है।

आपकी सहायता के लिए सराहना करें।

नीचे कोड (वर्तमान में मेरे कोड IE ब्राउज़र पर केवल काम कर रहा है) है: -

<script type="text/javascript"> 
function copyToClipboard(s) 
{ 
    if(window.clipboardData && clipboardData.setData) 
    { 
     clipboardData.setData("Text", s); 
    } 
    else 
    { 
     // You have to sign the code to enable this or allow the action in about:config by changing 
     user_pref("signed.applets.codebase_principal_support", true); 
     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); 

     var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard); 
     if (!clip) return; 

     // create a transferable 
     var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable); 
     if (!trans) return; 

     // specify the data we wish to handle. Plaintext in this case. 
     trans.addDataFlavor('text/unicode'); 

     // To get the data from the transferable we need two new objects 
     var str = new Object(); 
     var len = new Object(); 

     var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString); 

     var copytext=meintext; 

     str.data=copytext; 

     trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]); 

     var clipid=Components.interfaces.nsIClipboard; 

     if (!clip) return false; 

     clip.setData(trans,null,clipid.kGlobalClipboard);  
    } 
} 
</script> 

<textarea id='testText' rows="10" cols="100">Enter your Sample text</textarea><br /> 
<button onclick="copyToClipboard(document.getElementById('testText').value);" >clipboard</button><br /><br /> 
<textarea rows="10" cols="100">Paste your text here</textarea><br /> 
+0

http://stackoverflow.com/questions/400212/how-to-copy-to-clipboard-in-javascript का डुप्लिकेट? –

+4

जैसा कि मुझे पता है, आप डिफ़ॉल्ट रूप से फ़ायरफ़ॉक्स में क्लिपबोर्ड तक पहुंचने के लिए जेएस का उपयोग नहीं कर सकते हैं। जेएस के बजाए, एकमात्र सार्वभौमिक तरीका फ्लैश है। – Rufus

+0

@ रूफस: लेकिन मेरे मामले में, मुझे फ्लैश के बिना करने की ज़रूरत है। –

उत्तर

5

सुरक्षा कारणों से अधिकांश ब्राउज़र क्लिपबोर्ड को संशोधित करने की अनुमति नहीं है (आईई को छोड़कर, बिल्कुल ...) ।

कॉपी-टू-क्लिपबोर्ड फ़ंक्शन क्रॉस-ब्राउज़र संगत बनाने का एकमात्र तरीका फ्लैश का उपयोग करना है।

+0

को 'कॉपी' ईवेंट का उपयोग करके टेक्स्ट डेटा सेट करना संभव है। यदि मूल कार्यों का उपयोग नहीं किया जाता है तो प्रतिलिपि बनाने के लिए कुछ छिपे हुए पाठ का चयन करके। –

10

यह फ़ायरफ़ॉक्स 3.6.x पर काम करता है और IE:

function copyToClipboardCrossbrowser(s) {   
     s = document.getElementById(s).value;    

     if(window.clipboardData && clipboardData.setData) 
     { 
      clipboardData.setData("Text", s); 
     }   
     else 
     { 
      // You have to sign the code to enable this or allow the action in about:config by changing 
      //user_pref("signed.applets.codebase_principal_support", true); 
      netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); 

      var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard); 
      if (!clip) return; 

      // create a transferable 

      var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable); 
      if (!trans) return; 

      // specify the data we wish to handle. Plaintext in this case. 
      trans.addDataFlavor('text/unicode'); 

      // To get the data from the transferable we need two new objects 
      var str = new Object(); 
      var len = new Object(); 

      var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 

      str.data= s;   

      trans.setTransferData("text/unicode",str, str.data.length * 2); 

      var clipid=Components.interfaces.nsIClipboard;    
      if (!clip) return false; 
      clip.setData(trans,null,clipid.kGlobalClipboard);  
     } 
    } 
+2

वाह! यह काम! लेकिन चीजों का आह्वान करके "घटक" क्लास ['@ mozilla.org/widget/transferable;1'] "के रूप में सफल होते हैं, क्या हम मोज़िला साइट पर जा रहे हैं? –

+0

यह फ़ायरफ़ॉक्स के हाल के संस्करणों में काम नहीं करता है, जहां 'घटक' को बहिष्कृत किया गया है। –

+12

! हमें सभी ब्राउज़रों के लिए सार्वभौमिक कुछ चाहिए !!! , कॉमॉन ब्राउजर ... जहां पर। मैं आपको इस पर लोगों को हरा दूंगा ?! – Oneezy

8

मैं बहुत समय बिताया इस समस्या के समाधान के लिए भी देख। यहां मैंने जो पाया है, वह अब तक है:

यदि आप चाहते हैं कि आपके उपयोगकर्ता बटन पर क्लिक करने और कुछ टेक्स्ट कॉपी करने में सक्षम हों, तो आपको फ़्लैश का उपयोग करना पड़ सकता है।

यदि आप चाहते हैं कि आपके उपयोगकर्ता पृष्ठ पर कहीं भी Ctrl + C दबाएं, लेकिन हमेशा क्लिपबोर्ड पर xyz कॉपी करें, मैंने YUI3 में एक ऑल-जेएस समाधान लिखा है (हालांकि इसे आसानी से अन्य ढांचे, या कच्चे जेएस पर पोर्ट किया जा सकता है यदि आप विशेष रूप से आत्म-घृणित महसूस कर रहे हैं)।

इसमें स्क्रीन से एक टेक्स्टबॉक्स बनाना शामिल है जो उपयोगकर्ता को Ctrl/CMD हिट करता है जैसे ही हाइलाइट किया जाता है। जब वे जल्द ही 'सी' हिट करते हैं, तो वे छिपे हुए पाठ की प्रतिलिपि बनाते हैं। यदि वे 'वी' हिट करते हैं, तो वे पेस्ट इवेंट की आग से पहले एक कंटेनर (आपकी पसंद के) पर रीडायरेक्ट हो जाते हैं।

यह विधि अच्छी तरह से काम कर सकती है, क्योंकि जब आप शरीर में कहीं भी Ctrl/CMD कीडाउन को सुनते हैं, तो 'ए', 'सी' या 'वी' कीडाउन श्रोताओं केवल छिपे हुए टेक्स्ट बॉक्स से संलग्न होते हैं (और नहीं पूरा शरीर)। इसे उपयोगकर्ताओं की अपेक्षाओं को तोड़ने की ज़रूरत नहीं है - अगर आप किसी भी तरह की प्रतिलिपि बनाने के लिए चुने गए नहीं हैं तो आपको केवल छिपे हुए बॉक्स पर रीडायरेक्ट किया जाएगा!

यहाँ क्या मैं अपनी साइट पर काम कर रहा है, लेकिन अद्यतन के लिए http://at.cg/js/clipboard.js जाँच है यदि कोई हो तो:

YUI.add('clipboard', function(Y) { 


// Change this to the id of the text area you would like to always paste in to: 

pasteBox = Y.one('#pasteDIV'); 


// Make a hidden textbox somewhere off the page. 

Y.one('body').append('<input id="copyBox" type="text" name="result" style="position:fixed; top:-20%;" onkeyup="pasteBox.focus()">'); 
copyBox = Y.one('#copyBox'); 


// Key bindings for Ctrl+A, Ctrl+C, Ctrl+V, etc: 

// Catch Ctrl/Window/Apple keydown anywhere on the page. 
Y.on('key', function(e) { 
    copyData(); 
     // Uncomment below alert and remove keyCodes after 'down:' to figure out keyCodes for other buttons. 
     // alert(e.keyCode); 
     // }, 'body', 'down:', Y); 
}, 'body', 'down:91,224,17', Y); 

// Catch V - BUT ONLY WHEN PRESSED IN THE copyBox!!! 
Y.on('key', function(e) { 
    // Oh no! The user wants to paste, but their about to paste into the hidden #copyBox!! 
    // Luckily, pastes happen on keyPress (which is why if you hold down the V you get lots of pastes), and we caught the V on keyDown (before keyPress). 
    // Thus, if we're quick, we can redirect the user to the right box and they can unload their paste into the appropriate container. phew. 
    pasteBox.select(); 
}, '#copyBox', 'down:86', Y); 

// Catch A - BUT ONLY WHEN PRESSED IN THE copyBox!!! 
Y.on('key', function(e) { 
    // User wants to select all - but he/she is in the hidden #copyBox! That wont do.. select the pasteBox instead (which is probably where they wanted to be). 
    pasteBox.select(); 
}, '#copyBox', 'down:65', Y); 



// What to do when keybindings are fired: 

// User has pressed Ctrl/Meta, and is probably about to press A,C or V. If they've got nothing selected, or have selected what you want them to copy, redirect to the hidden copyBox! 
function copyData() { 
    var txt = ''; 
    // props to Sabarinathan Arthanari for sharing with the world how to get the selected text on a page, cheers mate! 
     if (window.getSelection) { txt = window.getSelection(); } 
     else if (document.getSelection) { txt = document.getSelection(); } 
     else if (document.selection) { txt = document.selection.createRange().text; } 
     else alert('Something went wrong and I have no idea why - please contact me with your browser type (Firefox, Safari, etc) and what you tried to copy and I will fix this immediately!'); 

    // If the user has nothing selected after pressing Ctrl/Meta, they might want to copy what you want them to copy. 
     if(txt=='') { 
       copyBox.select(); 
     } 
    // They also might have manually selected what you wanted them to copy! How unnecessary! Maybe now is the time to tell them how silly they are..?! 
     else if (txt == copyBox.get('value')) { 
     alert('This site uses advanced copy/paste technology, possibly from the future.\n \nYou do not need to select things manually - just press Ctrl+C! \n \n(Ctrl+V will always paste to the main box too.)'); 
       copyBox.select(); 
     } else { 
       // They also might have selected something completely different! If so, let them. It's only fair. 
     } 
} 
}); 

आशा किसी और इस उपयोगी पाता है:]

+0

क्या होगा यदि उपयोगकर्ता क्लिपबोर्ड पर कॉपी करने के लिए माउस का उपयोग करता है? Ctrl/Cmd कीस्ट्रोक की आवश्यकता भंगुर लगती है। – Wolverine

-3

मुझे लगता है कि zeroclipboard महान है। यह संस्करण नवीनतम फ्लैश 11: http://www.itjungles.com/javascript/javascript-easy-cross-browser-copy-to-clipboard-solution के साथ काम करता है।

+3

प्रश्नकर्ता ने पहले से ही कहा है कि ज़ीरोक्लिबोर्ड एक गैर स्टार्टर है। – ale

संबंधित मुद्दे