DEV Community

Marc Cheng
Marc Cheng

Posted on AI-assisted

macOS desktop, in a single static HTML page

My Bookmarks bar fits about eight sites. Everything else lives in folders I never open.

So I built Home Page OS: a new tab page that lays out all of your bookmarks like a Mac desktop. Folders in your Bookmarks bar become app categories, loose bookmarks sit on the desktop as icons, and when you change a bookmark, the desktop follows.

Home Page OS desktop
Home Page OS desktop

Here's how it's put together, and a few browser quirks I ran into along the way.

The rules I set myself

  1. No framework, no bundler, no build step. One index.html, plain CSS, and 13 plain scripts loaded with defer. No ES modules either, so you can clone the repo and double-click index.html straight from disk.
  2. One codebase, two products. The same page is my personal website and the browser extension.
  3. Nothing leaves your browser. No account, no analytics, no server. State lives in localStorage.

About 3,500 lines of JavaScript in the end, most of it UI: windows, a dock, drag-and-drop on a grid, six classic macOS wallpapers, and six languages.

One page, two products

The website and the extension ship the same files. A small Python script packages the extension, and it only does three things:

  • swaps the page's SEO <title> for New Tab, so the tab strip doesn't show my name;
  • copies src/ and the icons next to a manifest.json that overrides the new tab page;
  • for Firefox, also points the homepage at the page.

That last one surprised me. In Firefox, chrome_url_overrides.newtab only covers new tabs; a new window opens your homepage. Chrome uses the new tab page for both. So the Firefox build adds:

"chrome_settings_overrides": { "homepage": "index.html" }
Enter fullscreen mode Exit fullscreen mode

At runtime the page has to know which one it is. The obvious check is wrong:

// `chrome` exists on *every* page in Chrome, not just extension pages.
// `permissions` only exists on extension pages, so check for that instead.
const EXT_API = (globalThis.browser && globalThis.browser.permissions) ? globalThis.browser
  : (globalThis.chrome && globalThis.chrome.permissions) ? globalThis.chrome : null;
Enter fullscreen mode Exit fullscreen mode

On the website, EXT_API is null and the sync features simply aren't offered.

Reading the Bookmarks bar as a config

The whole desktop is driven by a config object: desktop icons, categories, the dock. On the website, that config is hand-written. In the extension, it's derived from your Bookmarks bar:

function readBrowserBookmarks() {
  return EXT_API.bookmarks.getTree().then(tree => {
    // Chrome calls it '1', Firefox 'toolbar_____'; newer Chrome also tags it.
    const bar = tree[0].children.find(n =>
      n.folderType === 'bookmarks-bar' || n.id === 'toolbar_____' || n.id === '1');

    // Only links the page can open: skips Firefox `place:` queries, bookmarklets...
    const isLink = n => n.url && /^https?:/i.test(n.url);
    const linksUnder = n => isLink(n) ? [n] : (n.children || []).flatMap(linksUnder);

    // Folders → categories (every link beneath, however deep).
    // Loose links → desktop icons.
    // ...
  });
}
Enter fullscreen mode Exit fullscreen mode

One guard I'm glad I added: if the bar has no links at all, it refuses to sync. Otherwise an empty Bookmarks bar would silently wipe the desktop on every new tab.

Ask for the permission late, and from a click

bookmarks is an optional permission. The extension installs asking for nothing, and only requests access when you turn sync on. Firefox only grants permissions straight from a user gesture, so the permissions.request() call has to come first in the click handler, before any await or confirmation dialog.

Live sync without thrashing

With "Always sync" on, the page listens to the bookmark events:

const BOOKMARK_EVENTS = ['onCreated', 'onRemoved', 'onChanged', 'onMoved',
                         'onChildrenReordered', 'onImportEnded']; // last two: Chrome only
Enter fullscreen mode Exit fullscreen mode

Two lessons:

  • Debounce. Importing bookmarks or deleting a folder fires one event per bookmark. A 300 ms debounce turns that into one sync.
  • Don't redraw if nothing changed. Every new tab runs a sync, and almost always the bar is exactly as you left it. Comparing the state before and after (a JSON.stringify is plenty at this size) means most new tabs do no DOM work at all.

Favicons are harder than they look

A desktop of bookmarks is only as good as its icons, and there's no single reliable source. The lookup goes:

  1. Google's favicon service, at 128 px;
  2. if that fails, backups: icon.horse, the site's own /favicon.ico and /apple-touch-icon.png;
  3. if everything fails, a neutral gray placeholder.

A few details made a real difference:

  • Race the CDNs. Google's service is on t0.gstatic.com, but that's blocked on some networks, where t0.gstatic.cn works. On load, the page pings /generate_204 on both with a no-cors fetch and uses whichever answers first, then remembers it for next time. (An <img> won't work as a ping: an empty 204 counts as an image error.)
  • Detect the fake success. For an unknown site, Google doesn't return an error. It returns a 16×16 generic globe. Any 16 px result from Google is treated as a miss and goes to the backups.
  • Never blow small icons up. A 16 px favicon stretched to a 68 px tile looks awful, so small icons are drawn at their real size on a white tile.
  • Don't remember failures. Icons that failed are only skipped for the current session. If an icon was merely blocked by the network you happened to be on, it gets another try next time instead of staying gray forever.

What I'd tell my past self

  • chrome being defined doesn't mean you're in an extension.
  • Firefox and Chrome disagree on the Bookmarks bar's ID, on which bookmark events exist, and on what a "new window" opens. Test both early.
  • Plain scripts with defer scale further than I expected. For a page this size, I never missed a bundler.
  • Optional permissions plus "read, never write" make the store reviews, and the privacy policy, very short.

If your Bookmarks bar is overflowing too, give it a try: Chrome, Firefox, Edge. The code is on GitHub, CC0, so fork it and make it your own. I'd love to hear what you'd add.

Top comments (0)