我有一个带有按钮的插件,可以打开一个带有几个按钮的面板。当一个按钮被按下时,它应该改变keyword.URL
偏好,但事实并非如此。
这是main.js:
data = require("self").data
var panel = require("panel").Panel({
height: 135,
width: 260,
contentURL: data.url("popup.html"),
contentScriptFile: data.url("clicklisten.js"),
onMessage: function(contentScriptMessage) {
require("preferences-service").set('keyword.URL', contentScriptMessage);
}
//I think the problem is right there, but i have no idea why.
});
var button = require("widget").Widget({
id: "search-engine-button",
label: "Set search engine",
contentURL: data.url("Refresh.ico"),
panel: panel
});
这是 HTML 按钮的示例:
<button class="searchButton" id="addons" _keywordURL="https://addons.mozilla.org/en-US/firefox/search/?q=">Add-ons</button>
这是 clicklisten.js(内容脚本):
// Add event listener
var buttons = document.getElementByClassName("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"));
}
我觉得我已经尝试了我能想到的一切,但我无法让它发挥作用。