|
| 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. |
0 commit comments