Skip to content

Instantly share code, notes, and snippets.

@techieshark
Last active November 9, 2018 12:14
Show Gist options
  • Save techieshark/45d6f183f80835757336 to your computer and use it in GitHub Desktop.
Save techieshark/45d6f183f80835757336 to your computer and use it in GitHub Desktop.
jquery-and-lodash-in-console
// For quickly trying things out in the browser, jQuery and Lo-Dash (like Underscore.js) are great.
// Read the code below (never copy & paste code you don't trust),
// then you can copy & paste it into the browser console to load jQuery & lodash.
(function () {
var jq = document.createElement('script');
jq.src = 'https://code.jquery.com/jquery-2.1.4.js';
document.getElementsByTagName('head')[0].appendChild(jq);
// jQuery should be available via "jQuery" (and we'll set up the $ alias in the defer() function)
var ld = document.createElement('script');
ld.src = 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js';
document.getElementsByTagName('head')[0].appendChild(ld);
// lodash should be available via "_".
var defer = function () {
if (window.jQuery) {
jQuery.noConflict();
$ = jQuery;
// jQuery should be available via "$".
} else {
setTimeout(function () {
defer();
}, 50);
}
};
defer(); // wait for jQuery to download, then set up $ alias
console.log('jQuery ($) and lodash (_) should now be available in your browser console.');
console.log('If not, there may be a Content Security Policy error.');
console.log('See: https://jquery.com https://lodash.com for documentation.')
}());
@fullofcaffeine
Copy link

Should turn this into a bookmarklet :)

@techieshark
Copy link
Author

@fullofcaffeine good idea! How about something like this? http://techieshark.github.io/copy-into-browser/

@techieshark
Copy link
Author

@fullofcaffeine It looks like we'd sometimes run into CSP issues. Hopefully not too often, and there may be workarounds. Just noting that for future reference.

Still need to look into this further, but for now, see also:

@techieshark
Copy link
Author

Update: check out "Injecta" extension for chrome. There's also "Script Injector", and "Custom Javascript for Websites".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment