You are here

js_injector.module in JS injector 6.2

Allows administrators to inject js into the page output based on configurable rules. Useful for adding simple js tweaks without modifying a site's official theme.

File

js_injector.module
View source
<?php

/**
 * @file
 * Allows administrators to inject js into the page output based on
 * configurable rules. Useful for adding simple js tweaks without modifying
 * a site's official theme.
 */

/**
 * Implementation of hook_help().
 */
function js_injector_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/settings/modules#description':
      $output .= t('Allows administrators to inject js (JavaScript Files) into the page output based on configurable rules.');
      break;
    case 'admin/settings/js_injector':
      $output .= '<p>' . t('Use js injection rules to add small snippets of js (JavaScript Files) to the page output when specific criteria are met.') . '</p>';
      break;
  }
  return $output;
}

/**
 * Implementation of hook_init().
 * Checks to see whether any js files should be added to the current page,
 * based on rules configured by the site administrator.
 */
function js_injector_init() {
  $js_rules = _js_injector_load_rule();
  foreach ($js_rules as $js_rule) {
    if (_js_injector_evaluate_rule($js_rule)) {
      switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
        case FILE_DOWNLOADS_PUBLIC:
          drupal_add_js(file_create_path(_js_injector_rule_path($js_rule['crid'])), 'module', $js_rule['scope'], FALSE, $js_rule['cache'], $js_rule['preprocess']);
          break;
        case FILE_DOWNLOADS_PRIVATE:
          drupal_add_js("system/files/js_injector_" . $js_rule['crid'] . ".js", 'module', $js_rule['scope'], FALSE, $js_rule['cache'], $js_rule['preprocess']);
          break;
      }
    }
  }
}

/**
 * Implementation of hook_file_download().
 */
function js_injector_file_download($filepath) {

  // Check if the file is controlled by the current module.
  if (preg_match('/^js_injector_\\d+.js$/', $filepath)) {
    return array(
      'Content-Type: Content-Type: application/x-javascript',
    );
  }
  return NULL;
}

/**
 * Implementation of hook_menu().
 * Defines menu callbacks for js Injector's configuration pages.
 */
function js_injector_menu() {
  $items = array(
    'admin/settings/js_injector' => array(
      'title' => 'js injector',
      'description' => 'Add js (JavaScript Files) to the page output based on configurable rules.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'js_injector_admin_form',
      ),
      'access arguments' => array(
        'administer js injection',
      ),
      'file' => 'js_injector.admin.inc',
    ),
    'admin/settings/js_injector/edit' => array(
      'title' => 'Edit js injector rule',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'js_injector_edit',
      ),
      'access arguments' => array(
        'administer js injection',
      ),
      'file' => 'js_injector.admin.inc',
      'type' => MENU_CALLBACK,
    ),
    'admin/settings/js_injector/add' => array(
      'title' => 'Add js injector rule',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'js_injector_edit',
      ),
      'access arguments' => array(
        'administer js injection',
      ),
      'file' => 'js_injector.admin.inc',
      'type' => MENU_CALLBACK,
    ),
    'admin/settings/js_injector/delete' => array(
      'title' => 'Delete js injector rule',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'js_injector_delete_confirm',
      ),
      'access arguments' => array(
        'administer js injection',
      ),
      'file' => 'js_injector.admin.inc',
      'type' => MENU_CALLBACK,
    ),
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function js_injector_theme() {
  $items['js_injector_admin_form'] = array(
    'arguments' => array(
      'form' => array(),
    ),
    'file' => 'js_injector.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function js_injector_perm() {
  return array(
    'administer js injection',
  );
}

/**
 * Helper function to load all js injection rules.
 */
function _js_injector_load_rule($crid = NULL, $reset = FALSE) {
  static $rules;
  if (!isset($rules) || $reset) {
    if (!$reset && ($cache = cache_get('js_injector:rules')) && !empty($cache->data)) {
      $rules = $cache->data;
    }
    else {
      $rules = array();
      $results = db_query("SELECT * FROM {js_injector_rule}");
      while ($rule = db_fetch_array($results)) {
        $rules[$rule['crid']] = $rule;
      }
      cache_set('js_injector:rules', $rules);
    }
  }
  if (is_numeric($crid)) {
    return $rules[$crid];
  }
  else {
    return $rules;
  }
}

/**
 * Helper function to delete an existing rule and its accompanying file.
 */
function _js_injector_delete_rule($crid) {
  if ($rule = _js_injector_load_rule($crid)) {
    file_delete(file_create_path(_js_injector_rule_path($crid)));
    db_query("DELETE FROM {js_injector_rule} WHERE crid = %d", $crid);
  }
}

/**
 * Helper function to determine whether a rule's conditions are met.
 */
function _js_injector_evaluate_rule($js_rule = array()) {

  // Match path if necessary.
  if (!empty($js_rule['rule_conditions'])) {
    if ($js_rule['rule_type'] < 2) {
      $path = drupal_get_path_alias($_GET['q']);

      // Compare with the internal and path alias (if any).
      $page_match = drupal_match_path($path, $js_rule['rule_conditions']);
      if ($path != $_GET['q']) {
        $page_match = $page_match || drupal_match_path($_GET['q'], $js_rule['rule_conditions']);
      }

      // When $js_rule['rule_type'] has a value of 0, the rule is matched on
      // all pages except those listed in $js_rule['rule_conditions'].
      // When set to 1, it is displayed only on those pages listed in
      // $js_rule['rule_type'].
      $page_match = !($js_rule['rule_type'] xor $page_match);
    }
    else {
      $page_match = drupal_eval($js_rule['rule_conditions']);
    }
  }
  else {
    $page_match = TRUE;
  }
  return $page_match;
}

/**
 * Helper function to get file path for a rule
 */
function _js_injector_rule_path($crid) {
  if (!empty($crid)) {
    $directory = file_directory_path();
    return $directory . '/js_injector_' . $crid . '.js';
  }
  return NULL;
}

Functions

Namesort descending Description
js_injector_file_download Implementation of hook_file_download().
js_injector_help Implementation of hook_help().
js_injector_init Implementation of hook_init(). Checks to see whether any js files should be added to the current page, based on rules configured by the site administrator.
js_injector_menu Implementation of hook_menu(). Defines menu callbacks for js Injector's configuration pages.
js_injector_perm Implementation of hook_perm().
js_injector_theme Implementation of hook_theme().
_js_injector_delete_rule Helper function to delete an existing rule and its accompanying file.
_js_injector_evaluate_rule Helper function to determine whether a rule's conditions are met.
_js_injector_load_rule Helper function to load all js injection rules.
_js_injector_rule_path Helper function to get file path for a rule