- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
Latest commit
133 lines (110 loc) · 5.67 KB
/
Copy pathscript.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(){
constuserText=document.getElementById('userText');
constgenerateBtn=document.getElementById('generateBtn');
constclearBtn=document.getElementById('clearBtn');
constqrContainer=document.getElementById('qrContainer');
constdownloadWord=document.getElementById('downloadWord');
constdownloadPdf=document.getElementById('downloadPdf');
constdownloadButtons=document.getElementById('downloadButtons');
consterrorMessage=document.getElementById('errorMessage');
letqrCode=null;
letcurrentText='';
// 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
consttextBlob=newBlob([currentText],{type: 'text/plain'});
consttextUrl=URL.createObjectURL(textBlob);
// Generate new QR code with the data URL
qrCode=newQRCode(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;
constblob=newBlob([currentText],{type: 'application/msword'});
saveAs(blob,'document.doc');
});
// Download as PDF
downloadPdf.addEventListener('click',function(){
if(!currentText)return;
const{ jsPDF }=window.jspdf;
constdoc=newjsPDF();
// Split the text into lines that fit the PDF
constlines=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
functioncheckForSharedText(){
if(window.location.hash.startsWith('#text=')){
constsharedText=decodeURIComponent(window.location.hash.substring(6));
showSharedText(sharedText);
}
}
functionshowSharedText(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(){
constblob=newBlob([text],{type: 'application/msword'});
saveAs(blob,'document.doc');
});
document.getElementById('downloadPdfMobile').addEventListener('click',function(){
const{ jsPDF }=window.jspdf;
constdoc=newjsPDF();
constlines=doc.splitTextToSize(text,180);
doc.text(lines,10,10);
doc.save('document.pdf');
});
}
functionshowError(message){
errorMessage.textContent=message;
errorMessage.style.display='block';
}
functionhideError(){
errorMessage.style.display='none';
}
// Initialize
checkForSharedText();
});