// ==UserScript== // @name מעצב אימיילים אוטומטי - Gmail Gemini // @namespace http://tampermonkey.net/ // @version 2.2 // @description הוספת כפתור עיצוב AI מתקדם לסרגל הכלים של כתיבת מייל ב-Gmail כולל חלון הגדרות מובנה ומוצמד // @match https://mail.google.com/* // @updateURL https://raw.githubusercontent.com/MOTEL-hue/gmail-formatter.user.js/main/gmail-formatter.user.js // @downloadURL https://raw.githubusercontent.com/MOTEL-hue/gmail-formatter.user.js/main/gmail-formatter.user.js // @grant GM_setValue // @grant GM_getValue // @run-at document-end // ==/UserScript== (function() { 'use strict'; function injectAiButton() { const toolbars = document.querySelectorAll('table.IZ td, div.n1'); toolbars.forEach(toolbar => { if (toolbar.querySelector('.gemini-mail-wrapper')) return; const referenceNode = toolbar.querySelector('div.J-J5-Ji') || toolbar.firstChild; if (!referenceNode) return; const wrapper = document.createElement('div'); wrapper.className = 'J-J5-Ji gemini-mail-wrapper'; wrapper.style.cssText = 'display: inline-block; position: relative; vertical-align: middle; display: inline-flex; align-items: center;'; // כפתור הקסם המרכזי const aiBtn = document.createElement('div'); aiBtn.style.cssText = 'display: inline-block; cursor: pointer; padding: 0 4px; vertical-align: middle;'; aiBtn.title = 'עיצוב אימייל אוטומטי באמצעות AI'; aiBtn.innerHTML = `
`; // חץ קטן לפתיחת חלון ההגדרות המוצמד const dropdownBtn = document.createElement('span'); dropdownBtn.innerHTML = '▾'; dropdownBtn.title = 'הגדרות תוסף'; dropdownBtn.style.cssText = 'cursor: pointer; font-size: 10px; color: #5f6368; padding: 0 3px; vertical-align: middle; user-select: none; opacity: 0.7;'; // יצירת חלון ההגדרות המוצמד (Popup) const popup = document.createElement('div'); popup.style.cssText = 'display: none; position: absolute; bottom: 30px; right: 0; background: #ffffff; border: 1px solid #dadce0; box-shadow: 0 4px 12px rgba(0,0,0,0.15); border-radius: 8px; padding: 12px; z-index: 9999; width: 240px; font-family: Arial, sans-serif; font-size: 12px; color: #3c4043; text-align: right; direction: rtl;'; popup.innerHTML = `
הגדרות מעצב AI
`; wrapper.appendChild(aiBtn); wrapper.appendChild(dropdownBtn); wrapper.appendChild(popup); // פתיחה/סגירה של החלון בלחיצה על החץ dropdownBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); const isVisible = popup.style.display === 'block'; if (!isVisible) { // טעינת הנתונים הקיימים לתוך השדות לפני הפתיחה popup.querySelector('.api-input').value = GM_getValue('gemini_api_key', ''); popup.querySelector('.mode-select').value = GM_getValue('gemini_mode', 'strict'); popup.querySelector('.speed-select').value = GM_getValue('gemini_speed', 'fast'); popup.style.display = 'block'; } else { popup.style.display = 'none'; } }); // סגירת החלון בלחיצה על ה-X popup.querySelector('.close-popup').addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); popup.style.display = 'none'; }); // שמירת ההגדרות מתוך החלון popup.querySelector('.save-settings').addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); const newKey = popup.querySelector('.api-input').value.trim(); const newMode = popup.querySelector('.mode-select').value; const newSpeed = popup.querySelector('.speed-select').value; if (newKey !== '') { GM_setValue('gemini_api_key', newKey); } GM_setValue('gemini_mode', newMode); GM_setValue('gemini_speed', newSpeed); popup.style.display = 'none'; alert('ההגדרות נשמרו בהצלחה!'); }); // סגירת החלון בלחיצה מחוץ לו document.addEventListener('click', (e) => { if (!wrapper.contains(e.target)) { popup.style.display = 'none'; } }); // הפעלת העיצוב בלחיצה על כפתור הקסם aiBtn.addEventListener('click', async (e) => { e.preventDefault(); e.stopPropagation(); popup.style.display = 'none'; const composeWindow = toolbar.closest('div.AD, div.M9'); const editableArea = composeWindow ? composeWindow.querySelector('div[contenteditable="true"]') : document.querySelector('div[contenteditable="true"]'); if (!editableArea) { alert('לא נמצאה תיבת טקסט פעילה לכתיבת מייל.'); return; } const originalText = editableArea.innerText.trim(); if (!originalText) { alert('אנא כתוב קודם תוכן כלשהו בגוף המייל.'); return; } let apiKey = GM_getValue('gemini_api_key'); if (!apiKey) { alert('אנא לחץ על החץ הקטן ליד כפתור הקסם והגדר את מפתח ה-API שלך.'); popup.style.display = 'block'; return; } const mode = GM_getValue('gemini_mode', 'strict'); const speed = GM_getValue('gemini_speed', 'fast'); const temperature = mode === 'strict' ? 0.1 : 0.3; aiBtn.style.opacity = '0.5'; let promptText = ''; if (mode === 'strict') { promptText = `תפקידך לעצב ולסדר את טקסט האימייל הבא מבלי לשנות את המשמעות או להמציא פרטים חדשים שלא היו בטקסט המקורי. הנחיות מחייבות: 1. שמור על כל העובדות והנתונים המקוריים. אסור להמציא מידע חדש. 2. תקן שגיאות כתיב, ללטש מעט ניסוח וסדר את הטקסט בצורה נקייה ומקצועית עם הדגשות () ורווחים. 3. אסור להכניס את הפלט לתוך תיבת קוד, ואסור לייצר רקע אפור או מסגרת. החזר אך ורק את קוד ה-HTML הנקי. הטקסט לעיצוב בלבד: ${originalText}`; } else { promptText = `עצב ושפר את טקסט האימייל הבא בסגנון של הצעת מחיר או מסמך מקצועי ומסודר, עם כותרות מודגשות ורווחים נקיים. מותר לשפר ניסוח ותיקון שגיאות בצורה זורמת יותר. חובה להקפיד: אסור להכניס את הפלט לתוך תיבת קוד, ואסור לייצר רקע אפור או מסגרת. החזר אך ורק את קוד ה-HTML הנקי. הטקסט לעיצוב ושדרוג: ${originalText}`; } try { const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:generateContent?key=${apiKey}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ contents: [{ parts: [{ text: promptText }] }], generationConfig: { temperature: temperature } }) }); const data = await response.json(); if (data.candidates && data.candidates[0].content.parts[0].text) { let aiResult = data.candidates[0].content.parts[0].text; aiResult = aiResult.replace(/^```(html)?\n?/i, '').replace(/\n?```$/i, '').trim(); editableArea.innerHTML = aiResult; editableArea.dispatchEvent(new Event('input', { bubbles: true })); } else { throw new Error(data.error?.message || 'שגיאה בעיבוד הנתונים.'); } } catch (error) { alert('שגיאה: ' + error.message); if (error.message.includes('API key')) GM_setValue('gemini_api_key', ''); } finally { aiBtn.style.opacity = '1'; } }); toolbar.insertBefore(wrapper, referenceNode); }); } const observer = new MutationObserver(() => { injectAiButton(); }); observer.observe(document.body, { childList: true, subtree: true }); })();