FireFox附加组件:从当前选项卡中的文本框中检索文本不会产生任何结果 [英] FireFox Add-on: retrieve text from textbox in current tab gives no result
问题描述
我试图创建一个获取内容的插件( city 的文本框中输入值rel =nofollow> textbox.value
)当前网页并将其写入文本文件。
文件可以被写入而不会获得文本框的值。但是,如果我更新代码,那么它什么都不写。下面是我用来获取文本框值的代码。
var cityfromfield = window.content.document.getElementById('city' )。值;
var date = new Date();
var TimeStamp = date.toLocaleString();
var wstrtotext = TimeStamp + cityfromfield;
fos.write(wstrtotext,wstrtotext.length);
任何帮助将不胜感激。
没有更多的信息,有必要猜测你的问题是什么。最可能的问题是,您正试图在错误的文档
中找到ID = city
的文本框元素。 / p>
Firefox附加组件通常运行在未定义全局窗口
对象的范围内(如果定义的依赖于关于如何输入当前正在运行的代码部分)。即使定义了它,它通常也不会被定义为你期待的窗口
窗口(窗口
)标签)。您可能需要获取对最近访问的窗口/选项卡的窗口
对象的引用。如果一个浏览器窗口存在(在某些情况下,你可能在没有浏览器窗口存在的地方运行,例如在启动时),你可以获得一个对最近的浏览器窗口
,文档
和 gBrowser
if(window === null || typeof window!==object){
// If你还没有窗口引用,你需要获得一个:
//添加/删除一个/来评论/取消注释适合你的加载项的代码。
/ *附加SDK:
var window = require('sdk / window / utils')。getMostRecentBrowserWindow();
// * /
// *覆盖和引导(来自几乎所有的上下文/范围):
var window = Components.classes [@ mozilla.org/appshell/window-mediator; 1]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow(navigator:browser);
//
if(typeof document ===undefined){
//如果没有定义文档,可以获取
var document = window.content.document;
if(typeof gBrowser ===undefined){
//如果没有定义gBrowser,可以获取
var gBrowser = window.gBrowser;
$ b 如果您正在运行代码以响应事件(例如,按钮 command
event),您可以通过以下方式获得当前的窗口
:
var window = event.view
可用的全局窗口
对象,或者让它引用的东西不是你期望的,这是很多人在编写Firefox插件时遇到的问题。
注意:如果您想要本地兼容多进程的Firefox (Electrolysis或者e10s),那么获得对当前文档内容的访问就比较复杂。有一些垫片可以使你的代码继续在多进程的Firefox中运行一段时间,但是它们可能会最终消失。
参考文献:
-
- 在Chrome代码中使用Windows
- SDK:< a href =https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/window_utils =nofollow noreferrer> window / utils
- SDK: windows
大部分内容都是从我以前的答案中复制的,包括这个链接。
I am trying to create an add-on that gets the contents (textbox.value
) from a textbox with ID = city
from the current webpage and write it to a text file.
The file can be written without getting the textbox value. But, If I update the code then it writes nothing. Below is the code I used to get the textbox value.
var cityfromfield = window.content.document.getElementById('city').value;
var date = new Date();
var TimeStamp = date.toLocaleString();
var wstrtotext = TimeStamp + cityfromfield;
fos.write(wstrtotext, wstrtotext.length);
Any help would be appreciated.
解决方案 Without more information, it is necessary to guess at what your problem is. The most likely issue is that you are attempting to find the textbox element with ID=city
in the wrong document
.
Firefox add-ons generally run in a scope where the global window
object is not defined (if it is defined depends on how the portion of your code that is currently running was entered). Even if it is defined, it is often not defined as the window
which you are expecting (the window
of the current tab). You will probably need to obtain a reference to the window
object for the most recently accessed window/tab.
If a browser window exists (in some instances you could be running where no browser window exists, yet, e.g. at start-up), you can obtain a reference to the most recent browser window
, document
, and gBrowser
with:
if (window === null || typeof window !== "object") {
//If you do not already have a window reference, you need to obtain one:
// Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
/* Add-on SDK:
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
//* Overlay and bootstrap (from almost any context/scope):
var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
//*/
}
if (typeof document === "undefined") {
//If there is no document defined, get it
var document = window.content.document;
}
if (typeof gBrowser === "undefined") {
//If there is no gBrowser defined, get it
var gBrowser = window.gBrowser;
}
If you are running the code in response to an event (e.g. a button command
event), you can obtain the current window
with:
var window = event.view
The lack of having the global window
object available, or having it reference something other than what you are expecting, is something that many people encounter as a problem when writing Firefox add-ons.
Note: If you are wanting to be natively compatible with multi-process Firefox (Electrolysis, or e10s), then gaining access to the contents of the current document is more complex. There are shims in place which should make your code continue to work with multi-process Firefox for some time, but they may/will eventually go away.
References:
Large portions of this were copied from my earlier answers, including this link.
这篇关于FireFox附加组件:从当前选项卡中的文本框中检索文本不会产生任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!