Is there an easy way to include jQuery in the Chrome JavaScript console for sites that do not use it? For example, on a website I would like to get the number of rows in a table. I know this is really easy with jQuery.
$('element').length;
The site does not use jQuery. Can I add it in from the command line?
noConflict(); It may take a bit of time for jQuery to load. But, once jQuery is loaded, you can run your custom jQuery functions in chrome console. There may be some conflicts with other jQuery versions that may have been loaded on the page.
To open the developer console in Google Chrome, open the Chrome Menu in the upper-right-hand corner of the browser window and select More Tools > Developer Tools. You can also use Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux).
You can use JavaScript by default in the chrome console but jQuery allows me to create these one-off scripts more quickly. Load jQuery into your current page by copying and pasting the following code into your Chrome Console. It may take a bit of time for jQuery to load.
The site does not use jQuery. Can I add it in from the command line? For more of an automated approach you can use a userscript to include it. Seriously this would be like a 5 line userscript :P document.getElementById ('tableID').rows.length. If the table doesn't have an ID, use the DOM editor to give it one.
Name it as you like, e.g. Inject jQuery, and use the following line for URL: javascript: (function (e,s) {e.src=s;e.onload=function () {jQuery.noConflict ();console.log ('jQuery injected')};document.head.appendChild (e);}) (document.createElement ('script'),'//code.jquery.com/jquery-latest.min.js') Below is the formatted code:
If you don’t know how to get to your browser’s console, make sure you right click on the page that you want to scrape and click on I nspect. Then, click on the Console tab, paste the code that you copied above it into the console, and click enter to run the code. It’ll look like the picture below: Were you able to run jQuery in the Chrome console?
Run this in your browser's JavaScript console, then jQuery should be available...
var jq = document.createElement('script'); jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"; document.getElementsByTagName('head')[0].appendChild(jq); // ... give time for script to load, then type (or see below for non wait option) jQuery.noConflict();
NOTE: if the site has scripts that conflict with jQuery (other libs, etc.) you could still run into problems.
Making the best better, creating a Bookmark makes it really convenient, let's do it, and a little feedback is great too:
javascript:(function(e,s){e.src=s;e.onload=function(){jQuery.noConflict();console.log('jQuery injected')};document.head.appendChild(e);})(document.createElement('script'),'//code.jquery.com/jquery-latest.min.js')
Below is the formatted code:
javascript: (function(e, s) { e.src = s; e.onload = function() { jQuery.noConflict(); console.log('jQuery injected'); }; document.head.appendChild(e); })(document.createElement('script'), '//code.jquery.com/jquery-latest.min.js')
Here the official jQuery CDN URL is used, feel free to use your own CDN/version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With