<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>random($foo) &#187; webdev</title>
	<atom:link href="http://randomfoo.net/tag/webdev/feed" rel="self" type="application/rss+xml" />
	<link>http://randomfoo.net</link>
	<description>blog blog blog</description>
	<lastBuildDate>Sun, 15 Jan 2012 20:26:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Clipboard Copying in Flash 10</title>
		<link>http://randomfoo.net/2009/01/13/clipboard-copying-in-flash-10</link>
		<comments>http://randomfoo.net/2009/01/13/clipboard-copying-in-flash-10#comments</comments>
		<pubDate>Wed, 14 Jan 2009 05:06:09 +0000</pubDate>
		<dc:creator>lhl</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[clipboard]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://randomfoo.net/?p=5155</guid>
		<description><![CDATA[One of the things I noticed about the DevFormatter plugin I&#8217;m using was that the clipboard copying code was no longer working. This apparently is because of the new security upgrades in Flash 10 which now have additional user-initiated action (UIA) requirements for various functions, including System.setClipboard(). While inconvenient, especially since it&#8217;s somewhat commonly used, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I noticed about the <a href="http://wordpress.org/extend/plugins/devformatter/">DevFormatter</a> plugin I&#8217;m using was that the clipboard copying code was no longer working.  This apparently is because of the <a href="http://www.adobe.com/devnet/flashplayer/articles/fplayer10_uia_requirements.html">new security upgrades in Flash 10</a> which now have additional user-initiated action (UIA) requirements for various functions, including <tt>System.setClipboard()</tt>.</p>
<p>While inconvenient, especially since it&#8217;s <a href="http://www.jeffothy.com/weblog/clipboard-copy">somewhat commonly used</a>, it was a necessary change due to some <a href="http://it.slashdot.org/article.pl?sid=08/08/20/0029220">high-profile clickjacking attacks</a>, and well, a good idea regardless, when you think about it.</p>
<p>Surprisingly, months after Flash 10&#8242;s release, it seems that neither the <a href="http://wordpress.org/extend/plugins/tags/geshi">WP plugins I looked at</a>, nor the most popular <a href="http://code.google.com/p/syntaxhighlighter/">general syntax highlighter script</a> seem to have <a href="http://code.google.com/p/syntaxhighlighter/issues/detail?id=135">fixed their clipboard functionality</a>, as the workaround isn&#8217;t too onerous. Instead of the having a JS triggered Flash copy, just reverse it with a Flash button calling a JS function &#8211; not quite as elegant since you&#8217;ll need as many Flash buttons as you want copy triggers, but not too onerous.  It&#8217;s been a while since I&#8217;ve done any Flash work, but luckily, it didn&#8217;t take very long at all.</p>
<p>Since this might prove useful to others, I&#8217;ve done this in AS 2.0 for more compatibility and made the package available here under a GPLv3 license: <a rel="attachment wp-att-5157" href="http://randomfoo.net/2009/01/13/clipboard-copying-in-flash-10/clipboard_copy">clipboard_copy</a>.</p>
<p>Of course, if you want to create your own unencumbered version, the code is easy enough to create yourself.  The JS call looks something like:</p>
<p><code lang="javascript[lines]">function clipboard_copy(id) {<br />
  return document.getElementById(id).innerHTML;<br />
  // or instead of innerHTML, you can get plain text:<br />
  // [((document.all)? "innerText" : "textContent")]<br />
}</code></p>
<p>Note, you can have the clipboard JS function act on a selection if you&#8217;d like, but for my purposes (integration w/ code blocks, getting the text was better).</p>
<p>The Flash is similarly simple.  Here&#8217;s the <a href="http://www.actionscriptversions.com/evolution/events/">AS 2.0 event attachment</a> and <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html">ExternalInterface</a>:</p>
<p><code lang="actionscript[lines]">// AS 2.0 Event<br />
copyButton.addEventListener("click",click);<br />
function click(event:Object):Void {<br />
  var item = "testid";<br />
  var jsFunction:String = "clipboard_copy";<br />
  var returnValue:String = ExternalInterface.call(jsFunction, item).toString();<br />
  System.setClipboard(returnValue);<br />
}</code></p>
<p>Easy peezy.</p>
<p>A couple of notes:</p>
<ul>
<li>If you&#8217;re testing, you&#8217;ll want to run from a web server, not the file system, otherwise you&#8217;ll get sandbox errors</li>
<li>Regular cross-domain rules also apply of course</li>
<li>I used the Button Component for my version, which is admittedly a bit fugly.  You could in theory have a text-only Flash link that you subsequently styled w/ JS (i.e., to match font-family and font-size), but I&#8217;ll leave that as an exercise for the reader</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://randomfoo.net/2009/01/13/clipboard-copying-in-flash-10/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

