hwm_work_find

Поиск и установка на работу с любой страницы игры

As of 2015-11-15 08:16:26 UTC. See the latest version.

//
// ==UserScript==
// @name           hwm_work_find
// @author         Pahan https://greasyfork.org/uk/users/18377-pahan
// @namespace      hwm_pahan
// @description    Поиск и установка на работу с любой страницы игры
// @homepage       https://greasyfork.org/en/users/18377-pahan
// @icon           http://dcdn.heroeswm.ru/avatars/30/nc-5/30547.gif
// @version        1.5
// @encoding 	   utf-8
// @include        http://www.heroeswm.ru/*
// @include        http://qrator.heroeswm.ru/*
// @include        http://178.248.235.15/*
// @include        http://www.lordswm.com/*
// @exclude        */rightcol.php*
// @exclude        */ch_box.php*
// @exclude        */chat*
// @exclude        */ticker.html*
// @exclude        */frames*
// @exclude        */brd.php*
// @grant          GM_deleteValue
// @grant          GM_getValue
// @grant          GM_listValues
// @grant          GM_setValue
// @grant          GM_addStyle
// @grant          GM_log
// @grant          GM_openInTab
// ==/UserScript==

if (typeof GM_deleteValue != 'function') {
	this.GM_getValue=function (key,def) {return localStorage[key] || def;};
	this.GM_setValue=function (key,value) {return localStorage[key]=value;};
	this.GM_deleteValue=function (key) {return delete localStorage[key];};

	this.GM_addStyle=function (key) {
		var style = document.createElement('style');
		style.textContent = key;
		document.querySelector("head").appendChild(style);
	}
}
if (typeof GM_listValues != 'function') {
	this.GM_listValues=function () {
		var values = [];
		for (var i=0; i<localStorage.length; i++) {
			values.push(localStorage.key(i));
		}
		return values;
	}
}

function addEvent(elem, evType, fn) {
//    elem["on" + evType] = fn;
	if (elem.addEventListener) {
		elem.addEventListener(evType, fn, false);
	}
	else if (elem.attachEvent) {
		elem.attachEvent("on" + evType, fn);
	}
	else {
		elem["on" + evType] = fn;
	}
}

function $(id) { return document.querySelector(id); } 

function URLAttrValueGet(attr_name, aurl)
{
 attr_name = attr_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 var regexS = "[\\?&]" + attr_name + "=([^&#]*)";
 var regex = new RegExp( regexS );
 var results = regex.exec( aurl );
 if( results == null )
  return "";
 else
  return results[1];
}

function Ajax(AMethod, AURL, AParams, ACallBackFunc)
{
  var LRequest = new XMLHttpRequest();
  LRequest.open(AMethod, AURL, true);
  LRequest.overrideMimeType('text/html; charset=windows-1251');
  LRequest.send(AParams);
  LRequest.onreadystatechange = function()
  {
    if (LRequest.readyState == 4)
    {
      ACallBackFunc(LRequest.responseText);
    }  
  };
} 

function Trim(AText)
{
  return AText.replace(/^\s+|\s+$|^(&nbsp;)+|(&nbsp;)+$/g, '');
}

// ---------------------------------

FindWorkHTML = '';

function close_find_work_info()
{
  bg = $('#bgOverlayFindWork') ;
  bgc = $('#bgCenterFindWork') ;
  if( bg ) {
      bg.style.display = bgc.style.display = 'none' ;
  }
}

function ShowFindWorkHTML(AFindWorkHTML)
{
  FindWorkHTML = AFindWorkHTML;
  
  bg = $('#bgOverlayFindWork') ;
  bgc = $('#bgCenterFindWork') ;
  if( !bg ) {
      bg = document.createElement('div') ;
      bg.id = 'bgOverlayFindWork' ;
      document.body.appendChild( bg );
      bg.style.position = 'absolute' ;
      bg.style.left = '0';
      bg.style.width = '100%';
      bg.style.height = '100%';
      bg.style.background = "#000000";
      bg.style.opacity = "0.5";
      bg.addEventListener( "click", close_find_work_info , false );

      bgc = document.createElement('div') ;
      bgc.id = 'bgCenterFindWork' ;
      document.body.appendChild( bgc );
      bgc.style.position = 'absolute' ;
      bgc.style.width = '600px';
      bgc.style.background = "#F6F3EA";
      bgc.style.left = ( ( document.body.offsetWidth - 600 ) / 2 ) + 'px' ;
      bgc.style.zIndex = "1105";
  }

  bgc.innerHTML = 
    '<div style="border:1px solid #abc;padding:5px;margin:2px;">' +
    '  <div style="float:right;border:1px solid #abc;width:15px;height:15px;text-align:center;cursor:pointer;" id="bt_closeFindWork" title="Закрыть">x</div>' +
    '  <center>' +
    '    <table>' +
    '      <tr>' +
    '        <td>' + FindWorkHTML + '</td>' +
    '      </tr>' +
    '    </table>' +    '',
    '  </center>' +
    '</div>';

  $("#bt_closeFindWork").addEventListener( "click", close_find_work_info , false );    
  
  bg.style.top = (-document.body.scrollTop)+'px';
  bgc.style.top = ( document.body.scrollTop + 100 ) + 'px';
  bg.style.display = bgc.style.display = 'block' ;    
}

function GetObjectInfo(AObjHTML)
{
  var LFirst = AObjHTML.indexOf('Тип:');
  var LEnd = AObjHTML.indexOf('Окончание смены');
  var LLast = AObjHTML.indexOf('</td>', LEnd);
  return AObjHTML.substr(LFirst, LLast - LFirst);
}

function ObjectProcess(AObjHTML)
{
  AObjHTML = AObjHTML.replace(/[\n\r]/g, ' ').replace(/\s{2,}/g, ' ');
  var re = /Вы уже устроены\./gim;
  var re2 = /Прошло меньше часа с последнего устройства на работу\. Ждите\./gim;
  var res;
  if (((res = re.exec(AObjHTML)) != null) || (res = re2.exec(AObjHTML)) != null)
    ShowFindWorkHTML(res[0]);
  else
  {
    re = /Свободных мест\: <b>0<\/b>/gim;
    if ((res = re.exec(AObjHTML)) != null)
    {}
    else
    {
//      re = /<form[^>]*name="working"[^>]*>.*<\/form>/gim;
      re = /<form[^>]*name=working[^>]*>.*<\/form>/gim;
      if ((res = re.exec(AObjHTML)) != null)
      {
        ShowFindWorkHTML(GetObjectInfo(AObjHTML) + res[0]);
      }
    }
  }  
} 

function FindWorkObject(APlaceIndex, AObjIndex, APlaceHTML)
{
//  href="object-info.php?id=163">»»»</a>
  var re = /<[^>]*href='([^']*)'[^>]*>&raquo;&raquo;&raquo;<\/a>/gim;
  var res;
  var LLink;  
  var LFindCount = 0;
  while ((res = re.exec(APlaceHTML)) != null)
  {
    LLink = res[1];
    if (LFindCount == AObjIndex)
    {
      Ajax('GET', LLink, null,
        function(AObjHTML)
        {
          ObjectProcess(AObjHTML);
          if (FindWorkHTML == '')
            FindWorkObject(APlaceIndex, AObjIndex + 1, APlaceHTML);
        }
      );
    }
    LFindCount++;
  }

//  $("#find_work").innerHTML = $("#find_work").innerHTML + '<br>' +     
//    'AObjIndex: ' + AObjIndex + ', LFindCount: ' + LFindCount;
          
  if ((FindWorkHTML == '') && (AObjIndex >= LFindCount)) 
    FindWorkProcess(APlaceIndex + 1);
}

function FindWorkProcess(APlaceIndex)
{
  var works = ['sh', 'fc', 'mn']; 

  if (APlaceIndex >= works.length)
  {
    ShowFindWorkHTML('Поиск работы завершен.<br>' +
      'Все места заняты или недостаточно золота на объектах.<br>' +
      'Смените локацию, или попробуйте позже.');
    return;
  }

  Ajax('GET', '/map.php?st=' + works[APlaceIndex], null,
    function(AHTML)
    {
      FindWorkObject(APlaceIndex, 0, AHTML);
    }
  );
}

function AddWorkFindHref()
{
  var LLinks = document.querySelectorAll('a[href^="map.php"]');
  var LLink;
  if (LLinks)
  {                
    for(i = 0; i < LLinks.length; i++)
    {                   
      LLink = LLinks[i];
      if (LLink.innerHTML == '<b>Карта</b>')
      {
        LNewHref = document.createElement('a');
        LNewHref.href = '#';
        LNewHref.style = "text-decoration: none;color: #f5c137;" 
        LNewHref.innerHTML = '<b>ГР</b>';
        LNewHref.title = 'Найти место работы';
        LNewHref.id = 'find_work';
        LLink.parentNode.appendChild(LNewHref);
        
        addEvent($("#find_work"), "click",
          function()
          {
            FindWorkHTML = '';
            FindWorkProcess(0);
          }
        );
      }
    }
  }
}

function ProcessMain()
{  
  AddWorkFindHref();
}

ProcessMain();