3

I've converted a script that I hope to get embedded on a StackExchange site (Lego Answers) to a GreaseMonkey/Userscript but I'd like to get it interacting with the live preview when creating or editing questions and answers.

At the moment the script runs once the page is loaded and looks for our custom tags (for example [part:3700:4]) and when it finds one, calls a webservice to retrieve the part name and image details and then replaces the tag with the image.

However, this doesn't work with either inline editing or creating a question as the preview updates on the fly.

Has anyone else hooked a userscript or similar into the preview for this sort of feature?

The script can be seen here:

BrickJax: Brick Image Replacement

2
  • My Code Golf UserScript might be a good example of how to do this. Commented Dec 19, 2011 at 0:00
  • Cheers @GeorgeEdison, I'll take a look at that too :) Commented Dec 19, 2011 at 0:10

1 Answer 1

4

There's a few ways to do it, but at first glance the simplest way would be to hook into the wmdrefresh event that will get called when the preview area is updated:

$(document).bind('wmdrefresh', function (event) {
    // Make your changes to event.target's content
});

Keep in mind that the preview update fires many, many times, so be sure to cache the responses to your web service requests to avoid spamming the services or hanging the client.

1
  • Good call on the caching... Commented Dec 18, 2011 at 22:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.