@directo/adunit 0.0.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +228 -0
- package/dist/background.d.ts +10 -0
- package/dist/cjs/background.js +1 -0
- package/dist/cjs/content.js +2 -0
- package/dist/cjs/content.js.LICENSE.txt +55 -0
- package/dist/content.d.ts +5 -0
- package/dist/directo-global/background.js +1 -0
- package/dist/directo-global/content.js +2 -0
- package/dist/directo-global/content.js.LICENSE.txt +55 -0
- package/dist/esm/background.mjs +1 -0
- package/dist/esm/content.mjs +2 -0
- package/dist/esm/content.mjs.LICENSE.txt +55 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# Directo adUnit Integration Guide
|
|
2
|
+
|
|
3
|
+
<img alt="version" src="https://img.shields.io/badge/version-0.0.0-blue?style=flat" />
|
|
4
|
+
<a href="https://getdirecto.com" target="_blank">
|
|
5
|
+
<img alt="getdirecto.com" src="https://img.shields.io/badge/getdirecto-join-brightgreen?style=flat" />
|
|
6
|
+
</a>
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
This guide provides comprehensive instructions for integrating the **Directo adUnit Library** into your browser extension projects. It covers installation, background/content setup, required permissions, message filtering, API usage, and troubleshooting.
|
|
11
|
+
|
|
12
|
+
## Installation Methods
|
|
13
|
+
|
|
14
|
+
### Method 1: NPM Installation
|
|
15
|
+
|
|
16
|
+
1. Install the package from npm:
|
|
17
|
+
|
|
18
|
+
```shell
|
|
19
|
+
npm install @directo/adunit
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
2. Import the library in your code:
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
// In background scripts
|
|
26
|
+
import { initializeAdUnit, isAdUnitMessage } from '@directo/adunit/background';
|
|
27
|
+
|
|
28
|
+
// In content scripts
|
|
29
|
+
import { initializeAdUnit } from '@directo/adunit/content';
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
3. Use your bundler of choice (Vite, webpack, Rollup, esbuild) to build your extension.
|
|
33
|
+
|
|
34
|
+
**TypeScript Compatibility Note:** This package is bundled with the ES2022 library and targets ES6. Ensure your project's TypeScript configuration (`tsconfig.json`) is compatible with these settings for optimal type support and compatibility.
|
|
35
|
+
|
|
36
|
+
@todo
|
|
37
|
+
### Method 2: Direct Script Inclusion
|
|
38
|
+
|
|
39
|
+
1. Obtain the Directo library JS builds from the `directo-global` folder:
|
|
40
|
+
|
|
41
|
+
* directo-global/background.js – for background/service worker
|
|
42
|
+
|
|
43
|
+
* directo-global/content.js – for content scripts
|
|
44
|
+
|
|
45
|
+
2. Place these files in your extension (e.g., vendor/).
|
|
46
|
+
|
|
47
|
+
3. Reference them in manifest.json and your entry points:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"background": {
|
|
52
|
+
"service_worker": "background.js"
|
|
53
|
+
},
|
|
54
|
+
"content_scripts": [
|
|
55
|
+
{
|
|
56
|
+
"matches": ["<all_urls>"],
|
|
57
|
+
"js": [
|
|
58
|
+
"vendor/directo-global/content.js",
|
|
59
|
+
"content.js"
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Implementation Guide
|
|
67
|
+
|
|
68
|
+
### Background Script
|
|
69
|
+
|
|
70
|
+
After installing via NPM or including the JS file:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
// For JS inclusion only (Method 2):
|
|
74
|
+
// self.importScripts('vendor/directo-global/background.js');
|
|
75
|
+
// this.DirectoAdUnit.initializeAdUnit('YOUR_PARTNER_API_KEY');
|
|
76
|
+
|
|
77
|
+
// Using an IIFE for error handling isolation in the service worker context
|
|
78
|
+
(() => {
|
|
79
|
+
try {
|
|
80
|
+
// Initialize the Directo adUnit with your partner API key
|
|
81
|
+
initializeAdUnit('YOUR_PARTNER_API_KEY');
|
|
82
|
+
console.log('Directo background initialized successfully');
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.error('Failed to initialize Directo:', err);
|
|
85
|
+
// Optional: fallback logic
|
|
86
|
+
}
|
|
87
|
+
})();
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The background process bootstraps the Directo runtime, sets up listeners, and coordinates content activity.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
### Content Script
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
async function initializeContent() {
|
|
97
|
+
if (document.body) {
|
|
98
|
+
try {
|
|
99
|
+
console.log('Attempting to initialize Directo AdUnit');
|
|
100
|
+
await initializeAdUnit();
|
|
101
|
+
console.log('Directo AdUnit initialized successfully');
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error('Error initializing Directo AdUnit:', error);
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
console.warn('Document body not available yet');
|
|
107
|
+
setTimeout(initializeContent, 50);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (document.readyState === 'loading') {
|
|
112
|
+
console.log('Document still loading, waiting for DOMContentLoaded');
|
|
113
|
+
document.addEventListener('DOMContentLoaded', initializeContent);
|
|
114
|
+
} else {
|
|
115
|
+
console.log('Document already loaded, running initialization immediately');
|
|
116
|
+
initializeContent();
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Initialize the AdUnit early, once the DOM is ready, to ensure timely UI/display behavior.
|
|
121
|
+
|
|
122
|
+
## Required Permissions
|
|
123
|
+
|
|
124
|
+
**Exception requested:** the required extension permissions for this Directo integration are **storage** and **unlimitedStorage**
|
|
125
|
+
|
|
126
|
+
```json
|
|
127
|
+
{
|
|
128
|
+
"permissions": [
|
|
129
|
+
"storage",
|
|
130
|
+
"unlimitedStorage"
|
|
131
|
+
],
|
|
132
|
+
"host_permissions": [ "<all_urls>" ]
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**Permission explanation**
|
|
137
|
+
|
|
138
|
+
* storage: persists configuration and user preferences used by the AdUnit/runtime.
|
|
139
|
+
* unlimitedStorage (optional but recommended): if your combined extension \+ Directo data may exceed 10 MB; expect several MB used by cached rules/assets.
|
|
140
|
+
|
|
141
|
+
Ensure your injection strategy (e.g., content scripts declared in manifest.json) aligns with this permissions model.
|
|
142
|
+
|
|
143
|
+
## Critical Integration Requirements
|
|
144
|
+
|
|
145
|
+
### Auto-Rejecting Directo Messages
|
|
146
|
+
|
|
147
|
+
⚠️ **IMPORTANT:** Your extension **must** short-circuit Directo internal messages in your message listener to avoid conflicts with your own app messages.
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
// If using the package:
|
|
151
|
+
import { isAdUnitMessage } from '@directo/adunit/background';
|
|
152
|
+
|
|
153
|
+
// Example message listener (service worker / background)
|
|
154
|
+
chrome.runtime.onMessage.addListener((data, sender, sendResponse) => {
|
|
155
|
+
// Always gate Directo messages first:
|
|
156
|
+
if (isAdUnitMessage(data)) {
|
|
157
|
+
return; // Ignore: handled internally by Directo
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Your extension's message handling below...
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Why this is required
|
|
165
|
+
|
|
166
|
+
Without this guard:
|
|
167
|
+
|
|
168
|
+
1. Your extension might process Directo’s internal messages.
|
|
169
|
+
|
|
170
|
+
2. Directo functionality could break or behave unpredictably.
|
|
171
|
+
|
|
172
|
+
3. You may see console errors or crashes stemming from message handling.
|
|
173
|
+
|
|
174
|
+
## API Reference
|
|
175
|
+
|
|
176
|
+
### Background Script API
|
|
177
|
+
|
|
178
|
+
```javascript
|
|
179
|
+
initializeAdUnit('YOUR_PARTNER_API_KEY');
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Content Script API
|
|
183
|
+
|
|
184
|
+
```javascript
|
|
185
|
+
// Sets up the Directo AdUnit and observers
|
|
186
|
+
initializeAdUnit();
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Implement robust try/catch (or .catch) around initialization to surface failures and enact fallbacks. Both functions return promises that should be fire-and-forget, however we encourage handling initialization errors.
|
|
190
|
+
|
|
191
|
+
## Error Handling
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
initializeAdUnit('YOUR_PARTNER_KEY')
|
|
195
|
+
.then(() => {
|
|
196
|
+
console.log('Directo background initialized successfully');
|
|
197
|
+
})
|
|
198
|
+
.catch((error) => {
|
|
199
|
+
console.error('Failed to initialize Directo:', error);
|
|
200
|
+
// Add any cleanup or degraded-mode logic here
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Testing Your Integration
|
|
205
|
+
|
|
206
|
+
1. Confirm the background script initializes without errors (Service Worker logs).
|
|
207
|
+
|
|
208
|
+
2. Navigate to pages where the AdUnit should activate and verify expected UI/behavior.
|
|
209
|
+
|
|
210
|
+
## Troubleshooting
|
|
211
|
+
|
|
212
|
+
### Common issues
|
|
213
|
+
|
|
214
|
+
* **Initialization Failures**: Usually invalid/absent API key or network errors.
|
|
215
|
+
|
|
216
|
+
* **Missing AdUnit UI**: Ensure content scripts are injected as expected (manifest matches) and that DOM is ready before calling initializeAdUnit().
|
|
217
|
+
|
|
218
|
+
* **Console Errors**: Inspect logs from both background and content scripts.
|
|
219
|
+
|
|
220
|
+
* **Minimum Version**: Confirm compatibility with the minimum Directo library version intended for this integration.
|
|
221
|
+
|
|
222
|
+
## Support
|
|
223
|
+
|
|
224
|
+
For technical support or integration questions, contact julian@getdirecto.com.
|
|
225
|
+
|
|
226
|
+
## License
|
|
227
|
+
|
|
228
|
+
Proprietary - Directo Tech, Inc.
|