views:

116

answers:

1

I'm currently writing a bookmarklet that loads and executes a remote js file by appending a new <script> tag in the current window, like so :

javascript:(function() {
    if(typeof __bml_main != "undefined") return __bml_main.init();
    var s= document.createElement('script');
    s.type= 'text/javascript';
    s.src= 'http://127.0.0.1:8000/media/bookmarklet.js';
    void(document.body.appendChild(s));
})();

My bookmarklet needs to perform some dom manipulations in order to extract data from the page being viewed, and then to open a new popup to list them.

The thing is : if I want to bypass pop-up blockers, I can't open my new window from the injected script. I need to open it right from the beginning in the bookmarklet code, and to access it later when needed.

I've tried to do somehting like this :

javascript:var my_popup = window.open('http://127.0.0.1:8000/resources/manage/new/', 'newResourcePopup',config='height=200,width=400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
(function() {
// script injection (...)
})();

but if I then try to access my_popup from my remotely loaded script, most browsers will throw a security warning and won't let me access the Window object. This is understandable since the script is not from the same domain than the displayed page, but I'm kind of stuck...

A solution would be to use a div overlay, but I'd really prefer to open a window in this case.

Any hints ?

+1  A: 
T.J. Crowder
Thanks for your anwser ! :)This is kind of ugly, but it works quite well. Actually, I think I'll use document.createElement() instead of document.write() to create the dom of the page. An about:blank page doesn't have any doctype, but it sounds more standard-compliant to me to manipulate nodes rather than appending the markup as a string.
Neewok
@Neewok: Glad that helped. Do you know, it's been so long since I dealt with creating pop-ups that the last time I did, `document.write` was still more reliable and I just hadn't updated my thinking. I've never created a DOM from scratch in an empty document, but I can't imagine in principle why it shouldn't work, good thought. :-)
T.J. Crowder
A way better idea popped into my head : once the popup is opened, you can just change its location with all the variables needed in the query string. Creating the dom with javascript is a real hassle.
Neewok