Advertisement
stsyn

FlyerMeter

Jun 12th, 2020
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         FlyerMeter
  3. // @version      0.2
  4. // @author       stsyn
  5. // @match        https://tabun.everypony.ru/*
  6. // @require      https://github.com/stsyn/derpibooruscripts/raw/master/YouBooru/libs/CreateElement.js
  7. // @grant        none
  8. // ==/UserScript==
  9.  
  10. (function() {
  11.   'use strict';
  12.   let cache;
  13.   function render(container, dose) {
  14.     let alertLevelItem;
  15.     if (dose > 0.33) alertLevelItem = createElement('img', {src: 'https://sun2.ufanet.userapi.com/FOl-s-OK8s3K5gL3QwvnqmmbXXddB6or0YnS0g/YxNmCCqUoCI.jpg', height: 50});
  16.     else if (dose > 0.20) alertLevelItem = createElement('img', {src: 'https://sun1.ufanet.userapi.com/SlOZjhYxCAj0gmjTO41_M0nRSPiqSPLAVKzlKg/pAWqlJUathU.jpg', height: 50});
  17.     else if (dose > 0.10) alertLevelItem = 'Значение многократно превышает допустимую концентрацию, воздержитесь от присутствия в помещении и не дышите.';
  18.     else if (dose > 0.05) alertLevelItem = 'Значение превышает ПДК. Соблюдайте дистанцию и применяйте средства индивидуальной защиты!';
  19.     else if (dose > 0.02) alertLevelItem = 'Значение приближается к предельно допустимой концентрации.';
  20.     else alertLevelItem = 'Табунпотребназдор одобряет.';
  21.     container.parentNode.appendChild(createElement('div', {
  22.       style: {
  23.         color: dose > 0.10 ? 'red' : 'inherit'
  24.       }
  25.     }, [
  26.       'Доза Флаера в комментах составляет ',
  27.       createElement('b', Math.round(dose*100)),
  28.       '%. ',
  29.       alertLevelItem
  30.     ]));
  31.   }
  32.  
  33.   try {
  34.     cache = JSON.parse(localStorage._flyerMeterCache) || {};
  35.   } catch (_) {
  36.     cache = {};
  37.   }
  38.  
  39.   Array.from(document.querySelectorAll('ul.topic-info')).forEach(item => {
  40.     const topicId = parseInt(item.querySelector('.topic-info-comments a').href.split('/').pop().split('.')[0]);
  41.     const total = item.querySelector('.topic-info-comments a').innerText;
  42.     if (cache[topicId] && cache[topicId].total == total) {
  43.       render(item, parseInt(cache[topicId].metric) / (parseInt(total) || 1));
  44.       return;
  45.     }
  46.  
  47.     fetch(`/blog/${topicId}.html`, {
  48.       credentials: 'omit'
  49.     })
  50.     .then(resp => resp.text())
  51.     .then(text => {
  52.       const content = createElement('div', text);
  53.       const total = parseInt(content.querySelector('#count-comments').innerText);
  54.       const metric = content.querySelectorAll('.comment-author a:first-child[href*="/Sasha-Flyer/"]').length;
  55.       cache[topicId] = {total, metric};
  56.       localStorage._flyerMeterCache = JSON.stringify(cache);
  57.       render(item, metric / (parseInt(total) || 1));
  58.     })
  59.   });
  60. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement