Projects · CEXT Next
An overlay panel for any web page
CEXT Next is a small Chrome extension built on Manifest V3. Click its icon and a panel slides onto the page you're on. You can move it to any of nine spots, and it keeps a list that follows you from tab to tab. It's plain JavaScript with no build step, and makes a readable starting point for your own extension.
github.com/sawissac/cextnextWhat it does
- Click the extension icon on any
httporhttpspage and a panel appears on the right. Click again to hide it, or use the × inside the panel. - Nine arrow buttons move the panel: the four corners, the middle of the top and bottom edges, full height on the left or right, and the centre.
- Type in the input and press Enter or Send to add an item to a list. Each item has its own × to delete it, and an empty list says so.
- The list is saved in
chrome.storage.sync, so the next tab you open the panel in shows the same items. - The icon's badge reads ON on pages where the panel works and OFF everywhere else.
- On a page where the panel can't run, like a new tab, the icon opens a small popup with Google Search and Bing Search buttons instead.
- Right-click the icon and choose Options for the options page.
How the pieces fit
A Manifest V3 extension is a few separate programs that can't see each other's variables. They only talk by passing messages. CEXT Next has four:
| Part | File | Runs in |
|---|---|---|
| Service worker | background.js | The extension, in the background |
| Content scripts | content/js/cextbox.js, controller.js | Every web page you open |
| Action script | action/showcextbox.js | A page, injected on icon click |
| Popup, options | popup.html, options.html | Their own extension pages |
The content scripts build the panel when the page loads, but don't show it. The service worker listens for clicks on the icon. Only when you click does the panel get added to the page, and only then does it connect to the list.
Opening the panel
When you click the icon, the service worker:
- Checks the tab is an
httporhttpspage. Chrome doesn't let extensions run onchrome://pages or the new tab page. - Makes sure the content scripts are there (more on that below).
- Opens a port, a long-lived message channel, to the tab, and sends it the saved list.
- Injects
showcextbox.js, a one-line script that callsshowCextBox()to add or remove the panel.
Saving the list
The list lives in chrome.storage.sync, and the service worker is the only part that writes to it. When you add or delete an item, the panel updates itself straight away and posts the whole new list down the port. The service worker saves it, reads it back, and sends it to the panel again, so what you see always matches what was stored.
port.onMessage.addListener(function (msg) {
chrome.storage.sync.set({ state: msg.state });
chrome.storage.sync.get("state", (res) => {
port.postMessage({ state: res.state || [] });
});
});Because it's storage.sync and not storage.local, the list also follows you to other computers where you're signed in to Chrome with sync on. The trade-off is a small quota: about 100 KB in total, and 8 KB for any one key. The whole list is saved under a single key, so it's fine for short notes and not for anything large.
Tabs that were already open
Content scripts listed in the manifest are only added to pages loaded after the extension is installed or reloaded. Tabs that were already open don't have them, so clicking the icon there would do nothing.
ensureInjected covers that. Before opening the port, it asks the tab whether showCextBox exists. If not, it adds the CSS and the two content scripts on the spot:
const [{ result }] = await chrome.scripting.executeScript({
target: { tabId },
func: () => typeof showCextBox === "function",
});ON, OFF, and the popup
An extension icon either opens a popup or fires a click event, not both. CEXT Next switches between the two per tab. Whenever you change tabs, open one, or a page finishes loading, the service worker checks the URL. On a web page it sets the badge to ON and clears the popup, so a click reaches the service worker and toggles the panel. Anywhere else it sets OFF and points the icon at popup.html, so a click shows the search buttons instead of failing silently.
Moving the panel
Each position is just a set of classes on the panel's outer box: cextbox-pos-left-top, cextbox-pos-right-expand, cextbox-pos-center and so on, with the layout for each in content/css/style.css. The nine buttons share one arrow icon, rotated to point the right way, plus a + for the centre. Clicking one swaps the class.
Lessons from the rewrite
This version is a cleanup of an older one, and two of the fixes are worth knowing about for any extension:
- Add listeners once. The old controller added the Send button's click handler inside the port's message handler, so every message added another one, and one click could add the same item several times. Now every listener is added once when the script loads, and the current port is kept in a variable that the handlers read.
- Forget a port when it closes. The controller listens for
onDisconnectand clears its port, so it never tries to send to a channel that's gone.
Things to know before you build on it
- The manifest asks for
<all_urls>and runs its content scripts on every page. That's what lets the panel open anywhere, but it's a wide permission, and the Chrome Web Store will ask you to justify it. If your extension only needs a few sites, list those instead. - The panel's styles are plain CSS added to the page, so a site with aggressive global styles can bleed into it. Moving the panel into a shadow root would isolate it.
- Other tabs read the list when you next open the panel in them. They aren't pushed live updates while the panel is already open.
Run it yourself
There's no build step, it's plain HTML, CSS and JavaScript.
git clone https://github.com/sawissac/cextnext.git- Open
chrome://extensionsin Chrome. - Turn on Developer mode in the top right.
- Click Load unpacked and pick the
cextnextfolder.
Open any web page and click the icon. After editing the code, press the reload button on the CEXT Next card in chrome://extensions, then reload the page.
CEXT Next is a small project, but it touches most of what a Manifest V3 extension is made of: a service worker, content scripts, scripted injection, ports, synced storage and per-tab popups. That makes it a useful template to copy. The source is on GitHub.