访问热键对象中的选定文本 [英] Access selected text within a Hotkey object

查看:170
本文介绍了访问热键对象中的选定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在 Hotkey 的范围内抓取用户所选的文本吗?

定义在 Addon / lib / main.js

Is it possible to grab user's selected text within Hotkey's scope?
Defined in Addon/lib/main.js:

var showHotKey = Hotkey({
    combo: "accel-f1",
    onPress: function() {
        Addon.saveText(window.getSelection().toString());
    }
});

错误..

Error ..

error: Addon: An exception occurred.
ReferenceError: window is not defined
resource://org/Addon/lib/main.js 14
Traceback (most recent call last):
File "resource://gre/modules/commonjs/sdk/keyboard/observer.js", line 39, in handleEvent
    this._emit(event.type, event, event.target.ownerDocument.defaultView);
File "resource://gre/modules/commonjs/sdk/deprecated/events.js", line 123, in _emit
    return this._emitOnObject.apply(this, args);
File "resource://gre/modules/commonjs/sdk/deprecated/events.js", line 153, in _emitOnObject
    listener.apply(targetObj, params);
File "resource://gre/modules/commonjs/sdk/keyboard/hotkeys.js", line 103, in onKeypress
    hotkey();
File "resource://org/Addon/lib/main.js", line 14, in showHotKey<.onPress
    Addon.saveText(window.getSelection().toString());


推荐答案

是的,最简单的方法是使用< a href =https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/selection.html =nofollow> selection 模块。

Yes, the easiest way would be to use the selection module.

var { Hotkey } = require("sdk/hotkeys");
var selection = require("sdk/selection");

var showHotKey = Hotkey({
    combo: "accel-f1",
    onPress: function() {
        console.error(selection.text);
    }
});

如果您需要更复杂的内容,请查看 tabs.activeTab.attach() ,其中有一个例子显示了如何与一般的活动标签交互。

If you require something more complex, have a look at tabs.activeTab.attach(), which has an example showing how to interact with the active tab in general.

这篇关于访问热键对象中的选定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆