-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroughify-svgs.user.js
67 lines (60 loc) · 2.4 KB
/
roughify-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
// ==UserScript==
// @name Roughify Svgs
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Roughify Svgs
// @author Josh Parker
// @source https://github.com/joshparkerj/silly-internet-tricks/blob/main/wikipedia/roughify-svgs.user.js
// @downloadURL https://gist.github.com/joshparkerj/b61746e7f95aa03ffb4ac303bc01aa52/raw/roughify-svgs.user.js
// @updateURL https://gist.github.com/joshparkerj/b61746e7f95aa03ffb4ac303bc01aa52/raw/roughify-svgs.meta.js
// @match https://en.wikipedia.org/wiki/*
// @require https://unpkg.com/roughjs@latest/bundled/rough.js
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
/* globals rough */
(function roughifySvgs() {
const bodyContent = document.querySelector('#bodyContent');
const button = document.createElement('button');
button.innerText = 'roughify svgs';
button.addEventListener('click', () => {
button.disabled = true;
const svgs = [...document.querySelectorAll('svg')];
const newSvgSection = document.createElement('div');
// newSvgSection.id = 'new-svgs';
svgs.forEach((svg) => {
const newSvg = document.createElement('svg');
['width', 'height', 'xmlns'].forEach((attr) => newSvg.setAttribute(attr, svg.getAttribute(attr)));
const rns = rough.svg(newSvg);
const paths = [...svg.querySelectorAll('path')];
paths.forEach((path) => {
// console.log(path.outerHTML);
const d = path.getAttribute('d');
const fill = path.getAttribute('fill');
const stroke = path.getAttribute('stroke');
const strokeWidth = path.getAttribute('stroke-width');
const styleOptions = {
fillStyle: 'solid',
strokeWidth: 0.5,
};
if (fill) styleOptions.fill = fill;
if (stroke) styleOptions.stroke = stroke;
if (strokeWidth) styleOptions.strokeWidth = strokeWidth / 2;
newSvg.appendChild(rns.path(d, styleOptions));
});
const lines = [...svg.querySelectorAll('line')];
lines.forEach((line) => {
const x1 = line.getAttribute('x1');
const x2 = line.getAttribute('x2');
const y1 = line.getAttribute('y1');
const y2 = line.getAttribute('y2');
newSvg.appendChild(rns.line(x1, y1, x2, y2));
});
const div = document.createElement('div');
div.appendChild(newSvg);
newSvgSection.appendChild(div);
});
bodyContent.appendChild(newSvgSection);
});
bodyContent.appendChild(button);
}());