30 |
31 |
32 |
--------------------------------------------------------------------------------
/contentScript.js:
--------------------------------------------------------------------------------
1 | function getEmailBody(signatureDelimiter) {
2 | const selector = "div[aria-label='Message Body'], div[aria-label='Message text'], div.editable";
3 | const element = document.querySelector(selector);
4 | if (element) {
5 | let emailText = element.innerHTML;
6 | if (signatureDelimiter && emailText.includes(signatureDelimiter)) {
7 | emailText = emailText.substring(0, emailText.indexOf(signatureDelimiter)).trim();
8 | }
9 | return emailText;
10 | }
11 |
12 | return null;
13 | }
14 |
15 |
16 | async function sendToChatGPT(text, styles, apiKey, signatureDelimiter) {
17 | const response = await fetch('https://api.openai.com/v1/chat/completions', {
18 | method: 'POST',
19 | headers: {
20 | 'Content-Type': 'application/json',
21 | 'Authorization': `Bearer ${apiKey}`
22 | },
23 | body: JSON.stringify({
24 | messages: [{"role": "user", "content": `Review and provide suggestions for the following email draft combining the following styles or only a single style if only one is provided: ${styles.join(', ')}. Please return only the revised email text without suggesting a subject. Email draft: ${text}`}],
25 | model: "gpt-3.5-turbo",
26 | max_tokens: 150,
27 | n: 1,
28 | stop: null,
29 | temperature: 0.8
30 | })
31 | });
32 |
33 | const data = await response.json();
34 | if (data.choices && data.choices.length > 0) {
35 | displaySuggestions(data.choices[0].message.content, signatureDelimiter);
36 | } else {
37 | console.log("No suggestions received");
38 | }
39 | }
40 |
41 | function displaySuggestions(suggestions, signatureDelimiter) {
42 | const selector = "div[aria-label='Message Body'], div[aria-label='Message text'], div.editable";
43 | const element = document.querySelector(selector);
44 | if (element) {
45 | let newText = suggestions.trim();
46 | newText = newText.replace(/\n/g, ' ');
47 | if (signatureDelimiter) {
48 | const signatureIndex = element.innerHTML.indexOf(signatureDelimiter);
49 | if (signatureIndex !== -1) {
50 | const signature = element.innerHTML.substring(signatureIndex);
51 | newText = newText + '
' + signature;
52 | }
53 | }
54 |
55 | if (element.getAttribute('contenteditable') === 'true') {
56 | element.focus();
57 | document.execCommand('selectAll', false, null);
58 | document.execCommand('insertHTML', false, newText);
59 | } else {
60 | element.innerHTML = newText;
61 | }
62 | }
63 | }
64 |
65 |
66 | chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
67 | if (request.action === "reviewEmail") {
68 | const selectedStyles = request.styles;
69 |
70 | if (request) {
71 | chrome.storage.sync.get(["apiKey", "signatureDelimiter"], result => {
72 | if (result.apiKey) {
73 | const emailBody = getEmailBody(result.signatureDelimiter);
74 | sendToChatGPT(emailBody, selectedStyles, result.apiKey, result.signatureDelimiter).then(() => {
75 | sendResponse({ success: true });
76 | }).catch(() => {
77 | sendResponse({ success: false });
78 | });
79 | } else {
80 | alert("Please enter and save your OpenAI API key in the extension settings.");
81 | sendResponse({ success: false });
82 | }
83 | });
84 | } else {
85 | sendResponse({ success: false });
86 | }
87 |
88 | return true;
89 | }
90 | });
91 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | - Using welcoming and inclusive language
12 | - Being respectful of differing viewpoints and experiences
13 | - Gracefully accepting constructive criticism
14 | - Focusing on what is best for the community
15 | - Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | - The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | - Trolling, insulting/derogatory comments, and personal or political attacks
21 | - Public or private harassment
22 | - Publishing others' private information, such as a physical or email address, without their explicit permission
23 | - Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [INSERT EMAIL ADDRESS]. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project contributors who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
44 |
45 | [homepage]: https://www.contributor-covenant.org
46 |
47 | For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq
48 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ChatGPT Email Reviewer
2 |
3 | [](https://opensource.org/licenses/MIT)
4 | [](CONTRIBUTING.md)
5 | [](CODE_OF_CONDUCT.md)
6 | [](https://github.com/hummusonrails/chatgpt-gmail-suggestions-chrome-extension)
7 | [](https://shields.io/)
8 |
9 | This is a Chrome extension that integrates with Gmail to review email drafts using ChatGPT. The extension allows users to select different writing styles for their emails, such as friendly, business, authoritative, personal, casual, serious, and lighthearted. It then uses ChatGPT to analyze and provide suggestions for the email draft based on the chosen writing style.
10 |
11 | ## Installation
12 | To use this extension, you'll need to install it on your Google Chrome browser or any Chromium-based browser. Follow the steps below:
13 |
14 | Download or clone the project from GitHub:
15 |
16 | ```bash
17 | git clone https://github.com/hummusonrails/chatgpt-gmail-suggestions-chrome-extension
18 | ```
19 |
20 | Open the Extensions page in Chrome by navigating to `chrome://extensions/`.
21 |
22 | Enable "Developer mode" in the top right corner of the Extensions page.
23 |
24 | Click the "Load unpacked" button and select the project directory that you cloned or downloaded.
25 |
26 | The extension is now installed and ready to use in your Gmail account.
27 |
28 | ### Configuration
29 |
30 | Before you can use the extension, you'll need to set up a few configuration variables:
31 |
32 | #### OpenAI API Key
33 |
34 | The extension uses the OpenAI API to analyze the email drafts and provide suggestions. To use the API, you'll need an API key from OpenAI. You can obtain an API key by [creating an account on the OpenAI website](https://beta.openai.com/signup/).
35 |
36 | To configure the API key, click the extension icon in your browser's toolbar and enter your OpenAI API key in the provided input field. After entering the API key, click the "Save Settings" button to save it for future use.
37 |
38 | #### Signature Delimiter
39 |
40 | If your email signature is automatically appended to your email drafts in Gmail, you can specify a delimiter to distinguish the signature from the main content of the email. The extension will use this delimiter to exclude the signature from the analysis.
41 |
42 | To configure the signature delimiter, click the extension icon in your browser's toolbar and enter the delimiter in the provided input field. Then click the "Save Settings" button.
43 |
44 | ## Usage
45 |
46 | To use the extension, compose a new email draft in Gmail. After writing the email draft, click the extension icon in your browser's toolbar. Select the desired writing style(s) for your email and click the "Review Email" button.
47 |
48 | The extension will analyze your email draft using ChatGPT and provide suggestions based on the chosen writing style. You can then choose to use the suggestions to improve your email draft.
49 |
50 | ## Code of Conduct
51 |
52 | Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
53 |
54 | ## License
55 |
56 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.
--------------------------------------------------------------------------------