How to inject jQuery code on a page, strictly client side - Chrome?
I'm wondering if there's a way in Chrome I can execute 开发者_StackOverflowa .js file on a rendered page I'm viewing? In other words, I'd like to be able to visit some site that I have no control over (e.g. stackoverflow.com), and execute the below code for example:
$('body').fadeOut('slow');
I could just type it in the console, and that works, but let's say there's a whole bunch of jQuery code I wanted to run and test out. Is there some way I can do this?
Have you looked into Greasemonkey for Chrome?
sure, just append a new script to the dom:
var myScript = document.createElement('script');
myScript.type = 'text/javascript';
myScript.async = true;
myScript.src = 'http://path/to/your/js/file.js' // ie: jquery hosted on google cdn
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(myScript, s);
Repeat with other js files.
Note this is still using the console, but would allow you to at least load up a bunch of files rather than just typing your code to execute in the browser
Regarding this I found painless solution. I installed jQuerify. It injects jQuery (the latest available stable version) even into HTTPS pages with one click with no conflict to Prototype )))
I agree with the greesemonkey answer. But another option is a Bookmarklet:
http://en.wikipedia.org/wiki/Bookmarklet
and for something quick and dirty you can just copy the script that would be your bookmarklet into your address bar directly (good for testing before you've finalized the bookmarklet).
I think you might be looking for a chrome plugin. I looked in the webstore, but there doesn't appear to be one to execute arbitrary code. Since we have no idea what your end goal is we really can't point you to anything more specific that write the code in your editor of choice and copy/paste it into the console.
精彩评论