Firefox and the Python webbrowser module

I’d like to be able to open a window without browser chrome from the Python webbrowser module. After a bit of poking around, it seems to be possible. This will only work with Mozilla, and it might only work on Linux (although it should be OK on Windows). The complexity in doing it is that you can’t specify a window size or configuration when calling webbrowser.open(). However, JavaScript can: the window.open() function can specify no-chrome, window sizes, the lot. But you can’t just call webbrowser.open('javascript:window.open("...")'), because then you’ll have two windows; the one that ran the JS and the one the JS opened. JS can’t close the one that ran the JS, because it wasn’t opened by JS. The solution? Chrome windows:

import webbrowser
moz = webbrowser.get('mozilla')
moz.open('javascript:void(window.open("http://www.kryogenix.org","winname","chrome"))')

It should be possible to make this work for IE as well, because IE allows a child window to close its parent, even if the parent wasn’t opened by JS. However, I don’t need that at the moment; if I ever release this app that I’m using this for (it’s an example of Browser as Desktop UI) then I’ll make it cross-browser.


More in the discussion (powered by webmentions)

  • (no mentions, yet.)