How do I preprocess "extra fields"?
It depends on what fields you are dealing with.

hook_field_extra_fields() allows you to define extra fields for any entity, then the field itself is added in a hook that runs when the entity's content is being generated.

That is the place where you need to add the js.

In that hook you can modify the fields you care about and add your js using drupal_add_js() or #attached.

#attached is generally more resilient to caching as it lives with the field render array.

Use #attached like this:

$form['YOURELEMENT']['#attached']['js'][] = drupal_get_path('module', 'MODULENAME') . '/javascript.js';


For example, if the field you want is being added to nodes, you would implement hook_node_view() or hook_node_view_alter() something like this:

function MODULENAME_node_view($node, $view_mode, $langcode) {
  if (isset($node-&gt;content['EXTRAFIELD'])) {
    $node-&gt;content['EXTRAFIELD']['#attached']['js'] = drupal_get_path('module', 'MODULENAME') . '/javascript.js';
  }
}


or in hook_node_view_alter() it would be like this:

function MODULENAME_node_view_alter(&amp;$build) {
  if (isset($build['EXTRAFIELD'])) {
    // Change its weight.
    $build['EXTRAFIELD']['#attached']['js'] = drupal_get_path('module', 'MODULENAME') . '/javascript.js';
  }
}


The same principle can be applied to other entity types, like users, which would use hook_user_view() or hook_user_view_alter().

If your field is on multiple entity types you might have to use multiple of these implementations, or even better, you could use hook_entity_view() or hook_entity_view_alter(), which allow you to target all entity types in the one place.

If you need to make sure your hook runs after all other modules' implementations of that same hook then you can either set the weight of your module higher than others or use hook_module_implements_alter() to make sure your module runs last.