-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcryptokitty-rare-highlighter.user.js
104 lines (97 loc) · 3.18 KB
/
cryptokitty-rare-highlighter.user.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// ==UserScript==
// @name Cryptokitty rare highlighter
// @version 2.0.0
// @description A userscript that makes rare cryptokitty cattributes more noticeable
// @license MIT
// @author Rob Garrison
// @namespace https://github.com/Mottie
// @include https://www.cryptokitties.co/*
// @run-at document-idle
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @connect cryptokittydex.com
// @icon https://www.cryptokitties.co/icons/apple-icon-120x120.png
// @updateURL https://raw.githubusercontent.com/Mottie/Misc-userscripts/master/cryptokitty-rare-highlighter.user.js
// @downloadURL https://raw.githubusercontent.com/Mottie/Misc-userscripts/master/cryptokitty-rare-highlighter.user.js
// ==/UserScript==
(() => {
"use strict";
let data = GM_getValue("cryptoKittyData", {
updated: 0,
list: {}
});
const checkDelay = 8.64e7; // 24 hours in milliseconds
const maxIterations = 1000;
const rarity = {
"gold": number => number <= 10,
"purple": number => number <= 100,
"orange": number => number <= 500,
"blue": number => number <= 1000,
"green": number => number <= 10000,
"black": number => number <= 100000,
"silver": number => number > 100000
};
function buildList() {
GM_xmlhttpRequest({
method: "GET",
url: "https://cryptokittydex.com/",
onload: response => extractList(response.responseText)
});
}
function extractList(page = "") {
let pos1 = 0,
pos2 = 0,
counter = 0,
name = "";
const linkPattern = "<a class=\"badge badge-pill badge-cattribute";
const numberPattern = "<span class=\"badge badge-pill badge-secondary\">";
// Add counter to prevent infinite loops
while (
counter < maxIterations &&
(pos1 = page.indexOf(linkPattern, pos2)) > -1
) {
pos2 = page.indexOf(">", pos1);
pos1 = page.indexOf(numberPattern, pos2);
name = page.substring(pos2 + 1, pos1).trim();
pos1 += numberPattern.length;
pos2 = page.indexOf("</span>", pos1);
data.list[name] = parseInt(page.substring(pos1, pos2).replace(/,/g, ""), 10);
counter++;
}
data.updated = new Date().getTime();
GM_setValue("cryptoKittyData", data);
}
const getRarity = number => Object.keys(rarity).find(r => rarity[r](number));
const span = document.createElement("small");
span.className = "Cattribute-type";
function init() {
if (document.querySelector(".Cattribute-title")) {
if (new Date().getTime() > data.updated + checkDelay) {
buildList();
}
const date = new Date(data.updated)
.toISOString()
.substring(0, 19)
.replace("T", " ");
document.querySelectorAll(".Cattribute-title").forEach(el => {
const number = data.list[el.textContent.trim()] || -1;
if (number > -1 && !el.classList.contains("ckrh-processed")) {
el.classList.add("ckrh-processed");
el.style.color = getRarity(number);
span.textContent = number;
span.title = `Last Updated from CryptoKittydex on ${date} UTC`;
el.insertAdjacentElement("afterend", span.cloneNode(true));
}
});
}
}
let timer;
new MutationObserver(() => {
clearTimeout(timer);
timer = setTimeout(() => init(), 500);
}).observe(document.body, {
childList: true,
subtree: true
});
})();