unicode: copy glyph to clipboard on cell click
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 31 Mar 2010 21:38:34 +0000 (21:38 +0000)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 13 Apr 2010 20:34:51 +0000 (20:34 +0000)
Assume the only reason to click a cell is to copy the glyph contents, so
help the user by copying it automatically, avoiding the need for exact
selection and usually a copy command.

Should support Trident browsers out of the box, and Gecko with effort.
(At last a feature where the IE(>6) solution is vastly superior.)
Tested in MSIE 6.0/7.0 and Iceweasel 3.5.8/3.6.3 with XPCOM enabled.

clipboard.js [new file with mode: 0644]
unicode.plp

diff --git a/clipboard.js b/clipboard.js
new file mode 100644 (file)
index 0000000..cbf43d6
--- /dev/null
@@ -0,0 +1,47 @@
+copyhint = true;
+function clipboardcopy(val) {
+       if (window && window.clipboardData) {
+               // msie
+               return window.clipboardData.setData('text', val);
+       }
+
+       if (netscape && netscape.security) {
+               // request access to XPCOM api
+               try {
+                       netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
+               }
+               catch(e) {
+                       if (copyhint)
+                               alert('Cannot access clipboard.\nSet boolean signed.applets.codebase_principal_support in about:config');
+                       copyhint = false;
+                       return false;
+               }
+
+               // use nsIClipboard interface
+               try {
+                       Components.classes['@mozilla.org/widget/clipboardhelper;1']
+                               .getService(Components.interfaces.nsIClipboardHelper)
+                               .copyString(val);
+                       return true;
+               }
+               catch(e) {
+                       alert('Copy failed');
+                       return false;
+               }
+       }
+}
+
+charmatch = /^U\+([0-9A-F]{4,})/;
+function copycellchars() {
+       var unicode = charmatch.exec(this.title);
+       var str = String.fromCharCode(parseInt(unicode[1], 16));
+       return clipboardcopy(str);
+}
+
+var cells = document.getElementsByTagName('TD');
+for (var i = 0; i < cells.length; i++) {
+       if (!cells[i].title) continue;
+       if (!charmatch.test(cells[i].title)) continue;
+       cells[i].onclick = copycellchars;
+}
+
index 71499530bf12c15f9e5233d1b31314247012a36f..184fcb601519cb51c12c5a92ba1784b1326c6b3e 100644 (file)
@@ -218,3 +218,5 @@ $verbose ? (
        </table>
 </div>
 
        </table>
 </div>
 
+<script type="text/javascript" src="/clipboard.js"></script>
+