-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (110 loc) · 5.67 KB
/
script.js
File metadata and controls
133 lines (110 loc) · 5.67 KB
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
document.addEventListener('DOMContentLoaded', function() {
const userText = document.getElementById('userText');
const generateBtn = document.getElementById('generateBtn');
const clearBtn = document.getElementById('clearBtn');
const qrContainer = document.getElementById('qrContainer');
const downloadWord = document.getElementById('downloadWord');
const downloadPdf = document.getElementById('downloadPdf');
const downloadButtons = document.getElementById('downloadButtons');
const errorMessage = document.getElementById('errorMessage');
let qrCode = null;
let currentText = '';
// Generate QR Code
generateBtn.addEventListener('click', function() {
currentText = userText.value.trim();
if (!currentText) {
showError('Please enter some text first');
return;
}
// Clear previous QR code
if (qrCode) {
qrCode.clear();
document.getElementById('qrcode').innerHTML = '';
}
// Create data URL for local use
const textBlob = new Blob([currentText], { type: 'text/plain' });
const textUrl = URL.createObjectURL(textBlob);
// Generate new QR code with the data URL
qrCode = new QRCode(document.getElementById('qrcode'), {
text: textUrl,
width: 200,
height: 200,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
qrContainer.style.display = 'block';
downloadButtons.style.display = 'flex';
hideError();
});
// Clear text
clearBtn.addEventListener('click', function() {
userText.value = '';
if (qrCode) {
qrCode.clear();
document.getElementById('qrcode').innerHTML = '';
}
qrContainer.style.display = 'none';
downloadButtons.style.display = 'none';
hideError();
});
// Download as Word
downloadWord.addEventListener('click', function() {
if (!currentText) return;
const blob = new Blob([currentText], { type: 'application/msword' });
saveAs(blob, 'document.doc');
});
// Download as PDF
downloadPdf.addEventListener('click', function() {
if (!currentText) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
// Split the text into lines that fit the PDF
const lines = doc.splitTextToSize(currentText, 180);
// Add text to PDF (10mm from left, 10mm from top)
doc.text(lines, 10, 10);
doc.save('document.pdf');
});
// Check if page was opened from a QR code scan
function checkForSharedText() {
if (window.location.hash.startsWith('#text=')) {
const sharedText = decodeURIComponent(window.location.hash.substring(6));
showSharedText(sharedText);
}
}
function showSharedText(text) {
document.querySelector('.container').innerHTML = `
<h1>Download Your Text</h1>
<div class="qr-container">
<p>Your text is ready to download:</p>
<div style="text-align: left; background: #f9f9f9; padding: 15px; margin: 20px 0; border-radius: 5px; white-space: pre-wrap;">
${text.replace(/</g, '<').replace(/>/g, '>')}
</div>
<div class="buttons">
<button class="word-btn" id="downloadWordMobile">Download as Word</button>
<button class="pdf-btn" id="downloadPdfMobile">Download as PDF</button>
</div>
</div>
`;
document.getElementById('downloadWordMobile').addEventListener('click', function() {
const blob = new Blob([text], { type: 'application/msword' });
saveAs(blob, 'document.doc');
});
document.getElementById('downloadPdfMobile').addEventListener('click', function() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
const lines = doc.splitTextToSize(text, 180);
doc.text(lines, 10, 10);
doc.save('document.pdf');
});
}
function showError(message) {
errorMessage.textContent = message;
errorMessage.style.display = 'block';
}
function hideError() {
errorMessage.style.display = 'none';
}
// Initialize
checkForSharedText();
});