-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_gcode.html
172 lines (160 loc) · 5.17 KB
/
view_gcode.html
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
http-equiv="Content-Security-Policy"
content="upgrade-insecure-requests"
/>
<title>View GCode</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background-color: #f0f0f0;
font-family: "Roboto", sans-serif;
}
h1 {
color: #333;
}
#gcode-container {
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
max-height: 400px; /* Set a maximum height */
overflow-y: auto; /* Enable vertical scrollbar if content exceeds max height */
width: 300px;
}
.button-container {
display: none;
}
.button {
padding: 10px;
margin-right: 10px;
cursor: pointer;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
}
.button-copied {
background-color: #28a745;
}
.tip-message {
display: none;
font-size: 0.8rem;
color: #666;
margin-top: 10px;
}
.tip-message a {
color: #007bff;
}
</style>
</head>
<body>
<h1>View GCode</h1>
<div class="button-container">
<button class="button" id="copyButton">Copy to Clipboard</button>
<button class="button" id="downloadButton">Download as TXT</button>
</div>
<div class="tip-message">
Tip: You can view the GCode on
<a href="https://ncviewer.com/" target="_blank">NCViewer</a> online or on
CIMCO.
</div>
<pre>
<div id="gcode-container">
<!-- The response will be displayed here -->
</div>
</pre>
<script>
// Function to fetch and display GCode
function fetchAndDisplayGCode() {
// Get the 'id' from the query parameter
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get("id");
// Check if 'id' is provided
if (!id) {
document.getElementById("gcode-container").textContent =
"Error: No ID provided.";
return;
}
// Make an API request with the 'id'
fetch(
`https://0befbed3-5ba4-4003-87dd-d94ec8d46fbf-00-8ewr9zrcrljl.pike.replit.dev/view_gcode?id=${id}`
)
.then((response) => {
if (!response.ok) {
if (response.status == 404) {
throw new Error("GCode not found with the given ID.");
} else if (response.status == 400) {
throw new Error("ID not specifed.");
} else {
throw new Error("Network response was not ok");
}
}
return response.text();
})
.then((data) => {
// Display the GCode response
document.getElementById("gcode-container").textContent = data;
// Show the button container and tip message
document.querySelector(".button-container").style.display = "flex";
document.querySelector(".tip-message").style.display = "block";
})
.catch((error) => {
console.error(
"There was a problem with the fetch operation:",
error
);
document.getElementById("gcode-container").textContent =
"Error: " + error.message;
});
}
// Call the function when the page loads
window.addEventListener("load", fetchAndDisplayGCode);
// Copy text to clipboard
document
.getElementById("copyButton")
.addEventListener("click", function () {
const gcodeText =
document.getElementById("gcode-container").textContent;
navigator.clipboard
.writeText(gcodeText)
.then(() => {
// Copy successful, change button text and color
const copyButton = document.getElementById("copyButton");
copyButton.textContent = "Copied!";
copyButton.classList.add("button-copied");
setTimeout(() => {
// Reset button text and color after 2 seconds
copyButton.textContent = "Copy to Clipboard";
copyButton.classList.remove("button-copied");
}, 2000);
})
.catch((error) => {
console.error("Copy to clipboard failed:", error);
});
});
// Download text as a text file
document
.getElementById("downloadButton")
.addEventListener("click", function () {
const gcodeText =
document.getElementById("gcode-container").textContent;
const blob = new Blob([gcodeText], { type: "text/plain" });
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "gcode.txt";
a.click();
window.URL.revokeObjectURL(url);
});
</script>
</body>
</html>