I imagine you can write you javascript in a document that contains an iframe and load the pages that way.
Then you can run a regex against the innerHTML of the iframe to look for the code snippet.
Don't know about the mousedown bit. You might want to test that first.