Chain Warn

description

2021-11-28 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Chain Warn
// @namespace    namespace
// @version      0.6
// @description  description
// @author       tos
// @match        *.torn.com/*
// @grant        GM_addStyle
// @run-at       document-end
// @license      MIT
// ==/UserScript==

const alert_time = 100 //seconds
const min_chain = 10
const alert_sound = true

GM_addStyle(`
.cw_overlay {
  position: fixed;
  pointer-events: none;
  height: 100%;
  width: 100%;
  z-index: 20000;
  will-change: background-color;
}

.cw_warn {
  animation-name: cw_warn;
  animation-duration: 1.5s;
  animation-iteration-count: infinite;
}

@keyframes cw_warn {
  0% {
    background-color: #FF000000;
  }
  50% {
    background-color: #FF000055;
  }
  100 {
    background-color: #FF000000;
  }
}
`)

let warn_overlay = null
let chain_value = null

// playTone(gain, frequency, duration)
let a = new AudioContext()
function playTone(w,x,y){
 console.log("Gain:"+w, "Hz:"+x, "ms:"+y)
  osc = a.createOscillator()
  gain_node = a.createGain()
  osc.connect(gain_node)
  osc.frequency.value = x
  osc.type = "square"
  gain_node.connect(a.destination)
  gain_node.gain.value = w * 0.01
  osc.start(a.currentTime)
  osc.stop(a.currentTime + y *0.001)
}
const alert = () => { playTone(10,233,100); playTone(3,603,200) }
let alarm = null



const observer = new MutationObserver((mutations) => {
  for (const mutation of mutations) {
    try {
      if (mutation.target.parentElement.className.includes('bar-timeleft')) {
        //const a = performance.now()
        const t = mutation.target.data.split(':')
        const s = parseInt(t[0]) * 60 + parseInt(t[1])
        if (0 < s && s < alert_time && t.length < 3 && min_chain <= chain_value()) {
          warn_overlay.classList.add('cw_warn')
          if (!alarm && alert_sound) alarm = setInterval(alert, 1500)
        }
        else {
          warn_overlay.classList.remove('cw_warn')
          clearInterval(alarm)
          alarm = null
        }
        //const b = performance.now()
        //console.log(b-a)
      }
    } catch (err) { console.log(err) }
  }
})
const chain_bar = document.querySelector('[class^=chain-bar] [class^=bar-stats]')
console.log(chain_bar)
if (chain_bar) {
  document.querySelector('body').insertAdjacentHTML('afterbegin', `<div class="cw_overlay"></div>`)
  warn_overlay = document.querySelector('.cw_overlay')
  const cc_class = document.querySelector('[class^=chain-bar] [class^=bar-value]').className
  chain_value = () => parseInt(document.getElementsByClassName(cc_class)[0].innerText.replace(',', '').split('/')[0])
  observer.observe(chain_bar, { subtree: true, characterData: true })
}