[英]Cannot get instance of TinyMCE in XUL Firefox extension
I am trying to get the instance of tinyMCE editor in JavaScript for Firefox extensions. 我正在尝试在JavaScript中为Firefox扩展获取tinyMCE编辑器的实例。
When I type window.tinyMCE
in the console, it returns an object; 当我在控制台中键入
window.tinyMCE
时,它返回一个对象; however, when trying to get an instance of tinyMCE editor in JavaScript within my Firefox extension, I use window.content.tinyMCE
and get undefined
但是,当尝试在Firefox扩展中的JavaScript中获取tinyMCE编辑器的实例时,我使用
window.content.tinyMCE
并得到undefined
For example, I am trying to see if a tinyMCE editor exists on a page with my Firefox extension. 例如,我试图查看在带有我的Firefox扩展的页面上是否存在tinyMCE编辑器。 Here is my code:
这是我的代码:
if (window.content.tinyMCE) {
alert('its there!');
}
The TinyMCE editor exists on the page, but does not alert the above code. TinyMCE编辑器存在于页面上,但不会警告以上代码。 When I do
alert(window.content.tinyMCE)
, it alerts undefined
. 当我发出
alert(window.content.tinyMCE)
,它会发出undefined
警报。
In XUL, Window.content
returns a Window object for the primary content window. 在XUL中,
Window.content
返回主要内容窗口的Window对象。 https://developer.mozilla.org/en-US/docs/Web/API/Window/content https://developer.mozilla.org/zh-CN/docs/Web/API/Window/content
The Window object is definitely there, since when I type window.tinyMCE
in the console, it returns the tinyMCE object. Window对象肯定在那里,因为当我在控制台中键入
window.tinyMCE
时,它会返回tinyMCE对象。
Anyone know what I'm doing wrong? 有人知道我在做什么错吗?
The most likely issue is that the window
in your extension is not the window
object you are expecting. 最有可能的问题是,
window
在您的扩展不是window
你期待的对象。
Firefox add-ons generally run in a scope where the global window
and document
objects are not defined (if they are defined depends on how the portion of your code that is currently running was entered). Firefox附加组件通常在未定义全局
window
和document
对象的范围内运行(如果定义了它们,则取决于输入当前正在运行的代码部分的方式)。 Even if they are defined, they are often not defined as the window
, or document
which you are expecting (the window
/ document
of the current tab). 即使它们被定义,他们往往不被定义为
window
,或document
您期待(在window
/ document
当前选项卡的)。 You will probably need to obtain a reference to both objects 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, eg at start-up), you can obtain a reference to the most recent browser window
, document
, and gBrowser
with: 如果存在浏览器窗口(在某些情况下,您可能正在不存在任何浏览器窗口的情况下运行,例如在启动时),则可以使用以下内容获取对最新浏览器
window
, document
和gBrowser
:
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 (eg a button command
event), you can obtain the current window
with: 如果您是为了响应事件(例如按钮
command
事件)而运行代码,则可以使用以下方法获取当前window
:
var window = event.view
The lack of having the global window
an document
objects available, or having them reference something other than what you are expecting, is something that many people encounter as a problem when writing Firefox add-ons. 许多人在编写Firefox附加组件时会遇到一个问题,即缺少全局
window
可用的document
对象,或者缺少引用的对象。
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. 注意:如果要与多进程Firefox (Electrolysis或e10s)本地兼容,那么访问当前文档的内容将更加复杂。 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.
有一些垫片可以使您的代码在多进程Firefox上继续运行一段时间,但最终可能会消失。
References: 参考文献:
nsIWindowMediator
Large portions of this answer were copied from my earlier answers, including this link and here . 此答案的大部分内容是从我以前的答案中复制而来的,包括此链接和此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.