【问题标题】:Access selected text within a Hotkey object访问热键对象中的选定文本
【发布时间】:2013-09-22 13:15:58
【问题描述】:

是否可以在Hotkey 的范围内抓取用户选择的文本?
定义在Addon/lib/main.js:

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

错误..

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());

【问题讨论】:

    标签: javascript firefox-addon firefox-addon-sdk hotkeys


    【解决方案1】:

    是的,最简单的方法是使用selection 模块。

    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(),它有一个示例,展示了如何与活动标签进行交互。

    【讨论】:

    • 谢谢! ..不知道sdk/selection
    猜你喜欢
    • 2013-08-29
    • 2021-09-24
    • 2023-03-24
    • 2018-07-15
    • 2016-06-13
    • 2022-11-14
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多