问题描述
我需要它在我的Firefox扩展中使用。我已经尝试了window.onload事件监听器,并检查当前的URL ==旧的URL,这是一个好主意,但它不起作用,当页面加载PDF。
我也看到了散列更改功能,但它只适用于ff 3.6;因此,我需要一个事件监听器来检查document.location是否改变。
谢谢
例如,您可以使用:
var mainWindow = window
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface Components.interfaces.nsIDocShellTreeItem).rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
mainWindow.getBrowser()。addEventListener(DOMContentLoaded,
your_function,false);
但是,重点是,您需要将侦听器添加到浏览器,而不是窗口。 p>
编辑
请参阅了解有关从不同上下文访问浏览器的信息,以及一些其他有用的信息。
编辑
只需添加更多细节....
<$ p函数your_function(event){
if(event.originalTarget instanceof HTMLDocument){
var doc = event.originalTarget;
//如果它只是一个框架元素,则返回并等待
//主事件触发。
if(event.originalTarget.defaultView.frameElement)
return;
//现在你可以使用doc.location,但是你想要
code $ pre
请注意,这将响应在任何标签打开的页面,而不是一个特定的标签。
var newTabBrowser = gBrowser.getBrowserForTab(gBrowser.addTab( http://www.google.com/));
newTabBrowser.addEventListener(load,function(){
newTabBrowser.contentDocument.body.innerHTML =< div> hello world< / div>;
},true);
以上添加了一个新选项卡,然后添加了一个侦听器。但是,对于所有选项卡,您需要如下所示的内容。然后添加DOMContentLoaded事件侦听器在每个选项卡添加(并关闭时删除)。
您可能还想看到:和
(用于保存)
pre $ function exampleTabAdded(event){
var browser = gBrowser.getBrowserForTab(event.target);
//浏览器是已添加的浏览器的XUL元素
}
函数exampleTabMoved(event){
var browser = gBrowser.getBrowserForTab(event.target );
//浏览器是被移动的浏览器的XUL元素
}
函数exampleTabRemoved(event){
var browser = gBrowser.getBrowserForTab(event.target );
//浏览器是被删除的浏览器的XUL元素
}
//在初始化期间
var container = gBrowser.tabContainer;
container.addEventListener(TabOpen,exampleTabAdded,false);
container.addEventListener(TabMove,exampleTabMoved,false);
container.addEventListener(TabClose,exampleTabRemoved,false);
//不再需要
container.removeEventListener(TabOpen,exampleTabAdded,false);
container.removeEventListener(TabMove,exampleTabMoved,false);
container.removeEventListener(TabClose,exampleTabRemoved,false);
这应该会让你有99%的出路。
I need it to use in my firefox extension.I already tried window.onload event listener, and check if current url == old url, and it's a good idea, but it does'nt work when page loads pdf.
I saw hash changed function too but it works only with ff 3.6; i need it to work at least with ff 3.
So, i need an event listener that check if document.location changes.
Thanks
For example, you could use:
var mainWindow = window
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem).rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
mainWindow.getBrowser().addEventListener("DOMContentLoaded",
your_function, false);
But, the point is, you need to add the listener to the browser, not window.
EDIT
See https://developer.mozilla.org/En/Code_snippets/Tabbed_browser for info on accessing the browser from different contexts, as well as some other useful info.
EDIT
Just to add a little more detail....
function your_function(event) {
if (event.originalTarget instanceof HTMLDocument) {
var doc = event.originalTarget;
// if it's just a frame element, then return and wait for the
// main event to fire.
if (event.originalTarget.defaultView.frameElement)
return;
// now you can use doc.location however you want
}
}
Note that this will respond to pages opened in any tab, not a specific tab.
For a specific tab, you could use something like this:
var newTabBrowser = gBrowser.getBrowserForTab(gBrowser.addTab("http://www.google.com/"));
newTabBrowser.addEventListener("load", function () {
newTabBrowser.contentDocument.body.innerHTML = "<div>hello world</div>";
}, true);
The above adds a new tab and then adds a listener. However, for all tabs, you'll need something like the following. And then add the "DOMContentLoaded" event listener on each tab when added (and removed when closed).
You may also want to see: https://developer.mozilla.org/En/Code_snippets/Tabbed_browser#Notification_when_a_tab_is_added_or_removed and https://developer.mozilla.org/En/Code_snippets/Tabbed_browser#Getting_document_of_currently_selected_tab
(For preservation)
function exampleTabAdded(event) {
var browser = gBrowser.getBrowserForTab(event.target);
// browser is the XUL element of the browser that's been added
}
function exampleTabMoved(event) {
var browser = gBrowser.getBrowserForTab(event.target);
// browser is the XUL element of the browser that's been moved
}
function exampleTabRemoved(event) {
var browser = gBrowser.getBrowserForTab(event.target);
// browser is the XUL element of the browser that's been removed
}
// During initialisation
var container = gBrowser.tabContainer;
container.addEventListener("TabOpen", exampleTabAdded, false);
container.addEventListener("TabMove", exampleTabMoved, false);
container.addEventListener("TabClose", exampleTabRemoved, false);
// When no longer needed
container.removeEventListener("TabOpen", exampleTabAdded, false);
container.removeEventListener("TabMove", exampleTabMoved, false);
container.removeEventListener("TabClose", exampleTabRemoved, false);
This should get you 99% of the way there.
这篇关于我如何检查当前浏览器选项卡的URL是否被更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!