This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
content.js
105 lines (96 loc) · 2.98 KB
/
content.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
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
let removeColor = (node) => {
if (node.hasAttribute && node.hasAttribute('fill')) {
node.setAttribute('fill','currentColor');
}
if (node.hasChildNodes()) {
node.childNodes.forEach(child => removeColor(child));
}
return node;
}
let removeColorIfNecessary = (node) => {
let colors = getColors(node);
if (colors.length == 1){
console.log("REMOVING COLOR",colors[0]);
removeColor(node);
} else {
console.log("FOUND COLORS",colors);
}
return node;
}
let getColors = (node) => {
let colors = [];
if (node.hasAttribute && node.hasAttribute('fill')) {
let color = node.getAttribute('fill').toLowerCase();
if (color != 'none' && color !='currentcolor'){
colors.push(color);
}
}
if (node.hasChildNodes()) {
node.childNodes.forEach((child) => {
let childColors = getColors(child);
childColors.forEach((color)=>{
if (colors.indexOf(color)==-1){
colors.push(color);
}
})
});
}
return colors;
}
let removeDimensions = (node) => {
if (node.hasAttribute && node.hasAttribute('height')) {
node.removeAttribute('height');
}
if (node.hasAttribute && node.hasAttribute('width')) {
node.removeAttribute('width');
}
return node;
}
if (request.message === 'clicked_browser_action') {
let svgInlineCodes = Array.from(
document.querySelectorAll('svg'), e => {
let node = e.cloneNode(true);
removeColorIfNecessary(node);
removeDimensions(node);
return node;
}
);
// External SVG
let svgFiles = Array.from(document.querySelectorAll('img[src*=".svg"]'));
console.log(svgFiles);
let parser = new DOMParser();
let svgFilePromises = svgFiles.map((file) => {
return new Promise((resolve, reject) => {
let ajax = new XMLHttpRequest();
ajax.open('GET', file.src, true);
ajax.send();
ajax.onload = function (e) {
let node = parser.parseFromString(ajax.responseText, 'image/svg+xml').children[0];
resolve(node);
}
})
});
Promise.all(svgFilePromises).then((svgFileCodes) => {
let svgCodes = svgInlineCodes.concat(svgFileCodes);
let serializer = new XMLSerializer();
svgCodes = svgCodes.map((node) => {
removeColorIfNecessary(node);
return serializer.serializeToString(removeDimensions(node));
})
console.log(svgCodes);
// Removes spaces
svgCodes = svgCodes.map(item => {
return item.replace(/>\s+</g, '><');
});
// Removes duplicated svg's
svgCodesFinal = svgCodes.filter(function(item, pos) {
return svgCodes.indexOf(item) == pos;
});
chrome.runtime.sendMessage({
message: { type: 'open_new_tab', data: svgCodesFinal },
url: 'getsvgs.html'
});
});
}
});