Repository navigation
Expand file tree
/
Copy pathnote_propmt.js
More file actions
291 lines (289 loc) · 9.09 KB
/
Copy pathnote_propmt.js
File metadata and controls
291 lines (289 loc) · 9.09 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
(async () => {
const selectedText = (await chrome.storage.local.get("tempNote")).tempNote || "";
const notesData = await chrome.storage.local.get("notes");
const notes = notesData.notes || {};
function formatTextWithHeadings(text) {
const lines = text.split('\n');
const formattedLines = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) {
formattedLines.push('');
continue;
}
const isShort = line.length < 50;
const endsWithPunctuation = /[.!?]$/.test(line);
const isAllCaps = line === line.toUpperCase() && line.length > 2;
const isTitleCase = /^[A-Z][a-z]+/.test(line) && line.length < 30;
const isFollowedByContent = i < lines.length - 1 && lines[i + 1].trim().length > 20;
const isAtBeginning = i === 0;
const isHeading = (isShort && !endsWithPunctuation && (isAllCaps || isTitleCase)) ||
(isShort && isFollowedByContent) ||
(isAtBeginning && isShort && !endsWithPunctuation);
if (isHeading) {
formattedLines.push(`**${line}**`);
} else {
formattedLines.push(line);
}
}
return formattedLines.join('\n');
}
const formattedText = formatTextWithHeadings(selectedText);
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 10000;
font-family: 'Inter', 'Segoe UI', Arial, sans-serif;
`;
const modalContent = document.createElement('div');
modalContent.style.cssText = `
background: white;
border-radius: 12px;
padding: 24px;
max-width: 400px;
width: 90%;
max-height: 80vh;
overflow-y: auto;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
`;
const header = document.createElement('h2');
header.textContent = 'Add to Note';
header.style.cssText = `
margin: 0 0 16px 0;
color: #333;
font-size: 20px;
font-weight: 600;
`;
const textPreview = document.createElement('div');
textPreview.style.cssText = `
background: #f5f5f5;
border-radius: 8px;
padding: 12px;
margin-bottom: 16px;
font-size: 14px;
color: #666;
max-height: 150px;
overflow-y: auto;
border-left: 4px solid #0078d7;
line-height: 1.5;
`;
function renderFormattedPreview(text) {
const lines = text.split('\n');
const previewLines = lines.slice(0, 8);
const previewText = previewLines.join('\n');
const formattedPreview = previewText.replace(/\*\*(.*?)\*\*/g, '<strong style="font-weight: 700; color: #333; font-size: 16px;">$1</strong>');
textPreview.innerHTML = formattedPreview;
if (lines.length > 8) {
const moreIndicator = document.createElement('div');
moreIndicator.textContent = `... and ${lines.length - 8} more lines`;
moreIndicator.style.cssText = `
color: #999;
font-style: italic;
margin-top: 8px;
font-size: 12px;
`;
textPreview.appendChild(moreIndicator);
}
}
renderFormattedPreview(formattedText);
const existingNotesSection = document.createElement('div');
existingNotesSection.style.cssText = `
margin-bottom: 20px;
`;
const existingNotesTitle = document.createElement('h3');
existingNotesTitle.textContent = 'Add to existing note:';
existingNotesTitle.style.cssText = `
margin: 0 0 12px 0;
font-size: 16px;
color: #555;
font-weight: 500;
`;
const existingNotesList = document.createElement('div');
existingNotesList.style.cssText = `
display: flex;
flex-direction: column;
gap: 8px;
`;
const noteNames = Object.keys(notes);
if (noteNames.length > 0) {
noteNames.forEach(noteName => {
const noteButton = document.createElement('button');
noteButton.textContent = noteName;
noteButton.style.cssText = `
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 8px;
padding: 12px 16px;
text-align: left;
cursor: pointer;
font-size: 14px;
color: #333;
transition: all 0.2s;
font-weight: 500;
`;
noteButton.addEventListener('mouseenter', () => {
noteButton.style.background = '#e9ecef';
noteButton.style.borderColor = '#0078d7';
});
noteButton.addEventListener('mouseleave', () => {
noteButton.style.background = '#f8f9fa';
noteButton.style.borderColor = '#e9ecef';
});
noteButton.addEventListener('click', async () => {
const updatedNote = (notes[noteName] || "") + "\n\n" + formattedText;
notes[noteName] = updatedNote;
await chrome.storage.local.set({ notes });
const successMsg = document.createElement('div');
successMsg.textContent = `Added to "${noteName}"!`;
successMsg.style.cssText = `
color: #28a745;
font-weight: 500;
text-align: center;
margin-top: 8px;
`;
modalContent.appendChild(successMsg);
setTimeout(() => {
document.body.removeChild(modal);
}, 1500);
});
existingNotesList.appendChild(noteButton);
});
} else {
const noNotesMsg = document.createElement('p');
noNotesMsg.textContent = 'No existing notes found.';
noNotesMsg.style.cssText = `
color: #666;
font-style: italic;
margin: 0;
`;
existingNotesList.appendChild(noNotesMsg);
}
const newNoteSection = document.createElement('div');
newNoteSection.style.cssText = `
margin-bottom: 20px;
`;
const newNoteTitle = document.createElement('h3');
newNoteTitle.textContent = 'Create new note:';
newNoteTitle.style.cssText = `
margin: 0 0 12px 0;
font-size: 16px;
color: #555;
font-weight: 500;
`;
const newNoteInput = document.createElement('input');
newNoteInput.type = 'text';
newNoteInput.placeholder = 'Enter note name...';
newNoteInput.style.cssText = `
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 14px;
box-sizing: border-box;
margin-bottom: 12px;
`;
const createButton = document.createElement('button');
createButton.textContent = 'Create & Add';
createButton.style.cssText = `
background: #0078d7;
color: white;
border: none;
border-radius: 8px;
padding: 12px 20px;
font-size: 14px;
cursor: pointer;
font-weight: 500;
transition: background 0.2s;
`;
createButton.addEventListener('mouseenter', () => {
createButton.style.background = '#005a9e';
});
createButton.addEventListener('mouseleave', () => {
createButton.style.background = '#0078d7';
});
createButton.addEventListener('click', async () => {
const noteName = newNoteInput.value.trim();
if (!noteName) {
newNoteInput.style.borderColor = '#dc3545';
newNoteInput.focus();
return;
}
if (notes[noteName]) {
newNoteInput.style.borderColor = '#dc3545';
newNoteInput.placeholder = 'Note name already exists!';
newNoteInput.value = '';
newNoteInput.focus();
return;
}
notes[noteName] = formattedText;
await chrome.storage.local.set({ notes });
const successMsg = document.createElement('div');
successMsg.textContent = `Created "${noteName}" and added text!`;
successMsg.style.cssText = `
color: #28a745;
font-weight: 500;
text-align: center;
margin-top: 8px;
`;
modalContent.appendChild(successMsg);
setTimeout(() => {
document.body.removeChild(modal);
}, 1500);
});
const cancelButton = document.createElement('button');
cancelButton.textContent = 'Cancel';
cancelButton.style.cssText = `
background: #6c757d;
color: white;
border: none;
border-radius: 8px;
padding: 12px 20px;
font-size: 14px;
cursor: pointer;
font-weight: 500;
transition: background 0.2s;
margin-left: 8px;
`;
cancelButton.addEventListener('mouseenter', () => {
cancelButton.style.background = '#5a6268';
});
cancelButton.addEventListener('mouseleave', () => {
cancelButton.style.background = '#6c757d';
});
cancelButton.addEventListener('click', () => {
document.body.removeChild(modal);
});
modal.addEventListener('click', (e) => {
if (e.target === modal) {
document.body.removeChild(modal);
}
});
existingNotesSection.appendChild(existingNotesTitle);
existingNotesSection.appendChild(existingNotesList);
newNoteSection.appendChild(newNoteTitle);
newNoteSection.appendChild(newNoteInput);
const buttonContainer = document.createElement('div');
buttonContainer.style.cssText = `
display: flex;
gap: 8px;
justify-content: flex-end;
`;
buttonContainer.appendChild(createButton);
buttonContainer.appendChild(cancelButton);
newNoteSection.appendChild(buttonContainer);
modalContent.appendChild(header);
modalContent.appendChild(textPreview);
modalContent.appendChild(existingNotesSection);
modalContent.appendChild(newNoteSection);
modal.appendChild(modalContent);
document.body.appendChild(modal);
newNoteInput.focus();
})();