Saltar para o conteúdo

Usuário:Higor Douglas/Vigiar páginas novas.js

Origem: Wikipédia, a enciclopédia livre.

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5.
//Veja a documentação [[Usuário:Higor Douglas/Vigiar páginas novas]]

var npp_http;
var npp_enabled;
var npp_num_pages;
var npp_refresh;
var npp_num_idle_req;
var npp_curr_idle_req;

var npp_str_no_ajax = "Parece haver um problema usando o script Vigiar páginas novas";
var npp_str_box_title = "Páginas novas";
var npp_str_box_title_updating = "Páginas novas (...)";
var npp_str_box_title_failed = "Páginas novas (Error)";
var npp_str_enable = "Expandir";
var npp_str_disable = "Esconder";

$( npp_init );
 
/* initalise */
function npp_init() {
 

  if (npp_enabled == null) {
    npp_enabled = false;
  }
  if (npp_num_pages == null) {
    npp_num_pages = 10;
  }
  if (npp_refresh == null) {
    npp_refresh = 5;
  }
  if (npp_num_idle_req == null) {
    npp_num_idle_req = 60;
  }


  if (npp_num_pages > 50) {
    npp_num_pages = 50;
  }
  if (npp_num_pages < 1) {
    npp_num_pages = 1;
  }
  if (npp_refresh < 2) {
    npp_refresh = 2;
  }
  if (npp_num_idle_req > 1000) {
    npp_num_idle_req = 1000;
  }
  if (npp_num_idle_req < 5) {
    npp_num_idle_req = 5;
  }


  if (document.cookie.length > 0) {
    var c_start = document.cookie.indexOf("npp_show_box=");
    if (c_start != -1) { 
      c_start = c_start + 13; 
      var c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) {
        c_end = document.cookie.length;
      }
 
      if (document.cookie.substring(c_start, c_end) == "yes") {
        npp_enabled = true;
      } else {
        npp_enabled = false;
      }
    } 
  }
 

  npp_curr_idle_req = 0;
  if (npp_enabled == true) {
    npp_ajax_request();
  } else {
    npp_draw_disabled_box();
  }
}
 
/* init ajax */
function npp_create_request() {
  try {
    npp_http = new XMLHttpRequest();
 
  } catch (e) {
    try {
      npp_http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        npp_http = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        return false;
      }
    }
  }
 
  npp_http.onreadystatechange = function() {
    if(npp_http.readyState == 4) npp_ajax_response();
  }
 
  return true;
}
 
/* make a request */
function npp_ajax_request() {

  npp_curr_idle_req++;
  if (npp_curr_idle_req > npp_num_idle_req) {
    npp_disable_box();
  }


  if (npp_enabled == false) return;


  var cur_box = document.getElementById('p-newpages');
  if (cur_box != null) {
    cur_box.firstChild.firstChild.data = npp_str_box_title_updating;
  }
 
  if (npp_create_request () == false) {
    if (cur_box != null) {
      cur_box.firstChild.firstChild.data = npp_str_box_title_failed;
    } else {
      alert (npp_str_no_ajax);
    }
  }
 

  npp_http.open("GET", "/w/api.php?action=query&format=xml&list=recentchanges&rcshow=!bot|!redirect&rctype=new&rcnamespace=0&rcprop=title|timestamp|ids|patrolled&rclimit=" + npp_num_pages, true);
  npp_http.send(null);
}

function npp_draw_disabled_box() {
  if (skin == 'vector') {
    npp_draw_disabled_box_vector();
  } else {
    npp_draw_disabled_box_monobook();
  }
}

function npp_ajax_response() {
  if (skin == 'vector') {
    npp_ajax_response_vector();
  } else {
    npp_ajax_response_monobook();
  }


  setTimeout("npp_ajax_request()", npp_refresh * 1000);
}

function npp_disable_box() {
  npp_enabled = false;
  npp_draw_disabled_box();
  document.cookie = "npp_show_box=no; path=/";
}
 
function npp_enable_box() {
  npp_enabled = true;
  npp_curr_idle_req = 0;
  document.cookie = "npp_show_box=yes; path=/";
  npp_ajax_request();
}


/* Draw disabled (monobook) */
function npp_draw_disabled_box_monobook() {

  var link_div = document.createElement('div');
  link_div.className = 'pBody';
  var div = document.createElement('div');
  div.setAttribute('id', 'p-newpages');
  div.className = 'portlet';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode(npp_str_box_title));
  div.appendChild(heading);
  div.appendChild(link_div);
 

  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  var a = document.createElement('a');
  a.appendChild(document.createTextNode(npp_str_enable));
  a.onclick = npp_enable_box;
  p.appendChild(a);
  link_div.appendChild(p);
 

  var old_div = document.getElementById('p-newpages');
  var side_col = document.getElementById('column-one');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-search');
    side_col.insertBefore(div, node);
  }
}

/* Draw response (monobook) */
function npp_ajax_response_monobook() {
 
  var items = npp_http.responseXML.getElementsByTagName('rc');
 

  var link_div = document.createElement('div');
  link_div.className = 'pBody';
  var list = document.createElement('ul');
  link_div.appendChild(list);
 

  for (var i = 0; i < items.length; i++) {
    var item_name = items[i].getAttribute('title');
    var rcid = items[i].getAttribute('rcid');
    var patrolled = items[i].getAttribute('patrolled') != null;
 
    item_name = item_name.replace(/&/, "%26");
    var item_url = 'http://pt.wikipedia.org/w/index.php?title=' + item_name + '&rcid=' + rcid + '&redirect=no';
 
    a = document.createElement('a');
    a.setAttribute('href', item_url);
    a.appendChild(document.createTextNode(item_name));

    var li = document.createElement('li');
    li.appendChild(a);
    if (!patrolled) {
        li.setAttribute('class', 'not-patrolled');
    }
    list.appendChild(li);
  }
 

  var div = document.createElement('div');
  div.setAttribute('id', 'p-newpages');
  div.className = 'portlet';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode(npp_str_box_title));
  div.appendChild(heading);
  div.appendChild(link_div);
 

  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  p.style.textAlign = 'right';
  a = document.createElement('a');
  a.appendChild(document.createTextNode(npp_str_disable));
  a.onclick = npp_disable_box;
  p.appendChild(a);
  link_div.appendChild(p);
 

  var old_div = document.getElementById('p-newpages');
  var side_col = document.getElementById('column-one');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-search');
    side_col.insertBefore(div, node);
  }
}


/* Draw disabled box (vector skin) */
function npp_draw_disabled_box_vector() {

  var link_div = document.createElement('div');
  link_div.className = 'body';
  var div = document.createElement('div');
  div.setAttribute('id', 'p-newpages');
  div.className = 'portal';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode(npp_str_box_title));
  div.appendChild(heading);
  div.appendChild(link_div);
 

  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  var a = document.createElement('a');
  a.appendChild(document.createTextNode(npp_str_enable));
  a.onclick = npp_enable_box;
  p.appendChild(a);
  link_div.appendChild(p);
 

  var old_div = document.getElementById('p-newpages');
  var side_col = document.getElementById('panel');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-interaction');
    side_col.insertBefore(div, node);
  }
}

/* Draw response (vector skin) */
function npp_ajax_response_vector() {
 
  var items = npp_http.responseXML.getElementsByTagName('rc');
 

  var link_div = document.createElement('div');
  link_div.className = 'body';
  var list = document.createElement('ul');
  link_div.appendChild(list);
 

  for (var i = 0; i < items.length; i++) {
    var item_name = items[i].getAttribute('title');
    var rcid = items[i].getAttribute('rcid');
    var patrolled = items[i].getAttribute('patrolled') != null;
 
    item_name = item_name.replace(/&/, "%26");
    var item_url = 'http://pt.wikipedia.org/w/index.php?title=' + item_name + '&rcid=' + rcid + '&redirect=no';
 
    a = document.createElement('a');
    a.setAttribute('href', item_url);
    
    a.appendChild(document.createTextNode(item_name));
 
    var li = document.createElement('li');
    li.appendChild(a);
    if (!patrolled) {
        
        li.setAttribute('style','background-color: #e7e7e7');
        

    }
        list.appendChild(li);
  }
 
  var div = document.createElement('div');
  div.setAttribute('id', 'p-newpages');
  div.className = 'portal';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode(npp_str_box_title));
  div.appendChild(heading);
  div.appendChild(link_div);
 

  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  p.style.textAlign = 'left';
  a = document.createElement('a');
  a.appendChild(document.createTextNode(npp_str_disable));
  a.onclick = npp_disable_box;
  p.appendChild(a);
  link_div.appendChild(p);
 

  var old_div = document.getElementById('p-newpages');
  var side_col = document.getElementById('panel');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-interaction');
    side_col.insertBefore(div, node);
  }
}