Summary Files Commits Branches @commit @diff @stats
path: root / vim-fmi-strikeout-cheaters.js
blob: 40dbd11f279b0452fe4619b3b0c937d2c3c58000 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ==UserScript==
// @name        vim-fmi зачеркване на чиитъри
// @description Всички чиитъри се зачеркват в таблицата. Чиитър е всеки който ползва системния clipboard или вмъква съдържанието от файл (read команда).
//
// @version     1.0
// @license     MIT
// @author      Syndamia
//
// @source      https://gitlab.com/Syndamia/userscripts/-/blob/main/vim-fmi-strikeout-cheaters.js
// @supportURL  https://gitlab.com/Syndamia/userscripts/-/issues
// @homepage    https://gitlab.com/Syndamia/userscripts/
// @icon        https://vim-fmi.bg/favicon.png
//
// @namespace   Syndamia
// @match *://*vim-fmi.bg/tasks/*
// @run-at document-end
// ==/UserScript==

const inAllSol = window.location.pathname.endsWith('solutions');

/* Helper functions */
function getScore(elem) {
    return parseInt(elem.children[2 + inAllSol].innerText);
}
function getSolution(elem) {
    return elem.children[1 + inAllSol].innerText;
}
function getDate(elem) {
    return elem.children[3 + inAllSol].innerText;
}

/* Get data */
const tableBody = document.getElementsByClassName('solutions')[0].children[1];

  // Since you can sort the (/solutions page) table in different ways,
  // we'll need to preserve the original indecies of elements
var rows = [];
for (let i = 0; i < tableBody.children.length; i++) {
    rows.push([tableBody.children[i], i]);
}

/* Sort by score and then by date */
rows.sort(function (a, b) {
    a = a[0]; b = b[0];
    var aInt = getScore(a), bInt = getScore(b);
    return aInt > bInt || (aInt == bInt && getDate(a) > getDate(b));
});

/* Every following solution that is the same as the current one, or
 * if a solution pastes content from the system clipboard, or
 * if a solution uses the read command,
 * it is deemed to be "cheated", and a special style is applied to it */
function usesClipboard(rowElem) {
    return getSolution(rowElem[0]).match(/("[+*](|[gz\[\]])(p|P|<MiddleMouse>)|:pu[ t]|:r( |ead))/);
}
function applyCheatedStyle(rowElem) {
    rowElem[0].style.textDecoration = 'line-through';
    rowElem[0].style.fontStyle = 'italic';
    rowElem[0].style.opacity = '0.5';
    return rowElem;
}

if (usesClipboard(rows[0])) {
    rows[0] = applyCheatedStyle(rows[0]);
}

for (let i = rows.length - 1; i > 0; i--) {
    if ((getScore(rows[i][0]) == getScore(rows[i-1][0]) && getSolution(rows[i][0]) == getSolution(rows[i-1][0]))
        || usesClipboard(rows[i]))
    {
        rows[i] = applyCheatedStyle(rows[i]);
    }
}

/* Return the original sorting of data */
if (inAllSol) {
    rows.sort(function (a, b) {
        return a[1] > b[1];
    });
}

tableBody.replaceChildren(...rows.map(r => r[0]));