Skip to content

Commit 152a88c

Browse files
Merge pull request #43 from p2plabsxyz/theme-protocol
feat(theme-handler): implement browser://theme protocol handler
2 parents d389ab8 + e2d2370 commit 152a88c

27 files changed

+767
-197
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
- [x] Reload
2525
- [x] Browser protocol (peersky://)
2626
- [x] Home page (peersky://home)
27+
- [x] Cross browser themeing ([browser://theme/](https://github.com/p2plabsxyz/peersky-browser/blob/main/docs/Theme.md))
2728
- [x] Search engine
2829
- DuckDuckGo (default)
2930
- Ecosia
30-
- [ ] [Tabs?](https://github.com/p2plabsxyz/peersky-browser/issues/11)
31+
- [ ] [Tabs](https://github.com/p2plabsxyz/peersky-browser/issues/11)
3132

3233
- [x] IPFS protocol handler:
3334

@@ -104,7 +105,7 @@
104105
- [ ] Change themes
105106
- [ ] Clear browser cache
106107

107-
- [ ] Web extensions:
108+
- [ ] [Web extensions](https://github.com/p2plabsxyz/peersky-browser/issues/19):
108109
- [ ] Ability to add and manage extensions
109110
- [ ] Default extensions
110111
- [Ad-blocker](https://github.com/gorhill/uBlock)

docs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Peersky Browser Documentation
2+
3+
- [Theme Protocol](Theme.md)

docs/Theme.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Theme Protocol (`browser://theme/`)
2+
3+
## Overview
4+
5+
The `browser://theme/` protocol provides a standardized way for web applications to access browser-level CSS styles and theme variables in [Peersky](https://peersky.p2plabs.xyz/) and other compatible browsers, such as [Agregore](https://agregore.mauve.moe/). This protocol ensures consistent theming across different browsers by serving CSS files with a common set of variables. It allows developers to build applications that adapt to the browser's theme without needing browser-specific code.
6+
7+
![DWeb Scratchpad in Peersky and Agregore](./images/browser-theme-protocol-example.png)
8+
9+
## Purpose
10+
11+
The goal of the `browser://theme/` protocol is to:
12+
13+
- Enable cross-browser compatibility for theming in any browser, including p2p browsers like Peersky and Agregore.
14+
- Provide a unified set of theme variables using standardized `--browser-theme-` prefixes.
15+
- Allow web applications to import styles or variables without hardcoding browser-specific protocols (e.g., `peersky://` or `agregore://`).
16+
17+
## Implementation
18+
19+
### Protocol Handler
20+
21+
The `browser://theme/` protocol is implemented in Peersky via a custom Electron protocol handler (`theme-handler.js`). It serves CSS files from the `src/pages/theme/` directory when requests are made to URLs like `browser://theme/vars.css` or `browser://theme/style.css`.
22+
23+
- **Location**: Files are stored in `src/pages/theme/` (e.g., `vars.css`, `style.css`, `base.css`, `index.css`).
24+
- **URL Structure**: Requests to `browser://theme/<filename>` map to `src/pages/theme/<filename>`.
25+
- **Example**: `browser://theme/vars.css` serves `src/pages/theme/vars.css`.
26+
27+
### Theme Variable Standardization
28+
29+
The `browser://theme/` protocol provides standardized theme variables prefixed with `--browser-theme-`, such as `--browser-theme-font-family`, `--browser-theme-background`, `--browser-theme-text-color`, `--browser-theme-primary-highlight`, and `--browser-theme-secondary-highlight`. These variables allow web applications to adapt to the host browser's theme without needing browser-specific code.
30+
31+
Each browser implements these standardized variables by mapping them to their internal theme variables. For example:
32+
33+
- In Peersky, `--browser-theme-background` is mapped to `--base01`, which is part of the Base16 color palette [Base16 Framework](https://github.com/chriskempson/base16).
34+
- In Agregore, `--browser-theme-background` is mapped to `--ag-theme-background`, which is defined in Agregore's theme configuration.
35+
36+
This ensures that applications built for one browser can work seamlessly in another, as long as they use the standardized `--browser-theme-` variables.
37+
38+
### Cross-Browser Compatibility
39+
40+
The `browser://theme/` protocol enables apps built for one browser to work seamlessly in another by providing standardized theme variables prefixed with `--browser-theme-`. These variables are mapped to each browser's internal theme variables, ensuring consistent theming across different browsers.
41+
42+
For example:
43+
44+
- In Peersky, `--browser-theme-background` is mapped to `--base01`, which is part of the Base16 color palette.
45+
- In Agregore, `--browser-theme-background` is mapped to `--ag-theme-background`, which is defined in Agregore's theme configuration.
46+
47+
As a result, an app using `--browser-theme-background` will render with the appropriate background color for each browser, whether it's based on Base16 (as in Peersky) or another theme system (as in Agregore).
48+
49+
Additionally, apps can use the full set of variables provided by each browser for more advanced theming, but for cross-browser compatibility, it's recommended to use the standardized `--browser-theme-` variables.
50+
51+
## Usage
52+
53+
### Importing Theme Styles
54+
55+
Web applications can import theme styles or variables using `<style>` tags or `<link>` elements. Examples:
56+
57+
- **Import Variables**:
58+
59+
```html
60+
<style>
61+
@import url("browser://theme/vars.css");
62+
body {
63+
background-color: var(--browser-theme-background);
64+
color: var(--browser-theme-text-color);
65+
}
66+
</style>
67+
```
68+
69+
- **Import Default Styles**:
70+
71+
```html
72+
<link rel="stylesheet" href="browser://theme/style.css" />
73+
```
74+
75+
- **Use Browser-Specific Variables** (for Agregore apps in Peersky):
76+
```html
77+
<style>
78+
@import url("browser://theme/vars.css");
79+
body {
80+
background-color: var(
81+
--ag-theme-background
82+
); /* Maps to --browser-theme-background */
83+
}
84+
</style>
85+
```
86+
87+
## Theme Files (`browser://theme/`)
88+
89+
- `vars.css`: Defines standardized `--browser-theme-`, Base16, and Peersky-specific CSS variables for theming.
90+
- `base.css`: Applies minimal default styles for unstyled pages, auto-injected by preload.
91+
- `style.css`: Opt-in comprehensive styling for web apps
92+
- `index.css`: Styles Peersky’s browser UI (e.g., navigation bar, URL input).
93+
- `home.css`: Styles Peersky’s home page with a background image and sidebar.
144 KB
Loading

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "peersky-browser",
3-
"version": "1.0.0-beta.3",
3+
"version": "1.0.0-beta.4",
44
"description": "A minimal local-first p2p web browser: access, communicate, and publish offline.",
55
"keywords": [
66
"peersky",
@@ -152,6 +152,7 @@
152152
"mime-types": "^2.1.35",
153153
"multiformats": "^13.3.2",
154154
"node-cache": "^5.1.2",
155+
"scoped-fs": "^1.4.1",
155156
"web3protocol": "^0.6.0"
156157
},
157158
"devDependencies": {

src/main.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { app, session, protocol as globalProtocol } from "electron";
2+
import { createHandler as createBrowserHandler } from "./protocols/peersky-protocol.js";
3+
import { createHandler as createBrowserThemeHandler } from "./protocols/theme-handler.js";
24
import { createHandler as createIPFSHandler } from "./protocols/ipfs-handler.js";
3-
import { createHandler as createBrowserHandler } from "./protocols/browser-protocol.js";
45
import { createHandler as createHyperHandler } from "./protocols/hyper-handler.js";
56
import { createHandler as createWeb3Handler } from "./protocols/web3-handler.js";
67
import { ipfsOptions, hyperOptions } from "./protocols/config.js";
@@ -31,12 +32,13 @@ const BROWSER_PROTOCOL = {
3132
let windowManager;
3233

3334
globalProtocol.registerSchemesAsPrivileged([
35+
{ scheme: "peersky", privileges: BROWSER_PROTOCOL },
36+
{ scheme: "browser", privileges: BROWSER_PROTOCOL },
3437
{ scheme: "ipfs", privileges: P2P_PROTOCOL },
3538
{ scheme: "ipns", privileges: P2P_PROTOCOL },
3639
{ scheme: "pubsub", privileges: P2P_PROTOCOL },
3740
{ scheme: "hyper", privileges: P2P_PROTOCOL },
3841
{ scheme: "web3", privileges: P2P_PROTOCOL },
39-
{ scheme: "peersky", privileges: BROWSER_PROTOCOL },
4042
]);
4143

4244
app.whenReady().then(async () => {
@@ -93,11 +95,18 @@ app.on("before-quit", (event) => {
9395
async function setupProtocols(session) {
9496
const { protocol: sessionProtocol } = session;
9597

98+
app.setAsDefaultProtocolClient("peersky");
99+
app.setAsDefaultProtocolClient("browser");
96100
app.setAsDefaultProtocolClient("ipfs");
97101
app.setAsDefaultProtocolClient("ipns");
98102
app.setAsDefaultProtocolClient("hyper");
99103
app.setAsDefaultProtocolClient("web3");
100-
app.setAsDefaultProtocolClient("peersky");
104+
105+
const browserProtocolHandler = await createBrowserHandler();
106+
sessionProtocol.registerStreamProtocol("peersky", browserProtocolHandler, BROWSER_PROTOCOL);
107+
108+
const browserThemeHandler = await createBrowserThemeHandler();
109+
sessionProtocol.registerStreamProtocol("browser", browserThemeHandler, BROWSER_PROTOCOL);
101110

102111
const ipfsProtocolHandler = await createIPFSHandler(ipfsOptions, session);
103112
sessionProtocol.registerStreamProtocol("ipfs", ipfsProtocolHandler, P2P_PROTOCOL);
@@ -109,9 +118,6 @@ async function setupProtocols(session) {
109118

110119
const web3ProtocolHandler = await createWeb3Handler();
111120
sessionProtocol.registerStreamProtocol("web3", web3ProtocolHandler, P2P_PROTOCOL);
112-
113-
const browserProtocolHandler = await createBrowserHandler();
114-
sessionProtocol.registerStreamProtocol("peersky", browserProtocolHandler, BROWSER_PROTOCOL);
115121
}
116122

117123
app.on("window-all-closed", () => {

src/pages/404.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@
33
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
<title>Peersky Browser | 404</title>
6+
<style>
7+
@import url("browser://theme/vars.css");
8+
9+
body {
10+
background-color: var(--browser-theme-background);
11+
color: var(--browser-theme-text-color);
12+
text-align: center;
13+
}
14+
</style>
615
<p>404 Page not found!</p>

src/pages/home.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
<style>
6-
@import url("peersky://static/css/home.css");
7-
@import url("peersky://static/css/index.css");
6+
@import url("browser://theme/home.css");
7+
@import url("browser://theme/index.css");
88
</style>
99
<title>Home</title>
1010
<div class="home-background">

src/pages/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
<style>
6-
@import url("peersky://static/css/index.css");
6+
@import url("browser://theme/index.css");
77
</style>
88
<title>Peersky Browser</title>
99
<nav-box id="navbox"></nav-box>

0 commit comments

Comments
 (0)