-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow-svgs.user.js
49 lines (46 loc) · 1.79 KB
/
show-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
// ==UserScript==
// @name show svgs
// @namespace http://tampermonkey.net/
// @version 0.1
// @description show svgs
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/show-svgs.user.js
// @downloadURL https://gist.github.com/joshparkerj/387e6798a4b5cb2e64e21bb2daf7d6fc/raw/show-svgs.user.js
// @updateURL https://gist.github.com/joshparkerj/387e6798a4b5cb2e64e21bb2daf7d6fc/raw/show-svgs.meta.js
// @match https://en.wikipedia.org/wiki/*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
(function showSvgs() {
const bodyContent = document.querySelector('#bodyContent');
const button = document.createElement('button');
button.innerText = 'show svgs';
button.addEventListener('click', () => {
button.disabled = true;
const svgUrls = [...document.querySelectorAll('a[href$=svg]')].map((a) => a.href);
const svgSection = document.createElement('section');
svgSection.id = 'svgs';
const parser = new DOMParser();
svgUrls.forEach((url) => {
const urlMatch = url.match(/https:\/\/en\.wikipedia\.org\/wiki\/File:(?<fileName>.*\.svg)/);
if (urlMatch) {
fetch(url)
.then((r) => r.text())
.then((text) => parser.parseFromString(text, 'text/html'))
.then((dom) => dom.querySelector('.fullImageLink a[href$=svg]').href)
.then((href) => {
fetch(href)
.then((r) => r.text())
.then((text) => {
const div = document.createElement('div');
div.innerHTML = text;
svgSection.appendChild(div);
});
});
}
});
// document.styleSheets[0].insertRule('#svgs div, #svgs svg { width: 100px; }');
bodyContent.appendChild(svgSection);
});
bodyContent.appendChild(button);
}());