--- ajax_load.module	2011-08-15 15:52:58.000000000 +0200
+++ ajax_load.module	2011-08-16 16:18:33.000000000 +0200
@@ -95,68 +95,83 @@ function ajax_load_ajax_data_alter(&$dat
  * Return an array of data representing the scripts and CSS files on a page.
  */
 function ajax_data_get_data() {
-  $return = array();
-  $return['scripts'] = array();
-  foreach (array('header', 'footer') as $scope) {
-    $javascript = drupal_add_js(NULL, NULL, $scope);
-    foreach ($javascript as $type => $data) {
-      if (!$data) {
-        unset($javascript[$type]);
+  return array(
+    'scripts' => _ajax_load_extract_page_variables('js'),
+    'css'     => _ajax_load_extract_page_variables('css'),
+  );
+}
+
+/**
+ * Extract page template variables emmulating page.tpl.php preprocess.
+ *
+ * @see theme()
+ * @see drupal_get_js()
+ */
+function _ajax_load_extract_page_variables($variable_name) {
+  static $variables;
+  if (!isset($variables)) {
+    $base_path = base_path();
+    init_theme();
+    $hooks = theme_get_registry();
+    $hook = 'page';
+    $info = $hooks[$hook];
+    global $theme_path;
+    $temp = $theme_path;
+    // point path_to_theme() to the currently used theme path:
+    $theme_path = $hooks[$hook]['theme path'];
+
+    // The theme call is a template.
+    $variables = array(
+      'template_files' => array()
+    );
+    if (!empty($info['arguments'])) {
+      foreach ($info['arguments'] as $name => $default) {
+        $variables[$name] = $default;
       }
-      elseif ($type == 'setting') {
-        $javascript[$type] = call_user_func_array('array_merge_recursive', $data);
+    }
+
+    if (isset($info['preprocess functions']) && is_array($info['preprocess functions'])) {
+      // This construct ensures that we can keep a reference through
+      // call_user_func_array.
+      $args = array(&$variables, $hook);
+      foreach ($info['preprocess functions'] as $preprocess_function) {
+        if (function_exists($preprocess_function)) {
+          call_user_func_array($preprocess_function, $args);
+        }
       }
     }
-    $return['scripts'] = array_merge_recursive($return['scripts'], $javascript);
-  }
-  $return['css'] = drupal_add_css();
 
-  // Allow the color.module alter the CSS array as if it was processed from
-  // a regular page request. See phptemplate_preprocess_page() in garland.
-  if (module_exists('color')) {
-    init_theme();
-    _color_page_alter($return);
-  }
+    // restore path_to_theme()
+    $theme_path = $temp;
 
-  // Deal with jQuery Update module, if exists.
-  if (module_exists('jquery_update')) {
-    ajax_load_jquery_update_preprocess($return);
-  }
+    // Extract the list of CSS files sent to the page.
+    preg_match_all('`<link type="text/css" rel="stylesheet" media="(.*?)" href="' . $base_path . '(.*?)" />`', $variables['styles'], $matches, PREG_SET_ORDER);
+    $entries = array();
+    foreach ($matches as $match) {
+      if (!isset($entries[$match[1]])) {
+        $entries[$match[1]]['theme'] = array();
+      }
+      $entries[$match[1]]['theme'][$match[2]] = FALSE;
+    }
+    $variables['css'] = $entries;
 
-  return $return;
-}
+    // Extract the list of JavaScript files sent to the page.
+    $embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
+    $embed_suffix = "\n//--><!]]>\n";
+    $scripts = $variables['scripts'] . $variables['closure'];
+    preg_match_all('`<script.*src="' . $base_path . '(.*?)".*</script>`', $scripts, $matches, PREG_SET_ORDER);
+    $entries = array();
+    foreach ($matches as $match) {
+      $entries[$match[1]] = array('defer' => FALSE, 'preprocess' => FALSE, 'cache' => (!preg_match('`\?[0-9]+`', $match[1]) ? TRUE : FALSE));
+    }
+    $variables['js'] = array('theme' => $entries, 'inline' => array());
 
-/**
- * Replace Drupal core's jquery.js with the new one from jQuery Update module.
- *
- * @see jquery_update_preprocess_page()
- */
-function ajax_load_jquery_update_preprocess(&$variables) {
-  // Only do this for pages that have JavaScript on them.
-  if (!empty($variables['scripts'])) {
-  
-    // Perform the logic if either jQuery Update's jquery.js is newer than
-    // core's, or if we're using a different compression type.
-    if (variable_get('jquery_update_replace', TRUE) ||
-        variable_get('jquery_update_compression_type', 'pack') != 'pack') {
-
-      // Replace jquery.js first.
-      $new_jquery = array(jquery_update_jquery_path() => $variables['scripts']['core']['misc/jquery.js']);
-      $variables['scripts']['core'] = array_merge($new_jquery, $variables['scripts']['core']);
-      unset($variables['scripts']['core']['misc/jquery.js']);
-
-      // Loop through each of the required replacements.
-      foreach (jquery_update_get_replacements() as $type => $replacements) {
-        foreach ($replacements as $find => $replace) {
-          // If the file to replace is loaded on this page...
-          if (isset($variables['scripts'][$type][$find])) {
-            // Create a new entry for the replacement file, and unset the original one.
-            $replace = JQUERY_UPDATE_REPLACE_PATH . '/' . $replace;
-            $variables['scripts'][$type][$replace] = $variables['scripts'][$type][$find];
-            unset($variables['scripts'][$type][$find]);
-          }
-        }
-      } 
+    // Extract inline JavaScript code sent to the page.
+    preg_match_all('`<script type="text/javascript">(.+?)</script>`ms', $scripts, $matches, PREG_SET_ORDER);
+    foreach ($matches as $match) {
+      $variables['js']['inline'][] = array('code' => trim(str_replace(array($embed_prefix, $embed_suffix), array('', ''), $match[1])), 'defer' => FALSE);
     }
   }
+
+  return $variables[$variable_name];
 }
