Nexus Clash Display Known Classes

Stores some known character classes and adds then to Game interface.

目前為 2015-11-17 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Nexus Clash Display Known Classes
// @namespace   http://userscripts.org/users/125692
// @description Stores some known character classes and adds then to Game interface.
// @include        http://nexusclash.com/modules.php?name=Game*
// @include        http://www.nexusclash.com/modules.php?name=Game*
// @exclude        http://nexusclash.com/modules.php?name=Game&op=disconnect
// @exclude        http://www.nexusclash.com/modules.php?name=Game&op=disconnect
// @version     1.3
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// ==/UserScript==
var characterlinks;  
var characterlink;
var charid;
var charclass;
var charpagemaxtier;
var currentsetting;
if(window.location.href.match(/name=Game&op=faction&do=roster$/)){
  characterlinks = document.evaluate(
	  "//a[starts-with(@href,'modules.php?name=Game&op=character&id=')]",
	  document, 
	  null,
	  XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
	  null
  );
  //alert("running");
  if (characterlinks.snapshotLength>0){
    //alert(characterlinks.snapshotLength);
    for(i=0;characterlink=characterlinks.snapshotItem(i);i++){
      charid=characterlink.href.match(/\d+$/);
      //charid='#'+charid+"#";//non fixed length number needs to be made unique.
      charclass=characterlink.parentNode.nextElementSibling.nextElementSibling.textContent;
      //alert(charid+":"+charclass);
      GM_setValue(''+charid,charclass);
    }
  }
}
else if(window.location.href.match(/name=Game&op=character&id=/)){
//we are at a character page. Might as well take the id and the highest class.
//as the class line has the undelimited class plus previous classes we instead look for the class headings and take from there        
//<a href="wiki/index.php/Eternal Soldier">
//<b>Eternal Soldier</b>
//</a>
        characterlinks = document.evaluate(
	  "//a[starts-with(@href,'wiki/index.php/')]",
	  document, 
	  null,
	  XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
	  null
  );
  if (characterlinks.snapshotLength>0){
        charid=window.location.href.match(/\d+$/);
    if (!isNaN(charid)){
      //class we want is the last in the list
      charpagemaxtier=characterlinks.snapshotLength-1;//2 here equals t3
      //if (characterlinks.snapshotItem(1).href.match(/wiki\/index.php\/$/)){//if no t2 then set to t1
      //  charpagemaxtier=0;//no t2 means we are a mortal.
      //}
      //else if(characterlinks.snapshotItem(2).href.match(/wiki\/index.php\/$/)){//if no t3 then set to t2
      //  charpagemaxtier=1;//1 is t2 
      //}
      while(characterlinks.snapshotItem(charpagemaxtier).href.match(/wiki\/index.php\/$/)&&charpagemaxtier>0){
        charpagemaxtier-=1;
      }
      charclass=characterlinks.snapshotItem(charpagemaxtier).firstElementChild.textContent;// text of the <b> child node
      if (charclass!=""){
       GM_setValue(''+charid,charclass);
      }
    } 
}
  
  //while we are on the character page we ought to set a button and store away the settings.
  //either we have character class as text inline or we set as title text.
	var anewdiv=document.createElement('div');
  var anewp=document.createElement('p');
  var anewform=document.createElement('form');
	var radbut1=document.createElement('input');
  var radbut1text=document.createTextNode("As inline text");
	var radbut2=document.createElement('input');
  var radbut2text=document.createTextNode("As mouse over text");
	var brtag=document.createElement('br');
  anewp.textContent="Set the display type for Nexus Clash Display Known Classes";
	radbut1.name="classlistsetting"; 
	radbut1.value="inline";
  radbut1.type='radio';
	radbut2.name="classlistsetting"; 
	radbut2.value="titletext";
  radbut2.type='radio';
  //radbut1.appendChild(radbut1text);
  //radbut2.appendChild(radbut2text);
  anewdiv.style="background-color:lightgrey";
  //anewdiv.style="display:inline-block";
	anewform.insertBefore(radbut2,anewform.lastElementChild);
	anewform.insertBefore(radbut1,anewform.lastElementChild);
	anewform.insertBefore(brtag,anewform.lastElementChild);
  anewform.insertBefore(radbut1text,radbut1.nextElementSibling);
  anewform.insertBefore(radbut2text,radbut2.nextElementSibling);
  anewdiv.insertBefore(anewform,anewdiv.firstElementChild);
  anewdiv.insertBefore(anewp,anewdiv.firstElementChild);
  
  
  
	currentsetting=GM_getValue('textsetting','');
	if (currentsetting=='titletext'){
		radbut2.checked=true;
	}
	else if (currentsetting==""){
		GM_setValue('textsetting','inline');
		currentsetting='inline';
		radbut1.checked=true;
	}
	else {
		radbut1.checked=true;
	}
	var setevent=function(e) {
		var ebutton=e.target;
		GM_setValue('textsetting',ebutton.value);
	}
	//value="Visit User Profile"
	
	characterlinks = document.evaluate(
//	  "//input[starts-with(@value,'Visit User Profile')]",
   "//body",
    document, 
	  null,
	  XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
	  null
    );
    if (characterlinks.snapshotLength>0){
		characterlinks.snapshotItem(0).insertBefore(anewdiv,characterlinks.snapshotItem(0).lastElementChild);
		radbut1.addEventListener("click",setevent,true);
		radbut2.addEventListener("click",setevent,true);
	}
  
}
else{//if not on roster page storing the class value maybe we are on the game page and we ought to add the class names.
  //we want this link type and we want to grab the id number. gm_getvalue the class and then add it and a comma after the name.
  //<a href="javascript:SelectItem('target_id','996')" class="faction">a playful otter </a>
  characterlinks = document.evaluate(
	  "//a[starts-with(@href,'javascript:SelectItem')]",
	  document, 
	  null,
	  XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
	  null
  );
  //alert("running");
  if (characterlinks.snapshotLength>0){
  //get setting  
 	currentsetting=GM_getValue('textsetting','');
	if (currentsetting==""){
		GM_setValue('textsetting','inline');
		currentsetting='inline';
	}
    //alert(characterlinks.snapshotLength);
    for(i=0;characterlink=characterlinks.snapshotItem(i);i++){
      charid=characterlink.href.match(/\d+/);
      //charid='#'+charid+"#";//non fixed length number needs to be made unique.
      charclass = GM_getValue(""+charid,"");
      if(charclass!=""){
        if(currentsetting=='inline'){
          GM_addStyle("A.classlevel{color:#666666;}");
          //charclass=charclass[0].match(/[AEIOU]/)?", an "+charclass:", a "+charclass; // and a or an
          characterlink.nextElementSibling.textContent=charclass+"-"+characterlink.nextElementSibling.textContent; 
          characterlink.nextElementSibling.className='classlevel';
        }
        else{
          characterlink.nextElementSibling.title=charclass;
        }
      }  
    }
  }
}