-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
162 lines (127 loc) · 5.2 KB
/
Copy pathpopup.js
File metadata and controls
162 lines (127 loc) · 5.2 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
document.addEventListener('DOMContentLoaded', function() {
const uploadBtn = document.getElementById('uploadBtn');
const convertBtn = document.getElementById('convertBtn');
const preview = document.getElementById('preview');
const status = document.getElementById('status');
let markdownContent = '';
let fileName = '';
uploadBtn.addEventListener('click', function() {
uploadMarkdownFile();
});
convertBtn.addEventListener('click', function() {
convertToMedium();
});
// Upload markdown file function
function uploadMarkdownFile() {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.md,.markdown,.txt';
fileInput.multiple = false;
fileInput.style.display = 'none';
fileInput.onchange = function(event) {
const file = event.target.files[0];
if (!file) {
showStatus('No file selected', 'error');
return;
}
// Validate file type
const validExtensions = ['.md', '.markdown', '.txt'];
const fileExtension = file.name.toLowerCase().substring(file.name.lastIndexOf('.'));
if (!validExtensions.includes(fileExtension)) {
showStatus('Please select a valid markdown file (.md, .markdown, .txt)', 'error');
return;
}
// Validate file size (max 5MB)
if (file.size > 5 * 1024 * 1024) {
showStatus('File too large. Please select a file smaller than 5MB', 'error');
return;
}
fileName = file.name;
showStatus(`Loading ${fileName}...`, 'info');
const reader = new FileReader();
reader.onload = function(e) {
markdownContent = e.target.result;
// Show preview (first 300 characters)
const previewText = markdownContent;
preview.innerHTML = `<pre>${escapeHtml(previewText)}</pre>`;
showStatus(`✅ ${fileName} loaded successfully (${markdownContent.length} characters)`, 'success');
};
reader.onerror = function() {
showStatus('Error reading file. Please try again.', 'error');
};
reader.readAsText(file);
};
// Clean up previous file input if exists
const existingInput = document.getElementById('temp-file-input');
if (existingInput) {
existingInput.remove();
}
fileInput.id = 'temp-file-input';
document.body.appendChild(fileInput);
fileInput.click();
// Clean up after click
setTimeout(() => {
if (fileInput.parentNode) {
fileInput.remove();
}
}, 1000);
}
// Convert to Medium function
function convertToMedium() {
if (!markdownContent) {
showStatus('Please upload a markdown file first', 'error');
return;
}
showStatus('Checking Medium page...', 'info');
// Check if we're on a Medium page
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (!tabs[0]) {
showStatus('No active tab found', 'error');
return;
}
const currentUrl = tabs[0].url;
// Check if on Medium new-story page
//if (!currentUrl.includes('medium.com/new-story')) {
// showStatus('Please navigate to medium.com/new-story first', 'error');
// return;
//}
window.close();
// Send message to content script
chrome.tabs.sendMessage(tabs[0].id, {
action: 'insertContent',
content: markdownContent,
}, (response) => {
if (chrome.runtime.lastError) {
showStatus('Cannot connect to Medium page. Please refresh the page and try again.', 'error');
return;
}
if (response && response.success) {
showStatus('✅ Content inserted into Medium editor!', 'success');
} else {
const errorMsg = response ? response.message : 'Failed to insert content';
showStatus(errorMsg, 'error');
}
});
});
}
// Show status messages
function showStatus(message, type) {
status.textContent = message;
status.className = `status ${type}`;
status.classList.remove('hidden');
// Auto-hide after 5 seconds for info messages
if (type === 'info') {
setTimeout(() => {
status.classList.add('hidden');
}, 5000);
}
}
// Escape HTML to prevent XSS
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Initialize
showStatus('Ready! Upload a markdown file to get started.', 'info');
});