diff --git a/extension/background.js b/extension/background.js index 3ce4bd2..55f323c 100644 --- a/extension/background.js +++ b/extension/background.js @@ -13,7 +13,7 @@ function getUserEmail() { } chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { if (request.type === 'GENERATE_BLOG') { - const { title, description, code, author, client_time, custom_prompt, difficulty } = request.payload; + const { title, description, code, author, client_time, custom_prompt, difficulty, topics } = request.payload; chrome.storage.local.get({ publishingPlatforms: ['devto'], publishAsDraft: false, @@ -28,34 +28,35 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { headers: { "Content-Type": "application/json", "X-User-Email": userEmail }, body: JSON.stringify({ title, description, code, author, client_time, custom_prompt, difficulty, + tags: (topics && topics.length > 0) ? topics : null, platforms: publishingPlatforms, publish_as_draft: publishAsDraft }) }) - .then(r => r.json()) - .then(data => { - if (data.status === 'success' || data.status === 'partial_success'){ - const generatedBlog = - data.data?.blog_content || ""; + .then(r => r.json()) + .then(data => { + if (data.status === 'success' || data.status === 'partial_success') { + const generatedBlog = + data.data?.blog_content || ""; - chrome.storage.local.set({ - generatedBlog, - generatedProblemTitle: title - }, () => { + chrome.storage.local.set({ + generatedBlog, + generatedProblemTitle: title + }, () => { - chrome.runtime.sendMessage({ - type: "BLOG_READY" - }); + chrome.runtime.sendMessage({ + type: "BLOG_READY" + }); - }); - } + }); + } - if (data.status === 'success' || data.status === 'partial_success') { - const platforms = data.data?.platforms || []; - const postedPlatforms = platforms - .filter(result => result.status === 'success') - .map(result => result.platform) - .join(', '); + if (data.status === 'success' || data.status === 'partial_success') { + const platforms = data.data?.platforms || []; + const postedPlatforms = platforms + .filter(result => result.status === 'success') + .map(result => result.platform) + .join(', '); const devtoResult = platforms.find(r => r.platform === 'devto' && r.status === 'success'); chrome.storage.local.get({ publishHistory: [] }, (res) => { const entry = { @@ -68,53 +69,53 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { history.unshift(entry); chrome.storage.local.set({ publishHistory: history.slice(0, 10) }); }); - const failedPlatforms = platforms - .filter(r => r.status === 'error').map(r => r.platform); - - const entry = { - title, - date: client_time || new Date().toISOString(), - platforms: postedPlatforms, - status: data.status, - author, - user_email: userEmail, - }; - - const historyKey = `publishHistory_${userEmail}`; - chrome.storage.local.get({ [historyKey]: [] }, (res) => { - const history = (res[historyKey] || []).filter(h => h.title !== entry.title); - history.unshift(entry); - chrome.storage.local.set({ [historyKey]: history.slice(0, 100) }); - }); - - fetch(`${API_BASE_URL}/dashboard/record`, { - method: "POST", - headers: { "Content-Type": "application/json", "X-User-Email": userEmail }, - body: JSON.stringify(entry) - }).catch(() => {}); - chrome.runtime.sendMessage({ - type: 'STATUS_UPDATE', - message: - failedPlatforms.length > 0 - ? `Posted to ${postedPlatforms.join(', ')}; failed: ${failedPlatforms.join(', ')}` - : postedPlatforms.length > 0 - ? `Posted to ${postedPlatforms.join(', ')}` - : 'Posted', - status: data.status === 'partial_success' ? 'warning' : 'success', - platforms - }); - } else { - const platformErrors = data.data?.platforms - ?.filter(result => result.status === 'error') - ?.map(result => `${result.platform}: ${result.message}`) - ?.join('; '); - const errMsg = platformErrors || data.message || JSON.stringify(data); - chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: 'Error: ' + errMsg, status: 'error' }); - } - }) - .catch(error => { - chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: 'Network Error: ' + error.message, status: 'error' }); - }); + const failedPlatforms = platforms + .filter(r => r.status === 'error').map(r => r.platform); + + const entry = { + title, + date: client_time || new Date().toISOString(), + platforms: postedPlatforms, + status: data.status, + author, + user_email: userEmail, + }; + + const historyKey = `publishHistory_${userEmail}`; + chrome.storage.local.get({ [historyKey]: [] }, (res) => { + const history = (res[historyKey] || []).filter(h => h.title !== entry.title); + history.unshift(entry); + chrome.storage.local.set({ [historyKey]: history.slice(0, 100) }); + }); + + fetch(`${API_BASE_URL}/dashboard/record`, { + method: "POST", + headers: { "Content-Type": "application/json", "X-User-Email": userEmail }, + body: JSON.stringify(entry) + }).catch(() => { }); + chrome.runtime.sendMessage({ + type: 'STATUS_UPDATE', + message: + failedPlatforms.length > 0 + ? `Posted to ${postedPlatforms.join(', ')}; failed: ${failedPlatforms.join(', ')}` + : postedPlatforms.length > 0 + ? `Posted to ${postedPlatforms.join(', ')}` + : 'Posted', + status: data.status === 'partial_success' ? 'warning' : 'success', + platforms + }); + } else { + const platformErrors = data.data?.platforms + ?.filter(result => result.status === 'error') + ?.map(result => `${result.platform}: ${result.message}`) + ?.join('; '); + const errMsg = platformErrors || data.message || JSON.stringify(data); + chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: 'Error: ' + errMsg, status: 'error' }); + } + }) + .catch(error => { + chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: 'Network Error: ' + error.message, status: 'error' }); + }); }); } }); @@ -145,65 +146,65 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { author: "Anonymous Developer" }) }) - .then(r => r.json()) - .then(data => { - if (data.status === 'success' || data.status === 'partial_success') { - const platforms = data.data?.platforms || []; - const postedPlatforms = platforms - .filter(r => r.status === 'success').map(r => r.platform); - const failedPlatforms = platforms - .filter(r => r.status === 'error').map(r => r.platform); - - const entry = { - title: generatedProblemTitle, - date: new Date().toISOString(), - platforms: postedPlatforms, - status: data.status, - author: "Anonymous Developer", - user_email: userEmail, - }; - - const historyKey = `publishHistory_${userEmail}`; - chrome.storage.local.get({ [historyKey]: [] }, (res) => { - const history = (res[historyKey] || []).filter(h => h.title !== entry.title); - history.unshift(entry); - chrome.storage.local.set({ [historyKey]: history.slice(0, 100) }); - }); - - fetch(`${API_BASE_URL}/dashboard/record`, { - method: "POST", - headers: { "Content-Type": "application/json", "X-User-Email": userEmail }, - body: JSON.stringify(entry) - }).catch(() => {}); + .then(r => r.json()) + .then(data => { + if (data.status === 'success' || data.status === 'partial_success') { + const platforms = data.data?.platforms || []; + const postedPlatforms = platforms + .filter(r => r.status === 'success').map(r => r.platform); + const failedPlatforms = platforms + .filter(r => r.status === 'error').map(r => r.platform); + + const entry = { + title: generatedProblemTitle, + date: new Date().toISOString(), + platforms: postedPlatforms, + status: data.status, + author: "Anonymous Developer", + user_email: userEmail, + }; + + const historyKey = `publishHistory_${userEmail}`; + chrome.storage.local.get({ [historyKey]: [] }, (res) => { + const history = (res[historyKey] || []).filter(h => h.title !== entry.title); + history.unshift(entry); + chrome.storage.local.set({ [historyKey]: history.slice(0, 100) }); + }); + + fetch(`${API_BASE_URL}/dashboard/record`, { + method: "POST", + headers: { "Content-Type": "application/json", "X-User-Email": userEmail }, + body: JSON.stringify(entry) + }).catch(() => { }); - const successMsg = failedPlatforms.length > 0 - ? `Posted to ${postedPlatforms.join(', ')}; failed: ${failedPlatforms.join(', ')}` - : postedPlatforms.length > 0 - ? `Posted to ${postedPlatforms.join(', ')}` - : 'Posted'; + const successMsg = failedPlatforms.length > 0 + ? `Posted to ${postedPlatforms.join(', ')}; failed: ${failedPlatforms.join(', ')}` + : postedPlatforms.length > 0 + ? `Posted to ${postedPlatforms.join(', ')}` + : 'Posted'; - chrome.runtime.sendMessage({ - type: 'STATUS_UPDATE', - message: successMsg, - status: data.status === 'partial_success' ? 'warning' : 'success', - platforms - }); - sendResponse({ success: true, message: successMsg, data }); - } else { - const platformErrors = data.data?.platforms - ?.filter(result => result.status === 'error') - ?.map(result => `${result.platform}: ${result.message}`) - ?.join('; '); - const errMsg = platformErrors || data.message || JSON.stringify(data); - chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: 'Error: ' + errMsg, status: 'error' }); + chrome.runtime.sendMessage({ + type: 'STATUS_UPDATE', + message: successMsg, + status: data.status === 'partial_success' ? 'warning' : 'success', + platforms + }); + sendResponse({ success: true, message: successMsg, data }); + } else { + const platformErrors = data.data?.platforms + ?.filter(result => result.status === 'error') + ?.map(result => `${result.platform}: ${result.message}`) + ?.join('; '); + const errMsg = platformErrors || data.message || JSON.stringify(data); + chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: 'Error: ' + errMsg, status: 'error' }); + sendResponse({ success: false, error: errMsg }); + } + }) + .catch(error => { + const errMsg = 'Network Error: ' + error.message; + chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: errMsg, status: 'error' }); sendResponse({ success: false, error: errMsg }); - } - }) - .catch(error => { - const errMsg = 'Network Error: ' + error.message; - chrome.runtime.sendMessage({ type: 'STATUS_UPDATE', message: errMsg, status: 'error' }); - sendResponse({ success: false, error: errMsg }); - }); + }); }); return true; } diff --git a/extension/content.js b/extension/content.js index 4f42905..6170988 100644 --- a/extension/content.js +++ b/extension/content.js @@ -55,6 +55,13 @@ document.querySelector('.ant-select-selection-item'); const language = langElement ? langElement.innerText.trim().toLowerCase() : "unknown"; + // Extract topic tags (e.g. Array, Dynamic Programming, Hash Table) + const tagElements = document.querySelectorAll('a[href*="/tag/"]'); + const topics = Array.from(tagElements) + .map(el => el.innerText.trim().toLowerCase()) + .filter(Boolean) + .slice(0, 4); // Dev.to allows max 4 tags + // Extract the user's LeetCode Username let author = "Anonymous LeetCoder"; const allLinks = document.querySelectorAll('a[href^="/u/"]'); @@ -79,7 +86,7 @@ // Send to background script chrome.runtime.sendMessage({ type: 'GENERATE_BLOG', - payload: { title, description, code, author, client_time, custom_prompt, difficulty, language } + payload: { title, description, code, author, client_time, custom_prompt, difficulty, language, topics } });