Firefox Addon Builder首选项 - 服务不适合我 - 为什么? [英] Firefox Addon Builder preferences-services not working for me--Why?
问题描述
我试图在Firefox附加组件生成器中创建一个按钮,该按钮将更改搜索栏使用的网址。要更改URL,我有一个面板显示按钮被点击的时间,并且面板中有一系列按钮。
在第二个 .js
文件中(不是 main.js
)我有一些函数应该使用 preferences-service
模块更改 keyword.url
函数。
以下是我用来执行此操作的代码:
function changekeywordurl(url){
alert('Search Engine Changed');
require(preferences-service)。set('keyword.url',url);
}
面板的HTML为:
< button onclick =changekeywordurl('http://search.yahoo.com/search?p=')> Yahoo< / button>
对不起,如果我犯了一个非常愚蠢的错误;这是我的第一次尝试和附加组件。
您的扩展程序加载的HTML页面没有任何特殊权限 - 它们就像普通的网页一样。特别是, require()
函数在此上下文中未定义。您需要做的是将内容脚本加载到面板中,因为内容脚本可以反馈给扩展。此内容脚本可能如下所示:
//在内容脚本中添加事件侦听器,页面不能看到内容脚本
//函数
var buttons = document.getElementsByClassName(searchButton);
for(var i = 0; i< buttons.length; i ++)
buttons [i] .addEventListener(click,changekeywordurl,false);
//如果点击一个按钮,发送消息给扩展
function changekeywordurl(event)
{
var button = event.target;
self.postMessage(button.getAttribute(_ keywordURL));
}
页面中的按钮如下所示:
< button _keywordURL =http://search.yahoo.com/search?p=><< ; /按钮>
然后,您的扩展程序可以侦听来自内容脚本的消息并使用必要的SDK模块。
I am trying to make a button in the Firefox Add-on Builder that will change the URL that the searchbar uses. To change the URL, I have a panel that shows when the button is clicked, and in the panel is a series of buttons.
In a second .js
file (not the main.js
) I have the functions that are supposed to change the keyword.url
function using the preferences-service
module.
Here is the code I am using to do this:
function changekeywordurl(url) {
alert('Search Engine Changed');
require("preferences-service").set('keyword.url', url);
}
and the HTML for the panel is:
<button onclick="changekeywordurl('http://search.yahoo.com/search?p=')">Yahoo</button>
Sorry if I am making a really dumb mistake; this is my first attempt and an add-on.
The HTML pages loaded by your extension don't have any special privileges - they are just like regular web pages. In particular, the function require()
isn't defined in this context. What you need to do is to load a content script into the panel because a content script can communicate back to the extension. This content script could look like this:
// Add event listener in the content script, the page cannot "see" content script
// functions
var buttons = document.getElementsByClassName("searchButton");
for (var i = 0; i < buttons.length; i++)
buttons[i].addEventListener("click", changekeywordurl, false);
// Send a message to the extension if a button is clicked
function changekeywordurl(event)
{
var button = event.target;
self.postMessage(button.getAttribute("_keywordURL"));
}
And the button in the page would look like this:
<button _keywordURL="http://search.yahoo.com/search?p="></button>
Then your extension can listen to messages from the content script and use the necessary SDK modules.
这篇关于Firefox Addon Builder首选项 - 服务不适合我 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!