5

Does anybody know if there is some way to run a piece of Javascript code for each page that loads in a browser? Sort of like inserting <script> first thing inside the head tag. I am most interested in a solution for Chrome. Maybe that is a job for a Chrome extension?

1
  • Do you mean for any web page, or just one you can modify the source for? Commented Mar 16, 2012 at 16:59

1 Answer 1

9

Check out Tampermonkey, it's a Chrome extension for running user scripts (equivalent to GreaseMonkey for FF). To run a script on every page, you'd have a line like this near the beginning of your script:

// @include     *

Here's a tutorial on writing user scripts.

Sign up to request clarification or add additional context in comments.

5 Comments

Looks promising. Do you know how Tampermonkey does the trick, i.e. telling Chrome to always load the script?
@JonasN not sure exactly, but Chrome APIs do give you a lot of control.
Got my first script going (alert('Tampermonkey Script!') :) ), so this is a verified solution. Thanks!
BTW, there is a selector "Run at:" in the Settings gui with the options of "document-start", "document-body" and "document-end". "document-end" seems to be the pre-selected alternative.
@JonasN that makes sense to me because people mostly use user scripts to "fix" things on pages or add new functionality. You'd want the script to run at the end so that the DOM exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.