Chrome UserScripting doesn’t have the advantage of UnsafeWindow that GreaseMonkey does. From what I can tell UnsafeWindow seems to be the lazy way to do things, but which has the advantage of being FAR F*CKING easier, hence it’s popularity.

Like a lot of things, the end solution is very simple, but it took me hours to get there:

var editor = document.createElement('textarea'); = "msgpost" = "msgpost"

Creates the text area on the page

var scriptnic = document.createElement('script');
scriptnic.type = 'text/javascript';
scriptnic.src = '';

Creates a script element in the head of the page that loads NicEdit. I had been playing about with the YUI RichEditor, but nicEdit is much more lightweight. And lightweight = awesome.

Now the fun part:

function waitforlibs() {
	if (typeof bkLib=='undefined')
		window.setTimeout(waitforlibs, 100);
		// Lazy, turns all text areas to editors, but you can target specific IDs        

Since this is all being inserted via UserScript, even the call to the external library, need to ensure that it’s loaded before calling the function. This is what this function does, which was pieced together from Rich Manalang and RunningBlind.

The biggest problem I had was how to get the above to actually run. It’s no good calling it from the UserScript itself as it needs to be in the context of the window. And there’s no UnsafeWindow, so…

var script = document.createElement('script');
script.type = 'text/javascript';
// Define function and run function
script.textContent = waitforlibs +"; waitforlibs();"; 

which seems incredibly simple and straightforward now, but had me banging my head for hours.

And that’s it. Now I can create a textarea and load the nicEditor. Of course this is useless on it’s own, but I intend to tie into other stuff.

Why would you want to do this? Well I did so I guess some other idiot might.