Storing text in the clipboard using Silverlight 2

Original Post at: http://www.jeff.wilcox.name/2008/05/21/clipboard-access/

To provide users the ability to copy permalinks or store other useful information in the clipboard, there aren’t many options for web developers today.  There’s no APIs inside JavaScript to access the clipboard.  Here’s a solution that will at least enable this from your Silverlight 2 app for most of your users.

Internet Explorer-only Clipboard Access

IE provides limited clipboard support in script. Using the interoperability features in Silverlight this data can be accessed.  The user will be prompted to approve the clipboard access using this method.

Open IE Clipboard demo (133k)
Requires Silverlight 2 Beta 1

Here’s the static clipboard method I created to attempt to write to the clipboard.  If the user denies the request, or the clipboard API is not available, an alert informs the user.  A successful write to the clipboard does not result in any visual confirmation.

Clipboard.cs:

using System;   
using System.Windows.Browser;   
namespace ClipboardDemo   
{  
    public static class Clipboard   
    {   
        const string HostNoClipboard = "The clipboard isn't available in the current host.";   
        const string ClipboardFailure = "The text couldn't be copied into the clipboard.";   

        /// <summary>   
        /// Write to the clipboard (Internet Explorer-only)   
        /// </summary>   
        public static void SetText(string text)   
        {   
            // document.window.clipboardData.setData(format, data);   
            var clipboardData = (ScriptObject)HtmlPage.Window.GetProperty("clipboardData");  
            if (clipboardData != null) {   
                bool success = (bool)clipboardData.Invoke("setData", "text", text);   
                if (!success) {   
                    HtmlPage.Window.Alert(ClipboardFailure);   
                }   
            }   
            else {   
                HtmlPage.Window.Alert(HostNoClipboard);   
            }   
        }   
    }   
}  

In the button event handler in my demo app, I simply call the static method to write to the clipboard:

private void Go_Click(object sender, RoutedEventArgs e)   
{   
    Clipboard.SetText(ClipboardText.Text);   
}  

Cross-browser, cross-platform solution

Now, this one’s a little interesting: since Flash does have clipboard access, you can actually use it from the web browser (JavaScript) or even Silverlight 2 (HTML DOM bridge) to enable a cross-browser, cross-platform clipboard copy function.  It’s a hack, but does get the job done.

I think there’s enough space in the world’s hard drives for both Flash and Silverlight on every computer!

Open Cross-browser, Cross-platform Clipboard demo (133k)

Requires Silverlight 2 Beta 1

To do this, I’ll be using the Flash component that ships with the syntaxhighlighter tool created by Alex Gorbatchev.  By simply adding a new Flash embed to the page and referencing his clipboard.swf Flash movie file, the Flash API will then attempt to write the proper data into the clipboard.

The implementation I’ve done here isn’t super robust: if the user doesn’t have Flash, there’s no error message for instance.  If the user’s browser is Internet Explorer, or implements the clipboardData API, then the Flash workaround will not be used.

Here’s the expanded Clipboard.cs.  Do note, I’m hard-coding the clipboard.swf location, so make sure you’re using a valid path on your server.

Clipboard.swf:

Make sure to download this file (here syntaxhighlighter) and store it in the same directory as your application.

Clipboard.cs:

using System;   
using System.Windows.Browser;   
namespace ClipboardDemo   
{   
    public static class Clipboard   
    {   
        const string HostNoClipboard = "The clipboard isn't available in the current host.";   
        const string ClipboardFailure = "The text couldn't be copied into the clipboard.";   
        const string BeforeFlashCopy = "The text will now attempt to be copied...";   
        const string FlashMimeType = "application/x-shockwave-flash";   

        // HARD-CODED!   
        const string ClipboardFlashMovie = "clipboard.swf";   

        /// <summary>   
        /// Write to the clipboard (IE and/or Flash)   
        /// </summary>   
        public static void SetText(string text)   
        {   
            // document.window.clipboardData.setData(format, data);   
            var clipboardData = (ScriptObject)HtmlPage.Window.GetProperty("clipboardData");   
            if (clipboardData != null) {   
                bool success = (bool)clipboardData.Invoke("setData", "text", text);   
                if (!success) {   
                    HtmlPage.Window.Alert(ClipboardFailure);   
                }   
            }   
            else {   
                HtmlPage.Window.Alert(BeforeFlashCopy);   
                // Append a Flash embed element with the data encoded  
                string safeText = HttpUtility.UrlEncode(text);   
                var elem = HtmlPage.Document.CreateElement("div");   
                HtmlPage.Document.Body.AppendChild(elem);   
                elem.SetProperty("innerHTML", "<embed src=""" +   
                    ClipboardFlashMovie + """ " +   
                    "FlashVars=""clipboard=" + safeText + """ width=""0"" " +   
                    "height=""0"" type=""" + FlashMimeType + """></embed>");   
            }   
        }   
    }   
}  

Hope this helps!

转载于:https://www.cnblogs.com/winkingzhang/archive/2008/05/22/1204577.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值