Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a resource using Chrome Dev Tools?

I'm viewing a webpage using Chrome. The original source does not include jQuery, but it'd be really helpful to use some jQuery functions to inspect the DOM. It would be a pain to alter the source to include the jQuery script, so is it possible to use Chrome Developer Tools to load jQuery to the DOM after the page loads? Alternately, is there a native JavaScript function that could perform the load?

I tried to edit the head tag to include the <script src="/path/to/jQuery.min.js"></script> tag, but that doesn't actually load the jQuery.min.js file.

Unfortunately, Modernizr and other asset loaders are also not included with the source.

like image 380
Jeromy French Avatar asked Apr 11 '13 17:04

Jeromy French


People also ask

What can you do with Chrome developer tools?

DevTools is a set of web developer tools that are built directly into the Google Chrome browser. DevTools allows you to view and change/manipulate the DOM, change a page's styles (CSS) in a preview environment, and work with JavaScript by allowing you to debug, view messages and run JavaScript code in the console.

How do I add to Chrome developer tools?

To add a variable to the watch list use the add icon to the right of the section heading. This will open an inline input where you provide the variable name to watch. Once it is filled in press your Enter key to add it to the list. The watcher will show you the current value of the variable as it is added.

How do I open the Resource tab in Chrome?

Click the Sources tab to open the Sources panel. Click the Page tab to show the page's resources. The Page pane opens.


2 Answers

You should be able to run this in your JS console.

var jq = document.createElement('script');
jq.src = "/path/to/jQuery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jQuery.noConflict();

Alternatively you can copy the entire jQuery file into the console. Not the best option, but that would work too.

A third option is this plugin, jQueryify, I haven't used it myself, but looks promising.

like image 140
Matt Busche Avatar answered Oct 01 '22 01:10

Matt Busche


Use requirify's awesome chrome extention and get access to require() in the console.

You can totally upgrade your testing workflow in Chrome developer console now that you can directly import npm modules into the console.

After installing requirify, you could easily add jquery to your console session by typing this into the developer console:

var $ = require('jquery');

This will fetch jQuery from npm, in turn making it available for you in the console. Now you can use jQuery in your console like you would on any webpage:

$('div').each(function(index){ //... });

Obviously this would work for your situation, but would also work in many other situations as well. A great tool to have in your toolbox!


See require() being used in the console below:

require() in the browser!

Awesome!

like image 26
radiovisual Avatar answered Oct 01 '22 00:10

radiovisual