2 of 4 Apparently clicking a link is hard for some, so I've copy and pasted the code from plunkr into the question itself.

Namespacing jQuery/jQueryUi into markup that I don't control

I have a project that I've been trying to get just right for the past three months and it's still not quite there yet.

I'm injecting some jQuery and jQueryUi code into pages that I have no control over and I need to be able to "sandbox" my code away from anything that may or may not be there at DOM-ready, or at anytime there after...

I think I've got it, but PLEASE.. I know there's some edge cases and race conditions that I'm missing. Please fork this and tell me what I've missed.. Tired of spinning my wheels on this issue: http://plnkr.co/edit/iWJWLrpH4RpQwQUL0bYe?p=preview

Markup:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css" />
    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />

    <script type="text/javascript" src="//www.litmos.com/wp-includes/js/jquery/jquery.js?ver=1.11.0"></script>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
  </head>

  <body>
    <h1>Loading multiple versions of jQuery!</h1>
    <script type="text/javascript">
      ;(function($){
        var $h2 = $('<h2>').append('Original jQuery: ' + $.fn.jquery + ' | Original ui: ' + $.ui.version);
        document.write($h2[0].outerHTML);
      })(jQuery);
    </script>
    <div>Some other html</div>

  <!--// Code that is being "injected into the page" //-->

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
      ;(function($){
        window.myJQuery = jQuery.noConflict(true);
      })(jQuery);
    </script>
    <script src="jquery-ui.js"></script>
    <script src="script.js"></script>

  <!--// End injection code //--> 

    <script type="text/javascript">
      ;(function($){
        var $h2 = $('<h2>').append('Original jQuery: ' + $.fn.jquery + ' | Original ui: ' + $.ui.version + ' | After namespacing');
        document.write($h2[0].outerHTML);
        window.setTimeout(function(){
          $('h2').not('.namespaced')
            .addClass('not-namespaced')
            .hide()
            .draggable()
            .toggle( "highlight" );
        }, 2000);
      })(jQuery);
    </script>
  </body>

</html>

Javascript:

;(function($, jQuery){

  var $h2 = $('<h2 class="namespaced">').append('Namespaced $: ' + $.fn.jquery + ' | $.ui: ' + $.ui.version);
  document.write($h2[0].outerHTML);

  var $jh2 = $('<h2 class="namespaced">').append('Namespaced jQuery: ' + jQuery.fn.jquery + ' | jQuery.ui: ' + jQuery.ui.version);
  document.write($jh2[0].outerHTML);

  //Verify that jQueryUi works on OUR version of jQuery:
  $(document).ready(function(){
    console.log($('h2.namespaced').draggable());
  });

})(myJQuery, myJQuery);