Ajax gallery @ mix.lv

By Intars Students Last update Feb 23, 2009 — Installed 36 times. Daily Installs: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

There are 1 previous version of this script.

// ==UserScript==
// @name           Ajax gallery @ mix.lv
// @description    Gives Ajax navigation to mix.lv galleries 
// @include        http://*.mix.lv/articles/*
// ==/UserScript==

var script_to_append = function gallery_ajax(url){
  // Create XMLHttpRequest
  var httpRequest;
  
  httpRequest = new XMLHttpRequest();
  if (httpRequest.overrideMimeType) {
    httpRequest.overrideMimeType('text/xml');
  }
  
  if (!httpRequest) {
    // If, somehow, failed to create XMLHttpRequest, HALT!
    return false;
  }
  
  // What shall i do with Response?
  httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState == 4) {
      // If response is good (200), then start working on it
      if (httpRequest.status == 200) {
        // Here some RegExp, for fetching information that we will need
        var regx_image = new RegExp('<div class="gallery">\n\\s+<div class="top">(.*?)</div>\n\\s+<div class="image">\n\\s+<img src="(.*?)"/>\\s+</div>', 'm');
        var regx_bottom = new RegExp('<div class="bottom">\n\\s+(.*?)\n', 'm');
        var regx_link = new RegExp('<a href="(.*?)">(.*?)</a>', 'g');
        
        // Fetch!
        var found_image = regx_image.exec(httpRequest.responseText) != null ? regx_image.exec(httpRequest.responseText) : false;
        var found_bottom = regx_bottom.exec(httpRequest.responseText) != null ? regx_bottom.exec(httpRequest.responseText) : false;
        
        // No error's? Good!
        if (found_image != false && found_bottom != false){
          // Now time to make new Ajax style link's for next browsing
          var tmp_link_str = '';
          var tmp = new Array();
          var c = 0;
          
          while ((tmp = regx_link.exec(found_bottom[1])) != null){
            if (tmp[2] == 'Iepriekšējā' && c == 0){
              tmp_link_str = tmp_link_str + '<a href="javascript:gallery_ajax(\''+ tmp[1] +'\')">Iepriekšējā</a> | ';
            }else if (tmp[2] == 'Nākamā' && c == 0){
              tmp_link_str = tmp_link_str + 'Iepriekšējā | <a href="javascript:gallery_ajax(\''+ tmp[1] +'\')">Nākamā</a>'; 
              c = 2;
            }else if (tmp[2] == 'Nākamā' && c == 1){
              tmp_link_str = tmp_link_str + '<a href="javascript:gallery_ajax(\''+ tmp[1] +'\')">Nākamā</a>';
            }
            
            c++;
          }
          
          // Yes, i know ... strange, but working :)
          if (c == 1){
            tmp_link_str = tmp_link_str + 'Nākamā';
          }
          
          // Last check before, we are ok (Isn't there an error with creating Ajax styled link's?)
          if (tmp_link_str != ''){
            // Ok we are ready to GO!
            var image = document.getElementById('ajax-image').getElementsByTagName('img')[0];
            
            // Last second error?
            if (typeof(image) != "undefined"){
              // Nope :)
              image.setAttribute('src', found_image[2]);
              document.getElementById('ajax-top').innerHTML = found_image[1];
              document.getElementById('ajax-bottom').innerHTML = tmp_link_str;
            }
          }
        }
      }
      
      // Hide Apple
      var ajax_load = document.getElementById('ajax-top');
      ajax_load.style.background = '';
    }
  };
  
  httpRequest.open('GET', url, true);
  httpRequest.send('');
  
  // Show Apple
  var ajax_load = document.getElementById('ajax-top');
  ajax_load.style.background = 'url(http://mix.lv/img/icon-new-user.gif) no-repeat';
};

// For script appending inside mix.lv
function script_append(script){
  var head = document.getElementsByTagName('head')[0];
  var new_script = document.createElement('script');
  
  new_script.setAttribute('type', 'text/javascript');
  new_script.innerHTML = script;
  
  head.appendChild(new_script);
}

// We need to find where's "gallery"
var div_boxes = document.getElementsByTagName('div');

for (var i=0; i<div_boxes.length; i++){
  if (div_boxes[i].className == "gallery"){
    var gallery = div_boxes[i];
    i = div_boxes.length;
  }
}

if (typeof(gallery) != "undefined"){
  // Add "id" for galleries "div"'s, so we don't need getElementsByTagName again
  var gallery_boxes = gallery.getElementsByTagName('div');
  for (var i=0; i<gallery_boxes.length; i++){
    if (gallery_boxes[i].className != ''){
      gallery_boxes[i].id = 'ajax-' + gallery_boxes[i].className;
    }
  }
  
  // Change already set gallery navigation link's to Ajax based
  var gallery_links = gallery_boxes[2].getElementsByTagName('a');
  for (var i=0; i<gallery_links.length; i++){
    var tmp_href = gallery_links[i].getAttribute('href');
    
    if (tmp_href){
      gallery_links[i].setAttribute('href', 'javascript:gallery_ajax(\''+ tmp_href +'\');');
    }
  }
  
  // Append Ajax Gallery script inside mix.lv
  document.onLoad = script_append(script_to_append);
}