-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathpaste-rich-text.html
182 lines (156 loc) · 4.25 KB
/
paste-rich-text.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
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rich Text HTML Extractor</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1, h2 {
font-weight: 500;
}
#paste-area {
width: 100%;
height: 150px;
margin-bottom: 15px;
border: 1px solid #ccc;
padding: 10px;
background-color: #f5f5f5;
font-size: 16px;
font-family: Helvetica, Arial, sans-serif;
overflow: auto;
}
#output-container {
display: none;
}
#html-output {
width: 100%;
height: 250px;
margin-bottom: 15px;
border: 1px solid #ccc;
padding: 10px;
background-color: #fff;
font-size: 16px;
font-family: monospace;
white-space: pre-wrap;
overflow-x: auto;
}
#preview-container {
margin-top: 20px;
border: 1px solid #ddd;
padding: 15px;
background-color: #fff;
}
.button {
padding: 8px 15px;
cursor: pointer;
background-color: #4285f4;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
margin-right: 10px;
transition: background-color 0.2s;
}
.button:hover {
background-color: #3367d6;
}
.info-text {
color: #666;
font-style: italic;
margin-bottom: 10px;
}
@media (max-width: 600px) {
body {
padding: 10px;
}
#paste-area, #html-output {
height: 180px;
}
}
</style>
</head>
<body>
<h1>Rich text HTML extractor</h1>
<p>Copy formatted text from a webpage and paste here to extract the HTML:</p>
<div id="paste-area" contenteditable="true"></div>
<p class="info-text">Paste using Ctrl+V or ⌘+V to capture the rich text HTML.</p>
<div id="output-container">
<h2>HTML code</h2>
<textarea id="html-output" readonly></textarea>
<button id="copy-button" class="button">Copy HTML</button>
<button id="clear-button" class="button">Clear all</button>
<div id="preview-container">
<h2>Preview</h2>
<div id="preview-content"></div>
</div>
</div>
<script type="module">
// Function to escape HTML entities for display
const escapeHtml = (html) => {
return html
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
// Get DOM elements
const pasteArea = document.getElementById('paste-area');
const outputContainer = document.getElementById('output-container');
const htmlOutput = document.getElementById('html-output');
const previewContent = document.getElementById('preview-content');
const copyButton = document.getElementById('copy-button');
const clearButton = document.getElementById('clear-button');
// Handle paste event to capture HTML
pasteArea.addEventListener('paste', function(e) {
// Let the paste happen normally first
setTimeout(() => {
// Get current HTML content after the paste
const pastedContent = pasteArea.innerHTML;
if (pastedContent) {
// Display the HTML code with escaped characters
htmlOutput.value = pastedContent;
// Show the output container
outputContainer.style.display = 'block';
// Show preview of how the HTML renders
previewContent.innerHTML = pastedContent;
// Let user know paste was successful
pasteArea.innerHTML = '<p style="color: green;">Content pasted. HTML extracted.</p>';
}
}, 0);
});
// Reset paste area when clicked
pasteArea.addEventListener('focus', function() {
if (pasteArea.textContent.includes('Content pasted. HTML extracted.')) {
pasteArea.innerHTML = '';
}
});
// Copy HTML to clipboard
copyButton.addEventListener('click', function() {
htmlOutput.select();
document.execCommand('copy');
const originalText = copyButton.textContent;
copyButton.textContent = 'Copied!';
setTimeout(() => {
copyButton.textContent = originalText;
}, 1500);
});
// Clear all content
clearButton.addEventListener('click', function() {
pasteArea.innerHTML = '';
htmlOutput.value = '';
previewContent.innerHTML = '';
outputContainer.style.display = 'none';
});
</script>
</body>
</html>