1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 16:12:26 +01:00

new liberator.util.readFromClipboard, so also non-Firefox extensions have access to the clipboard

This commit is contained in:
Martin Stubenschrott
2008-05-05 13:22:55 +00:00
parent 7b7d39cc8d
commit 04004ebb39
7 changed files with 49 additions and 22 deletions

View File

@@ -316,20 +316,20 @@ liberator.Buffer = function () //{{{
"Open (put) a URL based on the current clipboard contents in a new buffer", "Open (put) a URL based on the current clipboard contents in a new buffer",
function () function ()
{ {
liberator.open(readFromClipboard(), liberator.open(liberator.util.readFromClipboard(),
/\bpaste\b/.test(liberator.options["activate"]) ? /\bpaste\b/.test(liberator.options["activate"]) ?
liberator.NEW_BACKGROUND_TAB : liberator.NEW_TAB); liberator.NEW_BACKGROUND_TAB : liberator.NEW_TAB);
}); });
liberator.mappings.add(modes, ["p", "<MiddleMouse>"], liberator.mappings.add(modes, ["p", "<MiddleMouse>"],
"Open (put) a URL based on the current clipboard contents in the current buffer", "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"], liberator.mappings.add(modes, ["P"],
"Open (put) a URL based on the current clipboard contents in a new buffer", "Open (put) a URL based on the current clipboard contents in a new buffer",
function () function ()
{ {
liberator.open(readFromClipboard(), liberator.open(liberator.util.readFromClipboard(),
/\bpaste\b/.test(liberator.options["activate"]) ? /\bpaste\b/.test(liberator.options["activate"]) ?
liberator.NEW_TAB : liberator.NEW_BACKGROUND_TAB); liberator.NEW_TAB : liberator.NEW_BACKGROUND_TAB);
}); });
@@ -350,7 +350,7 @@ liberator.Buffer = function () //{{{
{ {
var sel = liberator.buffer.getCurrentWord(); var sel = liberator.buffer.getCurrentWord();
if (sel) if (sel)
liberator.copyToClipboard(sel, true); liberator.util.copyToClipboard(sel, true);
else else
liberator.beep(); liberator.beep();
}); });

View File

@@ -378,7 +378,7 @@ liberator.Editor = function () //{{{
{ {
var sel = window.content.document.getSelection(); var sel = window.content.document.getSelection();
if (sel) if (sel)
liberator.copyToClipboard(sel, true); liberator.util.copyToClipboard(sel, true);
else else
liberator.beep(); liberator.beep();
} }
@@ -494,14 +494,14 @@ liberator.Editor = function () //{{{
{ {
var elt = window.document.commandDispatcher.focusedElement; var elt = window.document.commandDispatcher.focusedElement;
if (elt.setSelectionRange && readFromClipboard()) if (elt.setSelectionRange && liberator.util.readFromClipboard())
// readFromClipboard would return 'undefined' if not checked // readFromClipboard would return 'undefined' if not checked
// dunno about .setSelectionRange // dunno about .setSelectionRange
{ {
var rangeStart = elt.selectionStart; // caret position var rangeStart = elt.selectionStart; // caret position
var rangeEnd = elt.selectionEnd; var rangeEnd = elt.selectionEnd;
var tempStr1 = elt.value.substring(0, rangeStart); var tempStr1 = elt.value.substring(0, rangeStart);
var tempStr2 = readFromClipboard(); var tempStr2 = liberator.util.readFromClipboard();
var tempStr3 = elt.value.substring(rangeEnd); var tempStr3 = elt.value.substring(rangeEnd);
elt.value = tempStr1 + tempStr2 + tempStr3; elt.value = tempStr1 + tempStr2 + tempStr3;
elt.selectionStart = rangeStart + tempStr2.length; elt.selectionStart = rangeStart + tempStr2.length;

View File

@@ -413,8 +413,8 @@ liberator.Hints = function () //{{{
case "V": liberator.buffer.viewSource(loc, true); break; case "V": liberator.buffer.viewSource(loc, true); break;
case "w": liberator.buffer.followLink(elem, liberator.NEW_WINDOW); break; case "w": liberator.buffer.followLink(elem, liberator.NEW_WINDOW); break;
case "W": liberator.commandline.open(":", "winopen " + loc, liberator.modes.EX); 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.util.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(elem.textContent || "", true)}, timeout + 50); break;
default: default:
liberator.echoerr("INTERNAL ERROR: unknown submode: " + submode); liberator.echoerr("INTERNAL ERROR: unknown submode: " + submode);
} }

View File

@@ -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 an ex command like str=":zoom 300"
execute: function (str, modifiers) execute: function (str, modifiers)
{ {

View File

@@ -935,7 +935,7 @@ liberator.CommandLine = function () //{{{
// copy text to clipboard // copy text to clipboard
case "<C-y>": case "<C-y>":
liberator.copyToClipboard(win.getSelection()); liberator.util.copyToClipboard(win.getSelection());
break; break;
// close the window // close the window

View File

@@ -79,6 +79,16 @@ liberator.util = { //{{{
return "&lt;unknown type&gt;"; return "&lt;unknown type&gt;";
}, },
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) escapeHTML: function (str)
{ {
// XXX: the following code is _much_ slower than a simple .replace() // XXX: the following code is _much_ slower than a simple .replace()
@@ -236,6 +246,34 @@ liberator.util = { //{{{
return string; 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' // takes a string like 'google bla, www.osnews.com'
// and returns an array ['www.google.com/search?q=bla', 'www.osnews.com'] // and returns an array ['www.google.com/search?q=bla', 'www.osnews.com']
stringToURLArray: function (str) stringToURLArray: function (str)

View File

@@ -117,7 +117,7 @@ liberator.config = {
liberator.mappings.add([liberator.modes.NORMAL], liberator.mappings.add([liberator.modes.NORMAL],
["y"], "Yank current location to the clipboard", ["y"], "Yank current location to the clipboard",
function () { liberator.copyToClipboard(liberator.buffer.URL, true); }); function () { liberator.util.copyToClipboard(liberator.buffer.URL, true); });
// opening websites // opening websites
liberator.mappings.add([liberator.modes.NORMAL], liberator.mappings.add([liberator.modes.NORMAL],