Skip to content

Commit bc69ed4

Browse files
committed
update md
1 parent 3e342bc commit bc69ed4

File tree

2 files changed

+769
-25
lines changed

2 files changed

+769
-25
lines changed

CLAUDE.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Overview
6+
7+
Useful Scripts is a Chrome extension that provides a collection of utility scripts for various websites and tasks. It includes scripts for Facebook, Instagram, YouTube, TikTok, Google Drive, and many other platforms. The extension allows users to run scripts on-demand or automatically based on URL patterns.
8+
9+
## Architecture
10+
11+
### Script Execution Contexts
12+
13+
The extension uses **four distinct execution contexts** for scripts, each with different capabilities and restrictions:
14+
15+
1. **popupScript**: Runs in extension popup context
16+
- Can use Chrome Extension APIs
17+
- Can inject scripts into active tab via `chrome.scripting.executeScript`
18+
- Cannot access page DOM directly
19+
- Used for user-triggered actions from the popup
20+
21+
2. **contentScript**: Runs in ISOLATED/SANDBOX world
22+
- Can use limited Chrome Extension APIs
23+
- Can access/modify DOM
24+
- **Cannot** access page JavaScript variables (different world)
25+
- Can communicate with background script via `chrome.runtime.sendMessage`
26+
27+
3. **pageScript**: Runs in webpage's MAIN world
28+
- **Cannot** use Chrome Extension APIs
29+
- Can access/modify DOM and page JavaScript variables
30+
- Can override default page behaviors
31+
- Communicates with contentScript via `window.postMessage`
32+
33+
4. **backgroundScript**: Runs as service worker
34+
- Can use full Chrome Extension APIs
35+
- **Cannot** use dynamic imports (use GLOBAL variables instead)
36+
- Handles events like `onBeforeRequest`, `tabs.onUpdated`, etc.
37+
- Context is the GLOBAL variable in `background_script.js`
38+
39+
### Communication Between Contexts
40+
41+
- **popupScript ↔ backgroundScript**: `chrome.runtime.sendMessage`
42+
- **contentScript ↔ backgroundScript**: `chrome.runtime.sendMessage`
43+
- **pageScript ↔ contentScript**: `window.postMessage`
44+
- **pageScript → backgroundScript**: `UfsGlobal.Extension.runInBackground`
45+
46+
### Directory Structure
47+
48+
- **`scripts/`**: All script functionality files
49+
- **`@index.js`**: Exports all scripts (required for new scripts)
50+
- **`@allScripts.js`**: Generated list of all scripts
51+
- **`background-scripts/background_script.js`**: Service worker entry point
52+
- **`content-scripts/`**: Content script infrastructure
53+
- **`helpers/`**: Shared utility functions
54+
- Individual script files (e.g., `fb_toggleLight.js`, `youtube_downloadVideo.js`)
55+
56+
- **`popup/`**: Extension popup UI
57+
- **`tabs.js`**: Defines script categories and organization (required for new scripts)
58+
- **`helpers/`**: UI helper modules (category, lang, modal, storage, theme, utils)
59+
- **`index.js`**: Main popup logic
60+
- **`main.js`**: Entry point
61+
62+
- **`pages/`**: Supporting pages (view script source, settings)
63+
64+
- **`templates/`**: Script templates
65+
- **`simple.js`**: Minimal script template
66+
- **`full.js`**: Complete template with all available options and documentation
67+
68+
## Adding a New Script
69+
70+
1. **Create script file** in `scripts/` directory with descriptive name (e.g., `platform_feature.js`)
71+
72+
2. **Use template**: Copy from `templates/simple.js` or `templates/full.js`
73+
74+
3. **Script structure** (minimal):
75+
```javascript
76+
export default {
77+
icon: '<i class="fa-solid fa-icon"></i>',
78+
name: {
79+
en: "English name",
80+
vi: "Vietnamese name",
81+
},
82+
description: {
83+
en: "English description",
84+
vi: "Vietnamese description",
85+
},
86+
87+
// URL filtering
88+
whiteList: ["https://*.example.com/*"], // Only run on these URLs
89+
blackList: [], // Don't run on these URLs
90+
91+
// Choose one or more contexts
92+
popupScript: {
93+
onClick: () => { /* code */ },
94+
},
95+
96+
contentScript: {
97+
onClick: () => { /* code */ },
98+
onDocumentIdle: () => { /* code */ },
99+
},
100+
101+
pageScript: {
102+
onClick: () => { /* code */ },
103+
onDocumentIdle: () => { /* code */ },
104+
},
105+
};
106+
```
107+
108+
4. **Import in `scripts/@index.js`**:
109+
```javascript
110+
export { default as scriptName } from "./scriptName.js";
111+
```
112+
113+
5. **Add to category in `popup/tabs.js`**:
114+
```javascript
115+
const tabs = [
116+
{
117+
...CATEGORY.categoryName,
118+
scripts: [
119+
s.scriptName, // Add your script here
120+
],
121+
},
122+
];
123+
```
124+
125+
6. **Test** by opening the extension popup and running the script
126+
127+
## Script Lifecycle Events
128+
129+
- **onDocumentStart**: Runs as early as possible (before DOM is fully loaded)
130+
- **onDocumentIdle**: Runs when DOM is ready (recommended for most scripts)
131+
- **onDocumentEnd**: Runs when page is fully loaded
132+
- **onClick**: Runs when user clicks the script in popup
133+
134+
To run in all frames (including iframes), append `_` to function name: `onDocumentIdle_()`, `onClick_()`, etc.
135+
136+
## Common Patterns
137+
138+
### Using UfsGlobal
139+
140+
UfsGlobal provides shared utilities across contexts:
141+
142+
```javascript
143+
import { UfsGlobal } from "./content-scripts/ufs_global.js";
144+
145+
// Wait for elements
146+
UfsGlobal.DOM.onElementsAdded('selector', (element) => {
147+
// Do something with element
148+
});
149+
150+
// Run in background context
151+
UfsGlobal.Extension.runInBackground({
152+
fnPath: "functionName",
153+
params: [arg1, arg2]
154+
});
155+
```
156+
157+
### Download Files
158+
159+
Use Chrome's download API in popupScript or backgroundScript:
160+
161+
```javascript
162+
chrome.downloads.download({
163+
url: fileUrl,
164+
filename: "myfile.ext"
165+
});
166+
```
167+
168+
### Inject Code into Page
169+
170+
From popupScript, use utilities:
171+
172+
```javascript
173+
await utils.runScriptInCurrentTab(() => {
174+
// This code runs in page context
175+
console.log(window.location.href);
176+
});
177+
```
178+
179+
## Important Notes
180+
181+
- **No package.json**: This is a vanilla JavaScript Chrome extension without build tools
182+
- **ES6 modules**: Use `import`/`export` for code organization
183+
- **Icon library**: Uses Font Awesome for script icons
184+
- **Localization**: Support both English (`en`) and Vietnamese (`vi`)
185+
- **Trusted Types**: Scripts that inject HTML must comply with Chrome's Trusted Types policy
186+
- **Linux compatibility**: Use relative paths and proper casing for file names
187+
- **Auto-run capability**: Scripts with `onDocumentStart`/`Idle`/`End` can be enabled for automatic execution
188+
189+
## Development Workflow
190+
191+
1. Load extension in Chrome via `chrome://extensions/` (Developer mode → Load unpacked)
192+
2. Make changes to script files
193+
3. Click refresh icon in `chrome://extensions/` to reload extension
194+
4. Test changes in target websites
195+
5. Check console for errors in both page context and extension context (inspect popup)
196+
197+
## Common Script Categories
198+
199+
- **CATEGORY.facebook**: Facebook-related utilities (download, reveal messages, toggle UI)
200+
- **CATEGORY.youtube**: YouTube tools (download, captions, PiP)
201+
- **CATEGORY.download**: General download utilities
202+
- **CATEGORY.automation**: Automation and productivity scripts
203+
- **CATEGORY.unlock**: Bypass paywalls and restrictions
204+
- **CATEGORY.webUI**: UI manipulation scripts
205+
206+
## Testing
207+
208+
- No automated test suite currently
209+
- Manual testing workflow:
210+
1. Install extension locally
211+
2. Navigate to target website
212+
3. Open extension popup and run script
213+
4. Verify expected behavior
214+
5. Check browser console for errors
215+
216+
## External Resources
217+
218+
- Demo site: https://useful-scripts-extension.github.io/useful-script/popup/popup.html
219+
- Facebook Group: https://www.facebook.com/groups/1154059318582088
220+
- Developer tutorials: YouTube playlist in README

0 commit comments

Comments
 (0)