mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-23 08:08:00 +01:00
new liberator.util.readFromClipboard, so also non-Firefox extensions have access to the clipboard
This commit is contained in:
@@ -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", "<MiddleMouse>"],
|
||||
"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();
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -935,7 +935,7 @@ liberator.CommandLine = function () //{{{
|
||||
|
||||
// copy text to clipboard
|
||||
case "<C-y>":
|
||||
liberator.copyToClipboard(win.getSelection());
|
||||
liberator.util.copyToClipboard(win.getSelection());
|
||||
break;
|
||||
|
||||
// close the window
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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],
|
||||
|
||||
Reference in New Issue
Block a user