使用Mozilla Firefox Add-on SDK获取工具栏搜索框的内容 [英] Getting the contents of the toolbar search box using the Mozilla Firefox Add-on SDK
问题描述
document.getAnonymousElementByAttribute(this,anonid,searchbar-textbox);
但是这给出了一个文档未定义错误,可能是因为Firefox没有想法'searchbar-textbox'是什么,这不在外接程序的范围内(在另一个文档中)。我相对较新的插件开发,所以可能有一个相当直接的方法来做到这一点,只是这个解决方案是我不知道的。谢谢。您的主模块(和其他lib / modules)没有附加任何文档。您需要首先使用一些低级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(搜索文本:+ win.document.getElementById(searchbar)。value);
}
});
I am developing a Firefox addon and I was wondering how to get the contents of the search box in the toolbar using the Mozila Addon SDK? I finally found the chrome URL where it resides (at least I think: chrome://browser/content/search/...), but I’m still a little unsure as to how to reference this to get the contents of the search box into my addon. I tried: document.getAnonymousElementByAttribute(this, "anonid", "searchbar-textbox");
but this gives a "document is not defined" error, probably because Firefox has no idea what ‘searchbar-textbox’ is and this is outside the scope of the addon (in a different ‘document’). I’m relatively new to addon development, so there’s probably a fairly straight forward way to do this, it is just that this solution is unknown to me. Thanks.
Your "main" module (and other lib/ modules) do not have any document attached. You need to first use some low-level API such as the window/utils
.getMostRecentBrowserWindow()
function to obtain the DOMWindow for the active browser window. After that it's just getting the #searchbar
element and checking the .value
property (exposed through XBL).
Complete example:
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);
}
});
这篇关于使用Mozilla Firefox Add-on SDK获取工具栏搜索框的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!