-
Notifications
You must be signed in to change notification settings - Fork 4
/
contentscript.js
153 lines (136 loc) · 12.5 KB
/
contentscript.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
147
148
149
150
151
152
153
chrome.extension.onMessage.addListener(
function(request, sender, sendMessage) {
console.log(request)
let isExportPNG = request.command === 'export3' || request.command === 'export4'
// blocks-media as base64 for svg inline image
let blocksMedia = new Map()
blocksMedia.set("repeat.svg","data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDIxLjAuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9InJlcGVhdCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiCgkgdmlld0JveD0iMCAwIDI0IDI0IiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCAyNCAyNDsiIHhtbDpzcGFjZT0icHJlc2VydmUiPgo8c3R5bGUgdHlwZT0idGV4dC9jc3MiPgoJLnN0MHtmaWxsOiNDRjhCMTc7fQoJLnN0MXtmaWxsOiNGRkZGRkY7fQo8L3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LDkuMzEtMC41Niw5Ljg3LDBhMS40NCwxLjQ0LDAsMCwxLDAsMkw3LjM3LDcuMzdBMS40MywxLjQzLDAsMCwxLDYuMzYsNy43OVoiIGZpbGw9IiNmZmYiLz48L3N2Zz4=")
let svg = document.createElement('svg')
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
svg.setAttribute('xmlns:html', 'http://www.w3.org/1999/xhtml')
svg.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink')
svg.setAttribute('version', '1.1')
let style = document.createElement('style')
style.innerHTML =
`
.blocklyText {
fill: #fff;
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 12pt;
font-weight: 500;
}
.blocklyNonEditableText>text, .blocklyEditableText>text {
fill: #575E75;
}
.blocklyDropdownText {
fill: #fff !important;
}
`
if (request.command === 'export1' || request.command === 'export3') {
svg = selectedBlocks(svg, style, isExportPNG)
} else {
svg = allBlocks(svg, style, isExportPNG)
}
// 处理 nbsp 空格
let texts = Array.from(svg.getElementsByTagName('text'))
texts.forEach(text => {
text.innerHTML = text.innerHTML.replace(/ /g, ' ')
})
// 处理image 路径
let images = Array.from(svg.getElementsByTagName('image'))
let scratchURL = window.location.origin
images.forEach(item => {
let builtinSvgData = blocksMedia.get(item.getAttribute('xlink:href').substring(item.getAttribute('xlink:href').lastIndexOf('/')+1))
if (builtinSvgData) { // 替换插件预置的svg数据(官方)
item.setAttribute('xlink:href', builtinSvgData)
} else if (item.getAttribute('xlink:href').indexOf('/static/') === 0) { // 替换为第三方 链接形式
item.setAttribute('xlink:href', scratchURL + item.getAttribute('xlink:href').slice(0))
} else if (item.getAttribute('xlink:href').indexOf('./static/') === 0) {
item.setAttribute('xlink:href', scratchURL + item.getAttribute('xlink:href').slice(1))
} else if (item.getAttribute('xlink:href').indexOf('static/') === 0) {
item.setAttribute('xlink:href', scratchURL + '/' + item.getAttribute('xlink:href'))
}
})
let tmp = document.createElement('div')
tmp.appendChild(svg);
if (request.command === 'export1' || request.command === 'export2') {
exportData(tmp.innerHTML)
} else {
exportPNG(svg);
}
sendMessage({command: 'ok'})
});
function selectedBlocks (svg, style, isExportPNG) {
let svgchild = document.querySelector('svg.blocklySvg g.blocklySelected')
if (!svgchild) alert('Click on the blocks you want to export!')
svgchild = svgchild.cloneNode(true)
let dataShapes = svgchild.getAttribute('data-shapes')
svgchild.setAttribute('transform', `translate(0,${dataShapes === 'hat' ? '18' : '0'}) ${isExportPNG ? 'scale(2)' : ''}`)
svg.append(style)
svg.append(svgchild)
return svg
}
function allBlocks(svg, style, isExportPNG) {
let svgchild = document.querySelector('svg.blocklySvg g.blocklyBlockCanvas')
svgchild = svgchild.cloneNode(true)
let xArr = []
let yArr = []
svgchild.childNodes.forEach(g => {
let x = g.getAttribute('transform').match(/translate\((.*?),(.*?)\)/)[1] || 0
let y = g.getAttribute('transform').match(/translate\((.*?),(.*?)\)/)[2] || 0
xArr.push(x * (isExportPNG ? 2 : 1))
yArr.push(y * (isExportPNG ? 2 : 1))
})
svgchild.setAttribute('transform', `translate(${-Math.min(...xArr)},${-Math.min(...yArr) + (18 * (isExportPNG ? 2 : 1))}) ${isExportPNG ? 'scale(2)' : ''}`)
svg.append(style)
svg.append(svgchild)
return svg
}
function exportData(text) {
const saveLink = document.createElement('a')
document.body.appendChild(saveLink)
const data = new Blob([text], { type: 'text' })
const url = window.URL.createObjectURL(data)
saveLink.href = url
// File name: project-DATE-TIME
const date = new Date()
const timestamp = `${date.toLocaleDateString()}-${date.toLocaleTimeString()}`
saveLink.download = `block_${timestamp}.svg`
saveLink.click()
window.URL.revokeObjectURL(url)
document.body.removeChild(saveLink)
}
function exportPNG(svg) {
const div = document.createElement("div");
div.appendChild(svg);
const iframe = document.createElement("iframe");
// iframe.style.display = "none"
document.body.append(iframe);
iframe.contentDocument.write(div.innerHTML);
let { width, height } = iframe.contentDocument.body.querySelector("svg g").getBoundingClientRect();
height = height + 20 * 2; // hat block height restore
svg.setAttribute("width", width + "px");
svg.setAttribute("height", height + "px");
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
let img = document.createElement("img");
img.setAttribute("src", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(div.innerHTML))));
img.onload = function () {
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0, img.width, img.height);
// Now is done
let dataURL = canvas.toDataURL("image/png");
let link = document.createElement("a");
const date = new Date();
const timestamp = `${date.toLocaleDateString()}-${date.toLocaleTimeString()}`;
link.download = `block_${timestamp}.png`;
link.href = dataURL;
link.click();
iframe.remove();
};
}