简体   繁体   English

带有面板的cfx Firefox附加组件未打开选项卡

[英]cfx Firefox add-on with panel not opening tab

I would like to make an add-on to open a tab with a specific link based on an input from the panel (which contains a text field). 我想创建一个附加组件,以基于面板(包含文本字段)的输入来打开具有特定链接的选项卡。 The problem is: I never used any programming language before. 问题是:我以前从未使用过任何编程语言。

When the user presses "Return" it should send whatever was into the text field to the main script. 当用户按下“返回”键时,它将文本字段中的所有内容发送到主脚本。 But the only thing that happens is that it resets the field value, without opening any tab. 但是发生的唯一事情是它重置了字段值,而没有打开任何选项卡。

This is the embedded script in the panel: 这是面板中的嵌入式脚本:

 var textArea = document.getElementById('edit-box'); textArea.addEventListener("keypress", onkeypress) function onkeypress(event) { if (event.keyCode == 13) { textArea.setAttribute('tabindex', 0); text = textArea.value.replace(/\\r?\\n/gm,""); addon.port.emit("text-entered", text); textArea.value = ''; } } 

And this is the main script, where it should open the tab: 这是主要脚本,应在其中打开选项卡:

 var buttons = require('sdk/ui/button/action'); var tabs = require("sdk/tabs"); var ui = require("sdk/ui"); var { ToggleButton } = require('sdk/ui/button/toggle'); var panels = require("sdk/panel"); var self = require("sdk/self"); var data = require("sdk/self").data; var button = ToggleButton({ id: "my-button", label: "my button", icon: { "16": "./icon-16.png", "32": "./icon-32.png", "64": "./icon-64.png" }, onChange: handleChange }); var panel = panels.Panel({ width: 300, height: 169, contentURL: self.data.url("panel.html"), onHide: handleHide }); function handleChange(state) { if (state.checked) { panel.show({ position: button }); } } function handleHide() { button.state('window', { checked: false }); } function handleClick(state) { text_entry.show(); } var text_entry = require("sdk/panel").Panel({ contentURL: data.url("panel.html"), contentScriptFile: data.url("get-text.js") }); text_entry.on("show", function() { text_entry.port.emit("show"); }); text_entry.port.on("text-entered", function(text) { console.log(text); text_entry.hide(); tabs.open("http://www.example.com/" + text + ".html"); }); 

Now, how do i make it work? 现在,我如何使其工作?

Thanks in advance 提前致谢

Change keypress event to keyup : keypress事件更改为keyup

you have: 你有:

textArea.addEventListener("keypress", onkeypress)

make it: 做了:

textArea.addEventListener("keyup", onkeypress)

keypress doesnt have e.keyCode i dont think 按键没有e.keyCode我不认为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM