unicode: copy glyph to clipboard on cell click
[sheet.git] / clipboard.js
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;
+}
+