-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhscript.js
105 lines (85 loc) · 2.6 KB
/
hscript.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
const fs = require("fs");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const http = require('http');
const filePath = "index.html";
let pageContent = `<!DOCTYPE html>
<html lang="en" id="html">
<head id="head">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title id="title">I Love Hscript!</title>
</head>
<body id="body">
</body>
</html>`
let index;
const presets = [{
name: "textCenter",
styleProp: "text-align: center;"
},
{
name: "documentCenter",
styleProp: "position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);"
},
]
const colors = [{
name: "red",
styleProp: "color: red;"
},
{
name: "green",
styleProp: "color: green;"
},
{
name: "blue",
styleProp: "color: blue;"
},
{
name: "yellow",
styleProp: "color: yellow;"
},
]
const hscript = {
title: function(params) {
const findpreset = presets.find(el => el.name == params.preset)
const findcolor = colors.find(el => el.name == params.color)
if (findpreset !== undefined) {
style = findpreset.styleProp + findcolor.styleProp + params.style
return `<h1 style="${style}">${params.content}</h1>`
}
},
button: function(params) {
const findpreset = presets.find(el => el.name == params.preset)
const findcolor = colors.find(el => el.name == params.color)
if (findpreset !== undefined) {
style = findpreset.styleProp + findcolor.styleProp + params.style
return `<button style="${style}">${params.content}</button>`
}
},
script: function(script) {
if (script !== undefined) {
return `<script>${script}</script>`
} else {
console.error("No script provided")
}
},
servePage: function(port) {
if (fs.existsSync(filePath)) {
index = fs.readFileSync(filePath, 'utf8')
} else {
fs.writeFileSync(filePath, pageContent);
index = fs.readFileSync(filePath, 'utf8')
}
http.createServer(function(req, res) {
res.write(index);
res.end();
}).listen(port);
},
addBody: function(content) {
const dom = new JSDOM(pageContent);
fs.writeFileSync(filePath, pageContent += content)
}
}
module.exports = hscript