简体   繁体   English

Firefox扩展:如何填充输入文本字段

[英]Firefox extension: how to fill an input text field

I have a custom toolbar button created in accordance with the tutorial . 我有一个按照教程创建的自定义工具栏按钮。 The javascript code for the button is the following: 该按钮的javascript代码如下:

CustomButton = {

1: function () {
  alert("Just testing") 
  },

}

And it works. 它有效。 When I click on this button, alert happens. 当我点击此按钮时,会发出警报。 I'm trying to make some changes, namely, fill input field on clicking this button, but this piece of code doesn't work: 我正在尝试进行一些更改,即单击此按钮填充输入字段,但这段代码不起作用:

CustomButton = {

1: function () {
  document.getElementById("loginUserName").value = "aaaa";  
  },

}

The element with Id "loginUserName" exists on the page. 页面上存在ID为“loginUserName”的元素。 I can easily fill it with Selenium WebDriver: 我可以使用Selenium WebDriver轻松填充它:

driver.findElement(By.id("loginUserName")).sendKeys(User). 

So my question is why it doesn't work in Firefox extension? 所以我的问题是为什么它在Firefox扩展中不起作用?

Thanks, Racoon 谢谢,浣熊

Your toolbar button and the input field (assuming the latter is part of a web page) live in separate documents. 您的工具栏按钮和输入字段(假设后者是网页的一部分)存在于单独的文档中。 But it's easy cross the boundary. 但它很容易越过边界。 Change your code to 将您的代码更改为

gBrowser.contentDocument.getElementById("loginUserName").value = "aaaa";

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

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