Maxsior / sdamege hacks

// ==UserScript==
// @name        sdamege hacks
// @updateURL   https://openuserjs.org/meta/Maxsior/sdamege_hacks.meta.js
// @description Дополнительный функционал sdamgia.ru
// @include     http*://*.sdamgia.ru/test?id=*
// @version     5.4
// @grant       none
// @icon        http://sdamgia.ru/favicon.ico
// @license MIT
// ==/UserScript==

console.log('starting sdamege hacks...')

function popup(url, x, y) {
  var div = document.getElementById('popup');
  if (!div) {
    div = document.createElement('iframe');
    div.id = 'popup';
    div.style.position = 'absolute';
    div.style.backgroundColor = '#FFD6A3';
    div.style.width = '60vw';
    div.style.height = '60vh';
    div.style.overflow = 'auto';
    div.style.padding = '5px';
    div.style.border = '5px solid #bac2ff';
    document.body.appendChild(div);
    document.body.addEventListener('click', function () {
      div.style.display = 'none';
    });
  }
  div.style.display = 'block';
  div.style.top = y + 'px';
  div.style.left = x + 'px';
  div.src = url;
}

console.log('init popup func...');

function bar() {
  console.log('sdamege hacks: init bar...');
  var div = document.createElement('div');
  div.innerHTML += '<u>Клик по номеру задачи</u> показывает ответ на неё<br>Ещё фичи:';
  div.style.display = 'none';
  div.style.borderTop = '1px solid black';
  var values = [
    'остановить таймер',
    'установить время',
    'запустить таймер'
  ];
  for (var i = 0; i < values.length; i++) {
    var b = document.createElement('input');
    b.type = 'button';
    b.style.display = 'block';
    b.id = 'm_b_' + i;
    b.value = values[i];
    div.appendChild(b);
  }
  
  var head = document.createElement('div');
  head.style.textAlign = 'center';
  head.style.cursor = 'pointer';
  head.style.userSelect = 'none';
  head.style.MozUserSelect = 'none';
  head.style.WebkitUserSelect = 'none';
  head.addEventListener('click', function () {
    div.style.display = (div.style.display == 'block') ? 'none' : 'block';
  });
  head.innerHTML = '<b>__Хаки__</b>';
  
  var wrap = document.createElement("div");
  wrap.style.cssText = '\
          background-color:#f0f0f0;\
          border-radius:5px;\
          border:1px solid black;\
          font-size:10pt;\
          padding: 5px;\
          width:160px;\
          position:fixed;\
          left:5px;\
          top:0;\
  ';
  wrap.appendChild(head);
  wrap.appendChild(div);
  document.body.appendChild(wrap);
  
  console.log('sdamege hacks: bar added...');
  
  console.log('sdamege hacks: binding hacks...');
  window.hack_funcs = [
    function () {
      clearInterval(window.ticker);
    },
    function () {
      var timestr = 0;
      clearInterval(window.ticker);
      try {
        var rand_h = Math.round(Math.random() * 3),
            rand_m = Math.round(Math.random() * 59),
            rand_s = Math.round(Math.random() * 59);
        var t = prompt('Время в формате  часы:минуты:секунды', rand_h + ':' + rand_m + ':' + rand_s).split(':');
        t[0] = + t[0];
        t[1] = + t[1];
        t[2] = + t[2];
        tim = (t[0] * 60 + t[1]) * 60 + t[2];
        if (t[0] > 3 || t[1] > 59 || t[2] > 59 || tim > 14100 || tim < 1) {
          alert('Вы ввели некорректное время!');
          throw new Error('Wrong time');
        }
      } catch (e) {
        return 1;
      }
      document.getElementById('timer').value = tim;
      window.ticker = window.setInterval(tick, 1000);
    },
    function () {
      clearInterval(window.ticker);
      window.ticker = window.setInterval(tick, 1000);
    }
  ];
  for (var i = 0; i < values.length; i++)
    document.getElementById('m_b_' + i).addEventListener('click', window.hack_funcs[i]);
  console.log('sdamege hacks: bar is ready...');
}


function decorator(el) {
  return function (e) {
    e.stopPropagation();
    console.log('clicked', el.innerHTML);
    var pid = el.nextElementSibling.querySelector('.prob_nums > a').innerHTML
    popup('//' + location.host + '/test?print=true&svg=0&sol=true&ans=true&pid=' + pid, e.pageX, e.pageY);
  };
}

window.addEventListener('DOMContentLoaded', function () {
  // console.log('sdamege hacks: binding events...');
  var tasks = document.getElementsByClassName('prob_num');
  for (var i = 0; i < tasks.length; i++)
    tasks[i].addEventListener('click', decorator(tasks[i]));
  document.querySelector('input[value="Пауза"]').remove();
});

bar();