Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include jQuery in the JavaScript Console

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?

like image 890
mrtsherman Avatar asked Sep 19 '11 16:09

mrtsherman


People also ask

Can you use jQuery in console?

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.

How do I use console in Chrome?

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).

Can I use jQuery in the Chrome console?

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.

Can I add jQuery to the site from the command line?

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.

How to inject jQuery into a URL?

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:

How do I run a code in the Chrome console?

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?


1 Answers

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.

Update:

Making the best better, creating a Bookmark makes it really convenient, let's do it, and a little feedback is great too:

  1. Right click the Bookmarks Bar, and click Add Page
  2. 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:

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.

like image 178
jondavidjohn Avatar answered Oct 10 '22 01:10

jondavidjohn