Domanda

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());
È stato utile?

Soluzione

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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top