【问题标题】:Getting the contents of the toolbar search box using the Mozilla Firefox Add-on SDK使用 Mozilla Firefox Add-on SDK 获取工具栏搜索框的内容
【发布时间】:2013-10-20 14:20:39
【问题描述】:
我正在开发一个 Firefox 插件,我想知道如何使用 Mozila 插件 SDK 获取工具栏中的搜索框的内容?我终于找到了它所在的 chrome URL(至少我认为:chrome://browser/content/search/...),但我仍然有点不确定如何引用它来获取搜索框进入我的插件。我试过:document.getAnonymousElementByAttribute(this, "anonid", "searchbar-textbox"); 但这给出了“文档未定义”错误,可能是因为 Firefox 不知道“搜索栏文本框”是什么,这超出了插件的范围(在不同的“文档”中)。我对插件开发比较陌生,所以可能有一种相当直接的方法可以做到这一点,只是我不知道这个解决方案。谢谢。
【问题讨论】:
标签:
javascript
firefox
dom
firefox-addon
xul
【解决方案1】:
您的“主”模块(和其他 lib/ 模块)没有附加任何文档。您需要首先使用一些低级 API,例如 window/utils .getMostRecentBrowserWindow() 函数来获取活动浏览器窗口的 DOMWindow。之后它只是获取#searchbar 元素并检查.value 属性(通过XBL 公开)。
完整示例:
const {getMostRecentBrowserWindow} = require("window/utils");
require("sdk/widget").Widget({
id: "log-search-field",
label: "Log Search Field",
contentURL: "http://www.mozilla.org/favicon.ico",
onClick: function() {
let win = getMostRecentBrowserWindow();
console.error("Search text: " + win.document.getElementById("searchbar").value);
}
});