[GreasyFork] highlight stats changes in own scripts

try to take over the world!

Від 16.02.2020. Дивіться остання версія.

// ==UserScript==
// @name         [GreasyFork] highlight stats changes in own scripts
// @namespace    https://greasyfork.org/users/321857-anakunda
// @version      1.01
// @description  try to take over the world!
// @author       Anakunda
// @iconURL      https://greasyfork.org/assets/blacklogo16-bc64b9f7afdc9be4cbfa58bdd5fc2e5c098ad4bca3ad513a27b15602083fd5bc.png
// @match        https://greasyfork.org/*/users/*
// @match        https://greasyfork.org/users/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// ==/UserScript==

(function() {
  'use strict';

  var scripts = document.querySelectorAll('ol#user-script-list > li > article');
  if (scripts.length <= 0) return;
  try { var lastStats = new Map(JSON.parse(GM_getValue('stats'))) } catch(e) { lastStats = new Map() }
  var articles = Array.from(scripts).map(article => queryStats(article.parentNode.dataset.scriptId).then(function(statData) {
	const id = parseInt(article.parentNode.dataset.scriptId);
	const installs = parseInt(article.parentNode.dataset.scriptTotalInstalls);
	const rating = parseFloat(article.parentNode.dataset.scriptRatingScore);
	var ref, _lastStats = lastStats.get(id) || {};
	if (statData.installs != installs) console.warn('Script ' + id + ' total installs inconsistency:',
		statData.installs, '≠', installs);
	if (_lastStats.installs != undefined && statData.installs /*installs*/ != _lastStats.installs
		&& (ref = article.querySelector('dd.script-list-total-installs')) != null) {
	  ref.style.backgroundColor = 'greenyellow';
	  var elem = document.createElement('span');
	  elem.innerHTML = ' (<b>+'.concat((statData.installs /*installs*/ - (_lastStats.installs || 0)), '</b>)');
	  ref.append(elem);
	}
	if (_lastStats.rating != undefined && rating != _lastStats.rating
		&& (ref = article.querySelector('dd.script-list-ratings')) != null) {
	  ref.style.backgroundColor = 'greenyellow';
	  elem = document.createElement('span');
	  elem.innerHTML = ' (<b>'.concat(rating, '</b>)');
	  ref.append(elem);
	}
	if ((ref = article.querySelector('dl.inline-script-stats')) != null) {
	  const className = 'script-list-update-checks';
	  elem = document.createElement('dt');
	  elem.className = className;
	  elem.innerHTML = '<span>Kontroly aktualizací</span>';
	  ref.append(elem);
	  elem = document.createElement('dd');
	  elem.className = className;
	  elem.innerHTML = '<span>' + statData.update_checks + '</span>';
	  if (_lastStats.update_checks != undefined && statData.update_checks != _lastStats.update_checks) {
		elem.firstElementChild.innerHTML +=
		  '<span> (<b>+'.concat((statData.update_checks - (_lastStats.update_checks || 0)), '</b>)</span>');
		elem.style.backgroundColor = 'greenyellow';
	  }
	  ref.append(elem);
	}
	return lastStats.set(id, {
	  installs: statData.installs /*installs*/,
	  update_checks: statData.update_checks,
	  rating: rating,
	});
  }));
  Promise.all(articles).then(results => { GM_setValue('stats', JSON.stringify(Array.from(lastStats.entries()))) });
  return;

  function queryStats(scriptId) {
	return new Promise(function(resolve, reject) {
	  var xhr = new XMLHttpRequest();
	  xhr.open('GET', '/scripts/'.concat(scriptId, '/stats.json'), true);
	  xhr.onload = function() {
		if (xhr.status != 200) return reject(defaultErrorHandler(xhr));
		const values = Array.from(Object.values(xhr.response));
		resolve({
		  installs: values.reduce((acc, item) => acc + parseInt(item.installs || 0), 0),
		  update_checks: values.reduce((acc, item) => acc + parseInt(item.update_checks || 0), 0),
		});
	  };
	  xhr.setRequestHeader('Accept', 'application/json');
	  xhr.responseType = 'json';
	  xhr.timeout = 20000;
	  xhr.send();
	})
  }
})();