如何使用Firefox Add-On SDK获取所选文本?

时间:2022-05-19 07:23:04

I'm trying to create a Firefox add-on using the online Add-On SDK.

我正在尝试使用在线附加组件SDK创建Firefox附加组件。

I'm starting with something simple - I want to add a toolbar button that reads the current selected text.

我从一些简单的东西开始 - 我想添加一个工具栏按钮来读取当前选定的文本。

The documentation for the Selection object makes this looks simple enough:

Selection对象的文档使这看起来很简单:

var selection = require("selection");
if (selection.text)
  console.log(selection.text);

This doesn't seem to work for me, I just get null.

这似乎不适合我,我只是得到null。

Here's my complete code:

这是我的完整代码:

var selection = require("selection");

require("widget").Widget({
    id: "widgetID1",
    label: "Test Mozilla Widget",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function(event) {
        console.log('selection.text = ' + selection.text);
    }
});

I've also tried to create the selection object inside the onClick even, with the same effect.
I am able to use the select event to get notified on new selections, so I guess I can use that instead (and keep the value), but I wonder why the above code isn't working... What am I doing wrong?

我也尝试在onClick中创建选择对象,效果相同。我可以使用select事件来获得有关新选择的通知,所以我想我可以使用它来代替(并保留值),但我想知道为什么上面的代码不起作用...我做错了什么?

2 个解决方案

#1


8  

The selection variable as defined will only have the selected text as long as it is in focus. Clicking on the widget icon takes focus away from the selected text, so it sees no text selected.

定义的选择变量只有选中的文本才会有焦点。单击窗口小部件图标会使焦点远离所选文本,因此它不会选择任何文本。

Thats why it works when used inside the listener function.

这就是为什么它在侦听器函数中使用时有效。

To confirm this, I tried logging its value when a toolbar button is pressed (using the toolbarbutton module), and it works. Pressing a toolbar button (presumably) does not steal focus.

为了确认这一点,我尝试在按下工具栏按钮时(使用toolbarbutton模块)记录其值,并且它可以正常工作。按下工具栏按钮(大概)不会窃取焦点。

Here's the code, and you can test it online too:

这是代码,您也可以在线测试:

var selection = require("selection");

var tbb = require("toolbarbutton").ToolbarButton({
    id: "test",
    label: "test",
    image: "http://www.mozilla.org/favicon.ico",
    onCommand: function(event) {
      console.log('selection = ' + JSON.stringify(selection)); // works!
    }
});

#2


2  

Here is a solution using the select event:

以下是使用select事件的解决方案:

var selection = require("selection");

var selectedText = '';

function selectionChanged(event){
    //todo: check for selection.isContiguous
    selectedText = selection.text;
}

selection.on('select', selectionChanged);

require("widget").Widget({
    id: "widgetID1",
    label: "Test Mozilla Widget",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function(event) {
        console.log('Selection: ' + selectedText);
    }
});

#1


8  

The selection variable as defined will only have the selected text as long as it is in focus. Clicking on the widget icon takes focus away from the selected text, so it sees no text selected.

定义的选择变量只有选中的文本才会有焦点。单击窗口小部件图标会使焦点远离所选文本,因此它不会选择任何文本。

Thats why it works when used inside the listener function.

这就是为什么它在侦听器函数中使用时有效。

To confirm this, I tried logging its value when a toolbar button is pressed (using the toolbarbutton module), and it works. Pressing a toolbar button (presumably) does not steal focus.

为了确认这一点,我尝试在按下工具栏按钮时(使用toolbarbutton模块)记录其值,并且它可以正常工作。按下工具栏按钮(大概)不会窃取焦点。

Here's the code, and you can test it online too:

这是代码,您也可以在线测试:

var selection = require("selection");

var tbb = require("toolbarbutton").ToolbarButton({
    id: "test",
    label: "test",
    image: "http://www.mozilla.org/favicon.ico",
    onCommand: function(event) {
      console.log('selection = ' + JSON.stringify(selection)); // works!
    }
});

#2


2  

Here is a solution using the select event:

以下是使用select事件的解决方案:

var selection = require("selection");

var selectedText = '';

function selectionChanged(event){
    //todo: check for selection.isContiguous
    selectedText = selection.text;
}

selection.on('select', selectionChanged);

require("widget").Widget({
    id: "widgetID1",
    label: "Test Mozilla Widget",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function(event) {
        console.log('Selection: ' + selectedText);
    }
});