TW Fred Timer

Fred's Timer View

目前為 2015-04-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        TW Fred Timer
// @name:ru     TW Fred Timer
// @namespace   TW Fred Timer
// @description Fred's Timer View
// @description:ru Счет времени бандита Фреда
// @include         http://*.the-west.*/game.php*
// @include         https://*.the-west.*/game.php*

// @version     0.03
// @grant       none

// ==/UserScript==


function TWFredScript(fn) {
	var script = document.createElement('script');
	script.setAttribute("type", "application/javascript");
	script.textContent = '(' + fn + ')();';
	document.body.appendChild(script);
	document.body.removeChild(script);
}

TWFredScript(function() {
  
  var VERSION = "0.03";
  var NAME = "TW Fred Timer";
  var installURL = '';
  
  
  fcContainer = $("<div />", 
                   { 
id: "twfred_container", 
style: "position: absolute; top: 64px; right: 50%; margin-right: 140px; z-index: 10; width: 142px; height: 16px;  background: url('/images/interface/character/character.png') no-repeat scroll left -160px transparent;" 
                   }
                  );
  
  fcContainer.appendTo("#user-interface");
  
  fcWrap = $('<div />',
              {
id: "twfred_wrap",
style:"padding-top:1px; font-size:10px; color:#FFF; text-align:center;",
              }
            );
    
  fcContainer.append(fcWrap);
  
  TWFred = {
    wofid:2, // don't know where find it
    diff: 0,
    timer:2, // seconds
    execCount:0,
    reInitVal:60, // execs to reinit data - on start
    reInitValN:60, // execs to reinit data - on normal work
    reInitValI:7, // execs to reinit data - on zero Timer
    clickObj: null,
    timerId: 0,
  };
  
  fcWrap.click(function(){TWFred.click()});
  
  TWFred.init = function()
  {
    TWFred.execCount = 0;
    TWFred.reInitVal = TWFred.reInitValN;
    TWFred.getClicker();
    
    $.post("/game.php?window=wheeloffortune&mode=init", {wofid:TWFred.wofid},
           function(data)
           {
             //console.log(data);
             
             if (data.error) {TWFred.destroy(); return;}
             
             var  diff = 0;
             var cd = data.mode.cooldowns;
             if (cd.length) // cuz no data at end of Event
             {
               cd = cd[0].cdstamp
               var stime = Game.getServerTime();
               diff = cd-stime;
             }
             TWFred.diff = diff;
             
             //TWFred.showTimer();
             TWFredTimer();
           }
          ); //post
  };
  
  TWFred.showTimer = function()
  {
    var str = TWFred.diff.formatDuration();
    fcWrap.html(str);
  };
  
  TWFred.doTimer = function()
  {
    //console.log('doTimer:' + TWFred.execCount + ',' + TWFred.reInitVal);
    TWFred.timerId = 0;
    if (undefined === TWFred) return;
    
    if (!TWFred.clickObj) TWFred.getClicker(); // init clicker
    
    var diff = TWFred.diff - TWFred.timer;
    if (diff < 0) diff = 0;
    if (!diff) TWFred.reInitVal = TWFred.reInitValI; 
    TWFred.diff = diff;
        
    TWFred.showTimer();
    TWFred.execCount++;
    if (TWFred.execCount > TWFred.reInitVal)
    {
      TWFred.init();
    }
    else
    {
      TWFredTimer();
    }
  };
  
  
  TWFred.getClicker = function()
  {
    if (TWFred.clickObj) return;
    
    var bar = WestUi.NotiBar.getBar();
        
    if (!bar) return;
    bar = bar.list;
        
    TWFred.reInitVal = TWFred.reInitValN;
    
    $.each(bar, function(key,val)
    {
      //console.log(val);
           
      if (val.uid == 'easter2015')
      {
        TWFred.clickObj = val.element;
      }
    });
    
    fcWrap.css("cursor","pointer");
    if (!TWFred.clickObj) TWFred.destroy();
  };
  
  
  TWFred.click = function()
  {
    if (!TWFred.clickObj) return;
    TWFred.init();
    TWFred.clickObj.click();
  };
  
  
  TWFred.destroy = function()
  {
    fcContainer.remove();
    delete TWFred;
  };
  
  function TWFredTimer()
  {
    if (undefined === TWFred) return;
    if (!TWFred.timerId) TWFred.timerId = setTimeout(TWFred.doTimer,TWFred.timer * 1000);
  };
  
  TWFred.init();
  
}
);