I'm not sure if Greasemonkey or Firefox has buggered this, but window.open
to a blank page, from a Greasemonkey script, now triggers a Same Origin Policy violation.
Meanwhile, Page scope, the console scope, and Firebug's console all work fine.
Greasemonkey scope gives:
SecurityError: The operation is insecure
whether @grant none
is used or not.
This, plus the general uselessness of GM_openInTab(), lead me to suspect it is a Greasemonkey bug. I don't have time to look into it right now, but file a bug report, if you wish.
To get this to work on the latest release of Firefox (28.0) and Greasemonkey (1.15), here's what I had to do:
- Tell my popup blockers to (temporarily) allow popups from stackoverflow.com.
- Inject the popup code into the page scope.
- Use an explicit
about:blank
for the URL. - Wait for the new window to load.
Here's a complete script that works on the latest FF+GM releases:
// ==UserScript==
// @name document.write () test
// @description tests document.write ()
// @include http://stackoverflow.com/questions/22651334/*
// ==/UserScript==
function fireNewTab () {
var newTab = window.open ('about:blank', '_blank');
newTab.addEventListener (
"load",
function () {
//--- Now process the popup/tab, as desired.
var destDoc = newTab.document;
destDoc.open ();
destDoc.write ('<html><head></head><body><ul><li>a</li><li>b</li><li>c</li></ul></body></html>');
destDoc.close ();
},
false
);
}
addJS_Node (null, null, fireNewTab);
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
var D = document;
var scriptNode = D.createElement ('script');
if (runOnLoad) {
scriptNode.addEventListener ("load", runOnLoad, false);
}
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}