repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

opencode/skills/web-development/SKILL.md

Raw

name: web-development description: Cross-browser extension development - API compatibility, polyfills, manifest V3. Use when: building browser extensions for Chrome, Firefox, Safari, Edge. license: MIT

Build browser extensions that work across Chrome, Firefox, Safari, and Edge.

Browser API Namespaces

Browser Namespace Promises Polyfill Needed
Chrome chrome.* Callbacks Yes
Firefox browser.* Native No
Safari browser.* Native No
Edge chrome.* Callbacks Yes

Universal Pattern

import browser from "webextension-polyfill";

const tabs = await browser.tabs.query({ active: true });

Feature Detection

function hasAPI(api: string): boolean {
  const parts = api.split(".");
  let obj: any = typeof browser !== "undefined" ? browser : chrome;
  for (const part of parts) {
    if (obj && typeof obj[part] !== "undefined") {
      obj = obj[part];
    } else {
      return false;
    }
  }
  return true;
}

Manifest V3

Minimal Manifest

{
  "manifest_version": 3,
  "name": "Extension Name",
  "version": "1.0.0",
  "description": "Brief description",
  "action": { "default_popup": "popup.html" },
  "permissions": ["storage"],
  "host_permissions": ["https://api.example.com/*"],
  "background": { "service_worker": "background.js", "type": "module" }
}

Browser-Specific Fields

{
  "browser_specific_settings": {
    "gecko": {
      "id": "extension@example.com",
      "strict_min_version": "109.0"
    }
  }
}

Background Scripts

MV3 Service Worker

// No DOM access - persist state to storage
chrome.runtime.onInstalled.addListener(() => {
  chrome.storage.local.set({ initialized: true });
});

// Use alarms for periodic tasks
chrome.alarms.create("task", { periodInMinutes: 5 });

Cross-Browser Detection

const isServiceWorker = typeof ServiceWorkerGlobalScope !== "undefined" &&
  self instanceof ServiceWorkerGlobalScope;

Permissions

Types

Type Location Purpose
API permissions Browser APIs
Host host_permissions Web content access
Optional optional_permissions Runtime-requested

Host Patterns

Pattern Matches
<all_urls> All URLs
https://*.example.com/* Subdomains
https://example.com/api/* Specific path

Content Scripts

Manifest Declaration

{
  "content_scripts": [{
    "matches": ["https://*.example.com/*"],
    "js": ["content.js"],
    "run_at": "document_idle"
  }]
}

Programmatic Injection

chrome.scripting.executeScript({
  target: { tabId },
  files: ["inject.js"],
});

Network Handling

Declarative Net Request (MV3)

{
  "permissions": ["declarativeNetRequest"],
  "declarative_net_request": {
    "rule_resources": [
      { "id": "ruleset_1", "enabled": true, "path": "rules.json" }
    ]
  }
}
[{
  "id": 1,
  "priority": 1,
  "action": { "type": "block" },
  "condition": { "urlFilter": "||ads.example.com" }
}]

Dynamic Rules

chrome.declarativeNetRequest.updateDynamicRules({
  addRules: [{
    id: 1000,
    priority: 1,
    action: { type: "block" },
    condition: { urlFilter: domain },
  }],
});

Storage

Type Quota Sync
local 10MB No
sync 100KB Yes
session 10MB No
await chrome.storage.local.set({ key: "value" });

MV2 vs MV3

Feature MV2 MV3
Background background.scripts background.service_worker
Remote code Allowed Forbidden
webRequest Supported Use DNR
executeScript Strings allowed Functions only

Migration Checklist


Common Issues

Storage Quota

Browser Local Sync
Chrome 10MB 100KB
Firefox Unlimited 100KB
Safari 10MB 100KB

webRequest Blocking

Safari doesn't support blocking. Use declarativeNetRequest:

await browser.declarativeNetRequest.updateDynamicRules({
  addRules: [{
    id: 1,
    action: { type: "block" },
    condition: { urlFilter: "*://ads.example.com/*" },
  }],
});
Markdown source
File outline
---
name: web-development
description: Cross-browser extension development - API compatibility, polyfills, manifest V3. Use when: building browser extensions for Chrome, Firefox, Safari, Edge.
license: MIT
---

Build browser extensions that work across Chrome, Firefox, Safari, and Edge.

## Browser API Namespaces

| Browser | Namespace   | Promises  | Polyfill Needed |
| ------- | ----------- | --------- | --------------- |
| Chrome  | `chrome.*`  | Callbacks | Yes             |
| Firefox | `browser.*` | Native    | No              |
| Safari  | `browser.*` | Native    | No              |
| Edge    | `chrome.*`  | Callbacks | Yes             |

## Universal Pattern

```typescript
import browser from "webextension-polyfill";

const tabs = await browser.tabs.query({ active: true });
```

## Feature Detection

```typescript
function hasAPI(api: string): boolean {
  const parts = api.split(".");
  let obj: any = typeof browser !== "undefined" ? browser : chrome;
  for (const part of parts) {
    if (obj && typeof obj[part] !== "undefined") {
      obj = obj[part];
    } else {
      return false;
    }
  }
  return true;
}
```

---

## Manifest V3

### Minimal Manifest

```json
{
  "manifest_version": 3,
  "name": "Extension Name",
  "version": "1.0.0",
  "description": "Brief description",
  "action": { "default_popup": "popup.html" },
  "permissions": ["storage"],
  "host_permissions": ["https://api.example.com/*"],
  "background": { "service_worker": "background.js", "type": "module" }
}
```

### Browser-Specific Fields

```json
{
  "browser_specific_settings": {
    "gecko": {
      "id": "extension@example.com",
      "strict_min_version": "109.0"
    }
  }
}
```

---

## Background Scripts

### MV3 Service Worker

```typescript
// No DOM access - persist state to storage
chrome.runtime.onInstalled.addListener(() => {
  chrome.storage.local.set({ initialized: true });
});

// Use alarms for periodic tasks
chrome.alarms.create("task", { periodInMinutes: 5 });
```

### Cross-Browser Detection

```typescript
const isServiceWorker = typeof ServiceWorkerGlobalScope !== "undefined" &&
  self instanceof ServiceWorkerGlobalScope;
```

---

## Permissions

### Types

| Type     | Location               | Purpose            |
| -------- | ---------------------- | ------------------ |
| API      | `permissions`          | Browser APIs       |
| Host     | `host_permissions`     | Web content access |
| Optional | `optional_permissions` | Runtime-requested  |

### Host Patterns

| Pattern                     | Matches       |
| --------------------------- | ------------- |
| `<all_urls>`                | All URLs      |
| `https://*.example.com/*`   | Subdomains    |
| `https://example.com/api/*` | Specific path |

---

## Content Scripts

### Manifest Declaration

```json
{
  "content_scripts": [{
    "matches": ["https://*.example.com/*"],
    "js": ["content.js"],
    "run_at": "document_idle"
  }]
}
```

### Programmatic Injection

```typescript
chrome.scripting.executeScript({
  target: { tabId },
  files: ["inject.js"],
});
```

---

## Network Handling

### Declarative Net Request (MV3)

```json
{
  "permissions": ["declarativeNetRequest"],
  "declarative_net_request": {
    "rule_resources": [
      { "id": "ruleset_1", "enabled": true, "path": "rules.json" }
    ]
  }
}
```

```json
[{
  "id": 1,
  "priority": 1,
  "action": { "type": "block" },
  "condition": { "urlFilter": "||ads.example.com" }
}]
```

### Dynamic Rules

```typescript
chrome.declarativeNetRequest.updateDynamicRules({
  addRules: [{
    id: 1000,
    priority: 1,
    action: { type: "block" },
    condition: { urlFilter: domain },
  }],
});
```

---

## Storage

| Type    | Quota | Sync |
| ------- | ----- | ---- |
| local   | 10MB  | No   |
| sync    | 100KB | Yes  |
| session | 10MB  | No   |

```typescript
await chrome.storage.local.set({ key: "value" });
```

---

## MV2 vs MV3

| Feature       | MV2                  | MV3                         |
| ------------- | -------------------- | --------------------------- |
| Background    | `background.scripts` | `background.service_worker` |
| Remote code   | Allowed              | Forbidden                   |
| webRequest    | Supported            | Use DNR                     |
| executeScript | Strings allowed      | Functions only              |

---

## Migration Checklist

- [ ] Update `manifest_version` to 3
- [ ] Convert background to service worker
- [ ] Move host_permissions from permissions
- [ ] Replace browser_action with action
- [ ] Remove remote code
- [ ] Replace webRequest with declarativeNetRequest

---

## Common Issues

### Storage Quota

| Browser | Local     | Sync  |
| ------- | --------- | ----- |
| Chrome  | 10MB      | 100KB |
| Firefox | Unlimited | 100KB |
| Safari  | 10MB      | 100KB |

### webRequest Blocking

Safari doesn't support blocking. Use declarativeNetRequest:

```typescript
await browser.declarativeNetRequest.updateDynamicRules({
  addRules: [{
    id: 1,
    action: { type: "block" },
    condition: { urlFilter: "*://ads.example.com/*" },
  }],
});
```