12

Is there a one-liner I could execute in a javascript console to download and execute a javascript script from a remote source?

I was looking to see if there was a nice way to download this script and use it for experimenting interactively on random pages which may not have say, jQuery loaded.

[edit: I'm aware I could dynamically create a script element but is there a nicer way to do this?]

jkp
  • 78,960
  • 28
  • 103
  • 104

3 Answers3

10

I've written a little script for that.

var loadjQuery = function(cb){
   if(typeof(jQuery) == 'undefined'){
     var scr = document.createElement('script');
     scr.setAttribute('type', 'text/javascript');
     scr.setAttribute('src', 'http://code.jquery.com/jquery-latest.js');

     if(scr.readyState){
        scr.onreadystatechange = function(){
            if(scr.readyState === 'complete' || scr.readyState === 'loaded'){
               scr.onreadystatechange = null;
               if(cb === 'function'){
                  args = [].slice.call(arguments, 1);
                  cb.apply(this, args);
               }
            }
        };
     }
     else {
        scr.onload = function(){
           if(cb === 'function'){
              args = [].slice.call(arguments, 1);
              cb.apply(this, args);
           }
        };
     }

     var head = document.getElementsByTagName('head')[0];
     head.insertBefore(scr, head.firstChild);  
   }
}

This works cross-browser.

edit

I've updated that script as a function with a callback. Synopsis should be:

loadjQuery(function(something){
    // execute code after library was loaded & executed
});
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • nice! just tried your code and it works well. Would be lovely if there was a way to get that loaded into chrome and give it some easy to use shortcut...I'm guessing thats the kind of thing I'd have to write a Chrome extension for. +1. – jkp Jul 16 '10 at 07:34
  • `scr.setAttribute('type', 'text/javascript');` is redundant. Also, you can use `scr.src=...` instead of `scr.setAttribute('src',...)`. – James Jul 16 '10 at 07:39
  • What is the variable "cb"? – Zuks Dec 02 '16 at 09:15
  • lol! Never mind. I see it's the callback function... This will come in handy. Thanks a lot! – Zuks Dec 02 '16 at 09:16
2

Well, it is quite simple to take a long javascript snippet and put it all together into one line :)

This approach takes a few lines you could mix togehter into a oneliner (but i guess you are looking for a shorter solution). You will have to eval the contents of the two script tags to load Google AJAX libraries - that is all. You might need to do a call to get the first one though.

Thariama
  • 50,002
  • 13
  • 138
  • 166
  • yes, I suppose you are right, I guess thats just what a bookmarklet is :) [edit: someone beat me to it! http://www.learningjquery.com/2006/12/jquerify-bookmarklet - glad I asked now....] – jkp Jul 16 '10 at 07:35
  • right, that is bookmarklet functionality - i use it often to include firebug lite – Thariama Jul 16 '10 at 07:43
0
  1. Go to the remote source (e.g.: https://ajax.googleapis.com/ajax/libs/d3js/5.7.0/d3.min.js)

  2. Select all the js source (ctrl + a) and copy to the clipboard (ctrl + c)

  3. Go to the target website where you want to inject the js

  4. Open the console, paste the copied source and hit enter

All the functions of the library are available to you on the target website's console now.

PrashanD
  • 2,643
  • 4
  • 28
  • 58