diff --git a/javascript_libraries.admin.inc b/javascript_libraries.admin.inc
index 0b22f7f..bf9db5d 100644
--- a/javascript_libraries.admin.inc
+++ b/javascript_libraries.admin.inc
@@ -397,6 +397,14 @@ function javascript_libraries_edit_form($form, &$form_state, $library = array())
     '#access' => empty($library['type']) || $library['type'] == 'external',
   );
 
+  $cache_external_access = empty($library['type']) || $library['type'] == 'external';
+  $form['cache_external'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Cache script locally'),
+    '#default_value' => isset($library['cache']) ? $library['cache'] : FALSE,
+    '#access' => $cache_external_access,
+  );
+
   $file_access = empty($library['type']) || $library['type'] == 'file';
   $form['js_file_upload'] = array(
     '#type' => 'managed_file',
@@ -495,6 +503,7 @@ function javascript_libraries_edit_form_submit($form, &$form_state) {
         'name' => $form_state['values']['name'],
         'weight' => $form['#library']['weight'],
         'uri' => $form_state['values']['external_url'],
+        'cache' => $form_state['values']['cache_external'],
       );
       variable_set('javascript_libraries_custom_libraries', $custom);
       break;
diff --git a/javascript_libraries.module b/javascript_libraries.module
index 63b10e9..658a2e2 100644
--- a/javascript_libraries.module
+++ b/javascript_libraries.module
@@ -132,6 +132,21 @@ function javascript_libraries_js_cache_clear() {
 }
 
 /**
+ * Implements hook_cron().
+ */
+function javascript_libraries_cron() {
+  $custom = variable_get('javascript_libraries_custom_libraries', array());
+  foreach ($custom as $library) {
+    $options = array();
+
+    // Get/build local cached versions of external scripts.
+    if ($library['type'] == 'external' && $library['cache'] == 1) {
+      javascript_libraries_cache($library['uri']);
+    }
+  }
+}
+
+/**
  * Implements hook_theme().
  */
 function javascript_libraries_theme() {
@@ -171,8 +186,17 @@ function javascript_libraries_init() {
     if ($library['scope'] == 'disabled') {
       continue;
     }
+
     $options = array();
-    $options['type'] = $library['type'];
+
+    // Get/build local cached versions of external scripts.
+    if ($library['type'] == 'external' && $library['cache'] == 1) {
+      $local_path = javascript_libraries_cache($library['uri']);
+      $options['type'] = 'file';
+    }
+    else {
+      $options['type'] = $library['type'];
+    }
     $options['scope'] = $library['scope'];
     $options['weight'] = $library['weight'] + $offset;
     $options['group'] = JS_THEME;
@@ -214,3 +238,58 @@ function javascript_libraries_library() {
 
   return $libraries;
 }
+
+/**
+ * Download/Synchronize/Cache tracking code file locally.
+ * 
+ * Stolen from google analytics module.
+ *
+ * @param $location
+ *   The full URL to the external javascript file.
+ * @param $sync_cached_file
+ *   Synchronize tracking code and update if remote file have changed.
+ * @return mixed
+ *   The path to the local javascript file on success, boolean FALSE on failure.
+ */
+function javascript_libraries_cache($location, $sync_cached_file = FALSE) {
+  $path = 'public://javascript_libraries';
+  $file_destination = $path . '/' . basename($location);
+
+  if (!file_exists($file_destination) || $sync_cached_file) {
+    // Download the library.
+    $result = drupal_http_request($location);
+
+    if ($result->code == 200) {
+      if (file_exists($file_destination)) {
+        // Synchronize tracking code and and replace local file if outdated.
+        $data_hash_local = drupal_hash_base64(file_get_contents($file_destination));
+        $data_hash_remote = drupal_hash_base64($result->data);
+        // Check that the files directory is writable.
+        if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {
+          // Save updated tracking code file to disk.
+          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
+          watchdog('javascript_libraries', t('Locally cached javascript library has been updated.'), array(), WATCHDOG_INFO);
+
+          // Change query-strings on css/js files to enforce reload for all users.
+          _drupal_flush_css_js();
+        }
+      }
+      else {
+        // Check that the files directory is writable.
+        if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
+          // There is no need to flush JS here as core refreshes JS caches
+          // automatically, if new files are added.
+          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
+          watchdog('javascript_libraries', 'Locally cached javascript library has been saved.', array(), WATCHDOG_INFO);
+
+          // Return the local JS file path.
+          return file_create_url($file_destination);
+        }
+      }
+    }
+  }
+  else {
+    // Return the local JS file path.
+    return file_create_url($file_destination);
+  }
+}
