From 04004ebb393a1ba2aa02b8bcab5ab7cbb701388b Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Mon, 5 May 2008 13:22:55 +0000 Subject: [PATCH] new liberator.util.readFromClipboard, so also non-Firefox extensions have access to the clipboard --- content/buffer.js | 8 ++++---- content/editor.js | 6 +++--- content/hints.js | 4 ++-- content/liberator.js | 11 ----------- content/ui.js | 2 +- content/util.js | 38 ++++++++++++++++++++++++++++++++++++++ content/vimperator.js | 2 +- 7 files changed, 49 insertions(+), 22 deletions(-) diff --git a/content/buffer.js b/content/buffer.js index 3542f2f9..ebb867f4 100644 --- a/content/buffer.js +++ b/content/buffer.js @@ -316,20 +316,20 @@ liberator.Buffer = function () //{{{ "Open (put) a URL based on the current clipboard contents in a new buffer", function () { - liberator.open(readFromClipboard(), + liberator.open(liberator.util.readFromClipboard(), /\bpaste\b/.test(liberator.options["activate"]) ? liberator.NEW_BACKGROUND_TAB : liberator.NEW_TAB); }); liberator.mappings.add(modes, ["p", ""], "Open (put) a URL based on the current clipboard contents in the current buffer", - function () { liberator.open(readFromClipboard()); }); + function () { liberator.open(liberator.util.readFromClipboard()); }); liberator.mappings.add(modes, ["P"], "Open (put) a URL based on the current clipboard contents in a new buffer", function () { - liberator.open(readFromClipboard(), + liberator.open(liberator.util.readFromClipboard(), /\bpaste\b/.test(liberator.options["activate"]) ? liberator.NEW_TAB : liberator.NEW_BACKGROUND_TAB); }); @@ -350,7 +350,7 @@ liberator.Buffer = function () //{{{ { var sel = liberator.buffer.getCurrentWord(); if (sel) - liberator.copyToClipboard(sel, true); + liberator.util.copyToClipboard(sel, true); else liberator.beep(); }); diff --git a/content/editor.js b/content/editor.js index e7fe2dcb..e43875f4 100644 --- a/content/editor.js +++ b/content/editor.js @@ -378,7 +378,7 @@ liberator.Editor = function () //{{{ { var sel = window.content.document.getSelection(); if (sel) - liberator.copyToClipboard(sel, true); + liberator.util.copyToClipboard(sel, true); else liberator.beep(); } @@ -494,14 +494,14 @@ liberator.Editor = function () //{{{ { var elt = window.document.commandDispatcher.focusedElement; - if (elt.setSelectionRange && readFromClipboard()) + if (elt.setSelectionRange && liberator.util.readFromClipboard()) // readFromClipboard would return 'undefined' if not checked // dunno about .setSelectionRange { var rangeStart = elt.selectionStart; // caret position var rangeEnd = elt.selectionEnd; var tempStr1 = elt.value.substring(0, rangeStart); - var tempStr2 = readFromClipboard(); + var tempStr2 = liberator.util.readFromClipboard(); var tempStr3 = elt.value.substring(rangeEnd); elt.value = tempStr1 + tempStr2 + tempStr3; elt.selectionStart = rangeStart + tempStr2.length; diff --git a/content/hints.js b/content/hints.js index 85cdc904..b7cb2f2b 100644 --- a/content/hints.js +++ b/content/hints.js @@ -413,8 +413,8 @@ liberator.Hints = function () //{{{ case "V": liberator.buffer.viewSource(loc, true); break; case "w": liberator.buffer.followLink(elem, liberator.NEW_WINDOW); break; case "W": liberator.commandline.open(":", "winopen " + loc, liberator.modes.EX); break; - case "y": setTimeout(function(){liberator.copyToClipboard(loc, true)}, timeout + 50); break; - case "Y": setTimeout(function(){liberator.copyToClipboard(elem.textContent || "", true)}, timeout + 50); break; + case "y": setTimeout(function(){liberator.util.copyToClipboard(loc, true)}, timeout + 50); break; + case "Y": setTimeout(function(){liberator.util.copyToClipboard(elem.textContent || "", true)}, timeout + 50); break; default: liberator.echoerr("INTERNAL ERROR: unknown submode: " + submode); } diff --git a/content/liberator.js b/content/liberator.js index cd37b810..0ec2c0a7 100644 --- a/content/liberator.js +++ b/content/liberator.js @@ -463,17 +463,6 @@ const liberator = (function () //{{{ } }, - // XXX? move to liberator.util? - copyToClipboard: function (str, verbose) - { - var clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"] - .getService(Components.interfaces.nsIClipboardHelper); - clipboardHelper.copyString(str); - - if (verbose) - liberator.echo("Yanked " + str, liberator.commandline.FORCE_SINGLELINE); - }, - // Execute an ex command like str=":zoom 300" execute: function (str, modifiers) { diff --git a/content/ui.js b/content/ui.js index da105e54..68d1b461 100644 --- a/content/ui.js +++ b/content/ui.js @@ -935,7 +935,7 @@ liberator.CommandLine = function () //{{{ // copy text to clipboard case "": - liberator.copyToClipboard(win.getSelection()); + liberator.util.copyToClipboard(win.getSelection()); break; // close the window diff --git a/content/util.js b/content/util.js index 9271367c..af6ea7be 100644 --- a/content/util.js +++ b/content/util.js @@ -79,6 +79,16 @@ liberator.util = { //{{{ return "<unknown type>"; }, + copyToClipboard: function (str, verbose) + { + var clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"] + .getService(Components.interfaces.nsIClipboardHelper); + clipboardHelper.copyString(str); + + if (verbose) + liberator.echo("Yanked " + str, liberator.commandline.FORCE_SINGLELINE); + }, + escapeHTML: function (str) { // XXX: the following code is _much_ slower than a simple .replace() @@ -236,6 +246,34 @@ liberator.util = { //{{{ return string; }, + // same as Firefox's readFromClipboard function, but needed for apps like Thunderbird + readFromClipboard: function() + { + var url; + try + { + var clipboard = Components.classes['@mozilla.org/widget/clipboard;1'].getService(Components.interfaces.nsIClipboard); + var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); + trans.addDataFlavor("text/unicode"); + if (clipboard.supportsSelectionClipboard()) + clipboard.getData(trans, clipboard.kSelectionClipboard); + else + clipboard.getData(trans, clipboard.kGlobalClipboard); + + var data = {}; + var dataLen = {}; + trans.getTransferData("text/unicode", data, dataLen); + if (data) + { + data = data.value.QueryInterface(Components.interfaces.nsISupportsString); + url = data.data.substring(0, dataLen.value / 2); + } + } + catch (ex) { } + + return url; + }, + // takes a string like 'google bla, www.osnews.com' // and returns an array ['www.google.com/search?q=bla', 'www.osnews.com'] stringToURLArray: function (str) diff --git a/content/vimperator.js b/content/vimperator.js index 03020112..9589f583 100644 --- a/content/vimperator.js +++ b/content/vimperator.js @@ -117,7 +117,7 @@ liberator.config = { liberator.mappings.add([liberator.modes.NORMAL], ["y"], "Yank current location to the clipboard", - function () { liberator.copyToClipboard(liberator.buffer.URL, true); }); + function () { liberator.util.copyToClipboard(liberator.buffer.URL, true); }); // opening websites liberator.mappings.add([liberator.modes.NORMAL],