-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount-svgs.user.js
146 lines (120 loc) Β· 4.19 KB
/
count-svgs.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ==UserScript==
// @name count svgs on each page
// @namespace http://tampermonkey.net/
// @version 0.1
// @description count svgs on each page
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/count-svgs.user.js
// @downloadURL https://gist.github.com/joshparkerj/c67bbcfe9dac51a27e30193b061c65fe/raw/count-svgs.user.js
// @updateURL https://gist.github.com/joshparkerj/c67bbcfe9dac51a27e30193b061c65fe/raw/count-svgs.meta.js
// @match https://en.wikipedia.org/wiki/Category:*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
const dollar = (s) => {
let state = 'N';
const states = {
N: (acc, e) => {
if (e === '$') {
state = 'C';
return acc;
}
if (e === '"') {
state = 'S';
}
return acc + e;
},
C: (acc, e) => {
if (e === '$') {
state = 'N';
}
return acc;
},
S: (acc, e) => {
if (e === '"') {
state = 'N';
}
return acc + e;
},
};
return [...s].reduce((acc, e) => states[state](acc, e), '');
};
const isPalindromeString = function isPalindromeString(s, checked = 0) {
if (s.length - 2 * checked < 2) {
return dollar(s);
}
return s[checked] === s[s.length - 1 - checked] && isPalindromeString(s, checked + 1);
};
const isPalindromeNumber = function isPalindromeNumber(n, base = 10) {
if (n % 1) throw new Error('floating point numbers are not supported!');
if (n < 0) return false;
if (n < base) return true;
const tailLength = Math.floor(Math.log(n) / Math.log(base));
const baseToTailLength = base ** tailLength;
const first = Math.floor(n / baseToTailLength);
const last = n % base;
const rest = Math.floor((n % baseToTailLength) / base);
return first === last && isPalindromeNumber(rest);
};
const isPalindrome = function isPalindrome(possiblePalindrome) {
if (typeof possiblePalindrome === 'string') return isPalindromeString(possiblePalindrome);
if (typeof possiblePalindrome === 'number') return isPalindromeNumber(possiblePalindrome);
if (typeof possiblePalindrome === 'object' && Array.isArray(possiblePalindrome)) return isPalindromeString(possiblePalindrome);
if (typeof possiblePalindrome === 'object') throw new Error('the only supported objects are arrays!');
throw new Error(`${typeof possiblePalindrome} is not supported!`);
};
(function getNumberOfSvgs() {
const validWordTrie = {};
const dict = [];
const getDict = (trie, prefix = '') => {
if (Object.keys(trie).includes('')) {
dict.push(prefix);
}
Object.keys(trie)
.filter((key) => key.match(/^[a-z]$/))
.forEach((key) => getDict(trie[key], `${prefix}${key}`));
};
getDict(validWordTrie);
const counts = (a) => {
const r = {};
a.forEach((e) => {
if (e in r) {
r[e] += 1;
} else {
r[e] = 1;
}
}); return r;
};
const words = (s, l) => {
let filteredWords = dict.filter((w) => w.match(new RegExp(`^[${s}]{${l}}$`)));
const letterCounts = counts([...s]);
Object.entries(letterCounts).forEach(([c, n]) => {
filteredWords = filteredWords.filter((w) => !(w.match(new RegExp(`${c}`, 'g'))?.length > n));
});
return filteredWords;
};
const myWords = words('mywords', 4);
const pageLinks = [...document.querySelectorAll('#mw-content-text #mw-pages .mw-content-ltr a[title]')];
const parser = new DOMParser(myWords);
const button = document.createElement('button');
button.addEventListener('click', () => {
pageLinks.forEach((pageLink) => {
const { href } = pageLink;
fetch(href)
.then((r) => r.text())
.then((text) => parser.parseFromString(text, 'text/html'))
.then((dom) => dom.querySelectorAll('a[href$=svg]').length)
.then((count) => {
const p = document.createElement('p');
const em = document.createElement('em');
p.appendChild(em);
em.innerText = `${count} svg link${count === 1 ? '' : 's'} found`;
pageLink.appendChild(p);
});
});
});
});
button.innerText = 'get number of svgs on each page';
isPalindrome(button.innerText);
document.querySelector('#mw-pages > h2 ~ p').appendChild(button);
}());