Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

No way to check for opened view source window

62 views
Skip to first unread message

sabine.micha...@gmail.com

unread,
Mar 14, 2012, 8:41:01 AM3/14/12
to
I am writing a test where a link is clicked and the result is displayed in a view source window. Needing to check that a view source window was opened I used the observer service to watch for xul-window-registered events. Unfortunately, it seems like the browser.xul window is returned instead.

observe: function(aSubject, aTopic, aData) {
let win = aSubject.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);

win.addEventListener("load", function windowLoad() {
win.removeEventListener("load", windowLoad);
let wintype = win.document.documentElement.getAttribute("windowtype");
info(win.location.href); // chrome://browser/content/browser.xul
info(wintype);
});
},

I have also tried using nsIWindowMediator.getMostRecentWindow("navigator:view-source") inside the onload event but that also fails to find the view source window.

Does anybody know a way to detect when a new view-source window is opened?

Gijs Kruitbosch

unread,
Mar 14, 2012, 9:20:16 AM3/14/12
to
Instead of using the observer service, use the window watcher's
registerNotification listener? The window watcher is available on the Services
object (https://developer.mozilla.org/en/JavaScript_code_modules/Services.jsm ),
should be easy to find. That should work, I believe...

Note also that idl types which have the 'function' keyword defined, like
nsIObserver, can be represented by a single function in JS. So both for the
observer service and for window watcher's registerNotification method, you can
just pass a JS function, you don't need to wrap it in an object with a
QueryInterface method etc.

HTH,
Gijs

sabine.micha...@gmail.com

unread,
Mar 14, 2012, 10:33:51 AM3/14/12
to
> Instead of using the observer service, use the window watcher's
> registerNotification listener? The window watcher is available on the Services
> object (https://developer.mozilla.org/en/JavaScript_code_modules/Services.jsm ),
> should be easy to find. That should work, I believe...
>
> Note also that idl types which have the 'function' keyword defined, like
> nsIObserver, can be represented by a single function in JS. So both for the
> observer service and for window watcher's registerNotification method, you can
> just pass a JS function, you don't need to wrap it in an object with a
> QueryInterface method etc.
>
> HTH,
> Gijs

Thanks, now the method is smaller but there is still the same problem. win.location.href outputs chrome://browser/content/browser.xul instead of chrome://global/content/viewSource.xul :o/

Services.ww.registerNotification(function onWindow(aSubject, aTopic) {
let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
win.addEventListener("load", function windowLoad() {
win.removeEventListener("load", windowLoad);

// The following line outputs chrome://browser/content/browser.xul
// instead of chrome://global/content/viewSource.xul
info(win.location.href);

Services.ww.unregisterNotification(onWindow);
});
});
// Open view source window by clicking a link
EventUtils.synthesizeMouseAtCenter(link, { }, contentWindow);

Gijs Kruitbosch

unread,
Mar 14, 2012, 11:02:19 AM3/14/12
to sabine.micha...@gmail.com
On 14/03/2012 15:33 PM, sabine.micha...@gmail.com wrote:
> Thanks, now the method is smaller but there is still the same problem. win.location.href outputs chrome://browser/content/browser.xul instead of chrome://global/content/viewSource.xul :o/
>

If you manually check the getMostRecentWindow("navigator:view-source") return
value after the window has opened, do you get the same window? Maybe there's a
redirect of some sort?

~ Gijs

sabine.micha...@gmail.com

unread,
Mar 14, 2012, 11:09:13 AM3/14/12
to sabine.micha...@gmail.com
> If you manually check the getMostRecentWindow("navigator:view-source") return
> value after the window has opened, do you get the same window? Maybe there's a
> redirect of some sort?
>
> ~ Gijs

If I do that from the onload handler then getMostRecentWindow returns null.

sabine.micha...@gmail.com

unread,
Mar 14, 2012, 11:39:16 AM3/14/12
to sabine.micha...@gmail.com
To open view source I was usingI was using:
openUILinkIn("view-source:" + href, "window");

If I instead use openDialog the above code works:
openDialog("chrome://global/content/viewSource.xul", null, null, href);

I still don't see using openUILinkIn would cause ww to receive the wrond xulWindow.

Rob Campbell

unread,
Mar 14, 2012, 1:32:05 PM3/14/12
to dev-apps-firefox
This is what I was hinting at in our meeting earlier today.

Using "openUILinkIn(…, "window")" is opening a new browser window.

Your call to getMostRecentWindow("navigator:view-source") is likely failing because because it's not a real "view-source" window. It's a browser (navigator:browser).

running this in a browser scratchpad confirms:

let win = window.openUILinkIn("view-source:" + gBrowser.contentWindow.location.href, "window");

Services.wm.getMostRecentWindow("navigator:view-source") // this is null

~ rob
> _______________________________________________
> dev-apps-firefox mailing list
> dev-apps...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-apps-firefox

Gavin Sharp

unread,
Mar 14, 2012, 1:33:46 PM3/14/12
to sabine.micha...@gmail.com, dev-apps...@lists.mozilla.org
openUILinkIn is a helper function to open a link in a normal browser
window. You just happen to be passing it a view-source: URI - that
doesn't magically ensure that viewSource.xul loads (the same way that
loading a view-source: URI in a normal window doesn't turn it into a
view-source window). If you want to open the view source window, you
need to open it specifically (using e.g. gViewSourceUtils.viewSource),
as you've discovered.

Gavin

Geoff Lankow

unread,
Mar 14, 2012, 6:58:19 PM3/14/12
to
As it happens I've been writing tests for the view source window in the
last few months. This should give you all the code you need:
http://mxr.mozilla.org/mozilla-central/source/toolkit/components/viewsource/test/browser/

0 new messages