This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsave-no-style.js
109 lines (89 loc) · 2.98 KB
/
save-no-style.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
/*
Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
~function() {
function descend(node, out) {
if (node.nodeType == Node.ELEMENT_NODE) {
if (node.nodeName == 'STYLE' || node.nodeName == 'SCRIPT')
return;
out.push(node);
for (var i = 0; i < node.attributes.length; ++i) {
if (node.attributes[i].name == 'style')
continue;
out.attribute(node.attributes[i]);
}
var style = getComputedStyle(node);
out.attribute({name: 'style', value: style.cssText});
var child = node.firstChild;
while (child) {
descend(child, out);
child = child.nextSibling;
}
out.pop();
} else if (node.nodeType == Node.COMMENT_NODE) {
out.comment(node.textContent);
} else if (node.nodeType == Node.TEXT_NODE) {
out.text(node.textContent);
}
}
function capturePseudoElementStyles(out) {
var pseudoStyles = '';
for (var i = 0; i < document.styleSheets.length; i++) {
var styleSheet = document.styleSheets[i];
for (var j = 0; j < styleSheet.rules.length; j++) {
var rule = styleSheet.rules[j];
if (rule.selectorText && rule.selectorText.indexOf('::') !== -1) {
pseudoStyles += rule.cssText;
}
}
}
if (pseudoStyles.length > 0) {
out.push({nodeName: 'STYLE'});
out.text(pseudoStyles);
out.pop();
}
}
function Recorder() {
this.log = [];
}
Recorder.prototype.base = function(url) {
this.log.push({ 't': 'b', 'v': url });
}
Recorder.prototype.attribute = function(attribute) {
this.log.push({ 't': 'a', 'n': attribute.name, 'v': attribute.value });
}
Recorder.prototype.push = function(node) {
this.log.push({ 't': 'n', 'n': node.nodeName });
}
Recorder.prototype.pop = function() {
this.log.push({ 't': '/' });
}
Recorder.prototype.text = function(text) {
this.log.push({ 't': 't', 'v': text })
}
Recorder.prototype.comment = function(comment) {
this.log.push({ 't': 'c', 'v': comment })
}
Recorder.prototype.save = function() {
var blob = new Blob([JSON.stringify(this.log)], { type: 'application/octet-binary' });
var blobURL = window.webkitURL.createObjectURL(blob);
var link = document.createElement('a');
link.href = blobURL;
link.download = encodeURIComponent(window.location).replace(/\./g, '-').replace(/\%[0-9A-F]{2}/g, '-') + '.ds.json';
link.click();
}
var recorder = new Recorder();
recorder.base(String(window.location));
capturePseudoElementStyles(recorder);
descend(document.documentElement, recorder);
recorder.save();
}();