Skip to content

Commit 18a1549

Browse files
committed
initial launch🚀
0 parents  commit 18a1549

File tree

18 files changed

+1061
-0
lines changed

18 files changed

+1061
-0
lines changed

‎README.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Aha: ELI5 Chrome Extension ![icon](/icons/icon32.png)
2+
3+
![demo](assets/demo.gif)
4+
5+
## Overview
6+
7+
**Aha** is a Chrome extension that simplifies complex terms into easy-to-understand "Explain Like I'm 5" (ELI5) explanations. Highlight any phrase and get a quick breakdown using the **Cerebras Llama 3.1 80b** language model, with extra resources provided by **Exa.ai**.
8+
9+
## Features
10+
11+
- **easy access**: Highlight text and hit Command/Ctrl + Shift + X
12+
- **lightning fast**: Uses **Cerebras Llama 3.1 80b** for lighting-fast responses.
13+
- **context-aware**: Uses the page title as context to explain the highlighted term
14+
- **useful resources**: Fetch additional learning materials via **Exa.ai**.
15+
- **markdown support**: easily copy explanations in markdown format
16+
- **one-click learning**: open all links in new tabs with one click.
17+
- **search history**: previous search terms stored in local storage
18+
19+
## Installation
20+
21+
1. Clone the repository:
22+
```
23+
git clone https://github.com/benthecoder/aha.git
24+
```
25+
2. Replace API keys
26+
- Open `src/cerebras.js` and replace `CEREBRAS_API_KEY` with your [Cerebras](https://www.cerebras.ai/) API key.
27+
- Open `src/exa.js` and replace `EXA_API_KEY` with your [Exa.ai](https://exa.ai/) API key.
28+
3. Load the extension in Chrome:
29+
- Open `chrome://extensions/` in Chrome.
30+
- Enable "Developer mode" in the top right.
31+
- Click "Load unpacked" and select the project
32+
33+
## Future
34+
35+
- create a server to host API calls

‎assets/demo.gif‎

2.27 MB
Loading

‎assets/explain.png‎

388 KB
Loading

‎assets/popup.png‎

50.5 KB
Loading

‎howitworks.md‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# how it works
2+
3+
how `background.js` and `content.js` work together. Let's break it down:
4+
5+
1. Background Script (`background.js`):
6+
7+
- Runs continuously in the background
8+
- Listens for commands and messages
9+
- Communicates with external APIs (Cerebras and Exa)
10+
- Manages tab operations
11+
12+
2. Content Script (`content.js`):
13+
- Injected into web pages
14+
- Interacts with the webpage's DOM
15+
- Creates and manages the explanation popup
16+
- Handles user interactions within the popup
17+
18+
Now, let's look at the key listeners and their roles:
19+
20+
3. `chrome.commands.onCommand.addListener`:
21+
22+
```6:14:src/background.js
23+
chrome.commands.onCommand.addListener((command) => {
24+
console.log('Command received:', command);
25+
if (command === 'explainText') {
26+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
27+
console.log('Sending getSelectedText message to tab:', tabs[0].id);
28+
chrome.tabs.sendMessage(tabs[0].id, { action: 'getSelectedText' });
29+
});
30+
}
31+
});
32+
```
33+
34+
- Listens for the keyboard shortcut (Ctrl+Shift+X or Cmd+Shift+X)
35+
- When triggered, it sends a message to the active tab to get selected text
36+
37+
4. `chrome.runtime.onMessage.addListener` (in background.js):
38+
39+
```16:26:src/background.js
40+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
41+
console.log('Message received in background:', request);
42+
if (request.action === 'explainText') {
43+
console.log('Calling explainText with:', request.text);
44+
explainText(sender.tab.id, request.text);
45+
} else if (request.action === 'openAllLinks') {
46+
console.log('Received openAllLinks request:', request);
47+
openAllLinks(request.urls);
48+
}
49+
return true;
50+
});
51+
```
52+
53+
- Listens for messages from content scripts
54+
- Handles 'explainText' and 'openAllLinks' actions
55+
56+
5. `chrome.runtime.onMessage.addListener` (in content.js):
57+
58+
```9:27:src/content.js
59+
// Message handling
60+
function handleMessage(request, sender, sendResponse) {
61+
console.log('Message received in content script:', request);
62+
63+
switch (request.action) {
64+
case 'getSelectedText':
65+
handleGetSelectedText();
66+
break;
67+
case 'showLoader':
68+
showLoader(request.selectedText);
69+
break;
70+
case 'showExplanation':
71+
handleShowExplanation(request);
72+
break;
73+
}
74+
75+
return true;
76+
}
77+
78+
```
79+
80+
- Listens for messages from the background script
81+
- Handles 'getSelectedText', 'showLoader', and 'showExplanation' actions
82+
83+
Now, let's walk through the entire flow:
84+
85+
1. User selects text on a webpage and presses the keyboard shortcut.
86+
2. Background script receives the command and sends a 'getSelectedText' message to the active tab.
87+
3. Content script receives the message, gets the selected text, and sends it back to the background script with an 'explainText' action.
88+
4. Background script receives the 'explainText' message and:
89+
a. Sends a 'showLoader' message to the content script.
90+
b. Calls the Cerebras API for an explanation.
91+
c. If enabled, calls the Exa API for related links.
92+
d. Combines the responses and sends a 'showExplanation' message to the content script.
93+
5. Content script receives the 'showExplanation' message and:
94+
a. Creates or updates the explanation popup with the received data.
95+
b. Sets up event listeners for user interactions (copy, close, open links).
96+
6. If the user clicks the "Open All Links" button, the content script sends an 'openAllLinks' message to the background script.
97+
7. The background script receives the 'openAllLinks' message and opens the URLs in new tabs.
98+
99+
## commands
100+
101+
Here's a summary of the main commands used in the extension, along with a brief description of their purpose:
102+
103+
- `chrome.commands.onCommand.addListener`:
104+
• Listens for the keyboard shortcut to trigger the text explanation feature.
105+
106+
- `chrome.runtime.onMessage.addListener` (in background.js):
107+
• Handles messages from content scripts, including requests for explanations and opening links.
108+
109+
- `chrome.runtime.onMessage.addListener` (in content.js):
110+
• Processes messages from the background script to manage the popup and user interactions.
111+
112+
- `chrome.tabs.sendMessage`:
113+
• Sends messages from the background script to specific tabs, initiating actions in the content script.
114+
115+
- `chrome.runtime.sendMessage`:
116+
• Sends messages from the content script to the background script, typically to request explanations or open links.
117+
118+
- `chrome.storage.local.get` and `chrome.storage.local.set`:
119+
• Manages local storage for saving user preferences and explanation history.
120+
121+
- `chrome.tabs.query`:
122+
• Finds the active tab to send messages or perform actions on the current page.
123+
124+
- `chrome.tabs.create`:
125+
• Opens new tabs when the user requests to open all related links.
126+
127+
- `chrome.tabs.update`:
128+
• Switches back to the original tab after opening related links in new tabs.
129+
130+
These commands work together to create a seamless flow of communication between different parts of the extension and the browser, enabling the core functionality of text explanation and link management.

‎icons/icon128.png‎

2.29 KB
Loading

‎icons/icon16.png‎

239 Bytes
Loading

‎icons/icon32.png‎

495 Bytes
Loading

‎icons/icon48.png‎

666 Bytes
Loading

‎lib/marked.min.js‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)