Auto-deploy: 2026-05-18 03:06:29
This commit is contained in:
153
post_feed.js
153
post_feed.js
@@ -218,6 +218,133 @@
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Core: call server and repurpose post ───────────────────────────────
|
||||
async function repurposePost(postEl) {
|
||||
const settings = await getSettings();
|
||||
if (!settings || !settings.apiKey) {
|
||||
alert('Please set your Gemini API key in the extension popup first.\n(Debug: Key not found in storage)');
|
||||
return;
|
||||
}
|
||||
|
||||
const postText = extractPostText(postEl);
|
||||
if (!postText) {
|
||||
alert('Could not read post text. The post might be an image or video.');
|
||||
return;
|
||||
}
|
||||
|
||||
const btn = postEl.querySelector('.lja-rewrite-btn');
|
||||
if (btn) {
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<span class="lja-spinner"></span> Rewriting...';
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await chrome.runtime.sendMessage({
|
||||
type: 'GEMINI_REQUEST',
|
||||
payload: {
|
||||
apiKey: settings.apiKey,
|
||||
action: 'repurposePost',
|
||||
postText: postText
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
throw new Error(response.error || 'Unknown error');
|
||||
}
|
||||
|
||||
const resultText = response.data.result || response.data;
|
||||
|
||||
// We reuse the comment box UI but style it differently, or just use the same box but change the title and actions.
|
||||
createRepurposeBox(postEl, resultText);
|
||||
|
||||
} catch (e) {
|
||||
console.error('[LJA Feed]', e);
|
||||
alert('Post rewriting failed: ' + e.message);
|
||||
} finally {
|
||||
if (btn) {
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '📝 Rewrite';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Create the repurpose result box ─────────────────────────────────────
|
||||
function createRepurposeBox(postEl, resultText) {
|
||||
const existing = postEl.querySelector('.' + BOX_CLASS);
|
||||
if (existing) existing.remove();
|
||||
|
||||
const isRTL = /[\u0600-\u06FF]/.test(resultText);
|
||||
|
||||
// Split text into Post and Image Prompt if the separator exists
|
||||
let postContent = resultText;
|
||||
let imagePrompt = '';
|
||||
const parts = resultText.split('--- IMAGE PROMPT ---');
|
||||
if (parts.length > 1) {
|
||||
postContent = parts[0].trim();
|
||||
imagePrompt = parts[1].trim();
|
||||
}
|
||||
|
||||
const box = document.createElement('div');
|
||||
box.className = BOX_CLASS;
|
||||
box.innerHTML = `
|
||||
<div class="lja-cb-header" style="background: linear-gradient(135deg, #FF6B6B 0%, #C92A2A 100%);">
|
||||
<span class="lja-cb-icon">✍️</span>
|
||||
<span class="lja-cb-title">Rewritten Post</span>
|
||||
<button class="lja-cb-close" title="Close">✕</button>
|
||||
</div>
|
||||
<div style="padding: 12px; font-size: 13px; color: #666; font-weight: 500; border-bottom: 1px solid rgba(0,0,0,0.05);">
|
||||
Post Content:
|
||||
</div>
|
||||
<textarea
|
||||
class="lja-cb-text"
|
||||
dir="${isRTL ? 'rtl' : 'ltr'}"
|
||||
style="text-align: ${isRTL ? 'right' : 'left'}; min-height: 120px;"
|
||||
>${postContent}</textarea>
|
||||
${imagePrompt ? `
|
||||
<div style="padding: 12px; font-size: 13px; color: #666; font-weight: 500; border-top: 1px solid rgba(0,0,0,0.05); border-bottom: 1px solid rgba(0,0,0,0.05);">
|
||||
Image Generation Prompt (Midjourney / DALL-E):
|
||||
</div>
|
||||
<textarea
|
||||
class="lja-cb-image-prompt"
|
||||
style="width: 100%; border: none; padding: 12px; background: rgba(0,0,0,0.02); resize: vertical; min-height: 80px; font-family: monospace; font-size: 12px; outline: none;"
|
||||
readonly
|
||||
>${imagePrompt}</textarea>
|
||||
` : ''}
|
||||
<div class="lja-cb-actions">
|
||||
<button class="lja-cb-copy">📋 Copy Post</button>
|
||||
${imagePrompt ? '<button class="lja-cb-copy-img">🎨 Copy Image Prompt</button>' : ''}
|
||||
</div>
|
||||
`;
|
||||
|
||||
box.querySelector('.lja-cb-close').addEventListener('click', () => box.remove());
|
||||
|
||||
box.querySelector('.lja-cb-copy').addEventListener('click', function () {
|
||||
const text = box.querySelector('.lja-cb-text').value;
|
||||
navigator.clipboard.writeText(text).then(() => {
|
||||
this.textContent = '✅ Post Copied!';
|
||||
setTimeout(() => { this.textContent = '📋 Copy Post'; }, 1500);
|
||||
});
|
||||
});
|
||||
|
||||
const copyImgBtn = box.querySelector('.lja-cb-copy-img');
|
||||
if (copyImgBtn) {
|
||||
copyImgBtn.addEventListener('click', function () {
|
||||
const text = box.querySelector('.lja-cb-image-prompt').value;
|
||||
navigator.clipboard.writeText(text).then(() => {
|
||||
this.textContent = '✅ Prompt Copied!';
|
||||
setTimeout(() => { this.textContent = '🎨 Copy Image Prompt'; }, 1500);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const actionBar = findActionBar(postEl);
|
||||
if (actionBar) {
|
||||
actionBar.parentNode.insertBefore(box, actionBar.nextSibling);
|
||||
} else {
|
||||
postEl.appendChild(box);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Inject button into a single post ────────────────────────────────────
|
||||
function injectButton(postEl) {
|
||||
if (postEl.querySelector('.' + BUTTON_CLASS)) return; // Already injected
|
||||
@@ -238,7 +365,33 @@
|
||||
generateComment(postEl);
|
||||
});
|
||||
|
||||
const rewriteBtn = document.createElement('button');
|
||||
rewriteBtn.className = 'lja-rewrite-btn';
|
||||
rewriteBtn.innerHTML = '📝 Rewrite';
|
||||
rewriteBtn.title = 'Rewrite this post in your own style and generate an image prompt';
|
||||
|
||||
// Add some inline styles for the rewrite button to differentiate it
|
||||
rewriteBtn.style.background = 'linear-gradient(135deg, #FF8787 0%, #E03131 100%)';
|
||||
rewriteBtn.style.color = 'white';
|
||||
rewriteBtn.style.border = 'none';
|
||||
rewriteBtn.style.borderRadius = '16px';
|
||||
rewriteBtn.style.padding = '4px 12px';
|
||||
rewriteBtn.style.fontSize = '12px';
|
||||
rewriteBtn.style.fontWeight = '600';
|
||||
rewriteBtn.style.cursor = 'pointer';
|
||||
rewriteBtn.style.marginLeft = '8px';
|
||||
rewriteBtn.style.display = 'flex';
|
||||
rewriteBtn.style.alignItems = 'center';
|
||||
rewriteBtn.style.gap = '4px';
|
||||
|
||||
rewriteBtn.addEventListener('click', (e) => {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
repurposePost(postEl);
|
||||
});
|
||||
|
||||
actionBar.appendChild(btn);
|
||||
actionBar.appendChild(rewriteBtn);
|
||||
}
|
||||
|
||||
// ─── Process all posts on the page ───────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user