@adviverse/desktop 0.0.0-stage → 0.1.1
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 +125 -2
- package/dist/core.d.ts +67 -0
- package/dist/core.js +155 -0
- package/dist/electron.d.ts +9 -0
- package/dist/electron.js +45 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +2 -0
- package/dist/preload.d.ts +1 -0
- package/dist/preload.js +6 -0
- package/dist/react.d.ts +10 -0
- package/dist/react.js +52 -0
- package/dist/vanilla.d.ts +15 -0
- package/dist/vanilla.js +72 -0
- package/package.json +33 -4
package/README.md
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @adviverse/desktop
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Adviverse ads for **native desktop apps that host a webview** — Electron, Tauri,
|
|
4
|
+
CEF, WKWebView, WebView2. It runs the same loop as the terminal/mobile SDKs:
|
|
5
|
+
**fetch → cache → render → viewability → impression → click**, with the ad
|
|
6
|
+
rendered right in your app's UI. On no-fill it renders nothing.
|
|
7
|
+
|
|
8
|
+
For a **pure-native** app (Qt/Cocoa/WinUI, no webview), use the native-render
|
|
9
|
+
core instead: [`@adviverse/desktop-native`](../desktop-native) (C ABI).
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @adviverse/desktop
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Get a **serve token** (`ts_…`) from your Adviverse dashboard (Publisher →
|
|
18
|
+
Placements → terminal/SDK token). `ak_…` API keys and publisher placement tags
|
|
19
|
+
also work.
|
|
20
|
+
|
|
21
|
+
## Renderer — React
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { AdviverseAd } from '@adviverse/desktop/react';
|
|
25
|
+
|
|
26
|
+
// A disclosed ad card. Renders nothing on no-fill; rotates if you pass refreshMs.
|
|
27
|
+
<AdviverseAd token="ts_yourtoken" refreshMs={60000} />
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Bring your own markup with a render prop:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
<AdviverseAd token="ts_yourtoken">
|
|
34
|
+
{(ad, onClick) => (
|
|
35
|
+
<button onClick={onClick}>{ad.label} · {ad.text} {ad.cta && `— ${ad.cta}`}</button>
|
|
36
|
+
)}
|
|
37
|
+
</AdviverseAd>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Renderer — no framework
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { mountAd } from '@adviverse/desktop';
|
|
44
|
+
|
|
45
|
+
const dispose = mountAd(document.getElementById('ad')!, {
|
|
46
|
+
token: 'ts_yourtoken',
|
|
47
|
+
refreshMs: 60000,
|
|
48
|
+
});
|
|
49
|
+
// later: dispose();
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Or drive it yourself:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { fetchAd, openClick, observeViewability } from '@adviverse/desktop';
|
|
56
|
+
|
|
57
|
+
const ad = await fetchAd({ token: 'ts_yourtoken' });
|
|
58
|
+
if (ad) {
|
|
59
|
+
const el = renderYourCard(ad); // your DOM
|
|
60
|
+
observeViewability(el, ad); // fires the impression when truly visible
|
|
61
|
+
el.onclick = () => openClick(ad); // opens the landing page in the default browser
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Opening clicks in the system browser
|
|
66
|
+
|
|
67
|
+
A display-ad click must open the **default browser**, not navigate your app
|
|
68
|
+
window. `openClick()` auto-detects the host:
|
|
69
|
+
|
|
70
|
+
- **Tauri** — uses the shell-open plugin (add `@tauri-apps/plugin-shell` and
|
|
71
|
+
allow `shell:open`).
|
|
72
|
+
- **Electron** — expose a bridge from the main process (below).
|
|
73
|
+
- **Fallback** — `window.open(url, '_blank')`.
|
|
74
|
+
|
|
75
|
+
### Electron bridge (one-time)
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
// main.ts
|
|
79
|
+
import { app, BrowserWindow } from 'electron';
|
|
80
|
+
import { registerAdviverseIpc, adviversePreloadPath } from '@adviverse/desktop/electron';
|
|
81
|
+
|
|
82
|
+
app.whenReady().then(() => {
|
|
83
|
+
registerAdviverseIpc(); // handles openExternal, http(s) only
|
|
84
|
+
new BrowserWindow({
|
|
85
|
+
webPreferences: { contextIsolation: true, preload: adviversePreloadPath() },
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Already have a preload? Add one line to it instead:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
// preload.ts
|
|
94
|
+
import { exposeAdviverse } from '@adviverse/desktop/electron';
|
|
95
|
+
exposeAdviverse(); // → window.adviverse.openExternal
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## What "done right" means (built in)
|
|
99
|
+
|
|
100
|
+
- **Viewability** — the impression fires only when the ad is ≥50% on-screen
|
|
101
|
+
**and** the window is visible **and** focused, continuously for ~1s (MRC-style).
|
|
102
|
+
Backgrounded/minimized windows don't count.
|
|
103
|
+
- **Consent** — pass `consent: { gdpr, usPrivacy }` (TCF string / US-Privacy);
|
|
104
|
+
they're forwarded to serving.
|
|
105
|
+
- **Integrity** — a stable per-install `uid` is generated + persisted; ships
|
|
106
|
+
your serve token. For signed placements use an `ak_` key server-side.
|
|
107
|
+
- **Frequency/caching** — the ad is cached ~60s per token, so a re-render or a
|
|
108
|
+
tight `refreshMs` never hammers the network.
|
|
109
|
+
|
|
110
|
+
## Config
|
|
111
|
+
|
|
112
|
+
| field | default | notes |
|
|
113
|
+
|-------|---------|-------|
|
|
114
|
+
| `token` | — | `ts_` serve token \| `ak_` API key \| publisher tag (required) |
|
|
115
|
+
| `uid` | auto | stable per-install id (persisted in localStorage) |
|
|
116
|
+
| `consent` | — | `{ gdpr, usPrivacy }` privacy signals |
|
|
117
|
+
| `emailSha256` | — | SHA-256 of a lowercased email (opt-in identity) |
|
|
118
|
+
| `client` | `"desktop"` | reporting/integrity label |
|
|
119
|
+
| `base` | `https://serve.adviverse.com` | serve host override |
|
|
120
|
+
| `timeoutMs` | `2500` | fetch timeout (UI never blocks regardless) |
|
|
121
|
+
|
|
122
|
+
## Store policies
|
|
123
|
+
|
|
124
|
+
Mac App Store / Microsoft Store require ads to be **disclosed** and non-deceptive
|
|
125
|
+
— the built-in card shows an `ad` label; keep it if you swap in custom markup.
|
|
126
|
+
Steam / self-distributed apps are more permissive.
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @adviverse/desktop — core ad loop for native desktop apps that host a webview
|
|
3
|
+
* (Electron, Tauri, CEF, WKWebView, WebView2). Framework-agnostic: fetch → cache
|
|
4
|
+
* → render → viewability → impression → click. The React and vanilla surfaces
|
|
5
|
+
* are thin wrappers over this.
|
|
6
|
+
*
|
|
7
|
+
* Serving mirrors the terminal/IDE SDKs: an `ADVIVERSE_TAG` routes by prefix —
|
|
8
|
+
* `ts_` (public serve token) and `ak_` (secret API key) go to the self-network
|
|
9
|
+
* serve endpoint; anything else is treated as a publisher placement tag.
|
|
10
|
+
*/
|
|
11
|
+
export interface Ad {
|
|
12
|
+
/** Headline — the line shown. */
|
|
13
|
+
text: string;
|
|
14
|
+
/** Optional secondary text. */
|
|
15
|
+
body?: string;
|
|
16
|
+
/** Call to action, e.g. "Try free". */
|
|
17
|
+
cta?: string;
|
|
18
|
+
brand?: string;
|
|
19
|
+
/** Tracked /click redirect — open this in the default browser on click. */
|
|
20
|
+
url?: string;
|
|
21
|
+
/** Compact /c/<code> link (fallback). */
|
|
22
|
+
short_url?: string;
|
|
23
|
+
/** GET this once when the ad is actually displayed (viewability). */
|
|
24
|
+
impression_url?: string;
|
|
25
|
+
/** Disclosure marker, e.g. "ad". */
|
|
26
|
+
label: string;
|
|
27
|
+
/** Seconds the ad may be cached/shown. */
|
|
28
|
+
ttl: number;
|
|
29
|
+
}
|
|
30
|
+
export interface AdviverseConfig {
|
|
31
|
+
/** ts_ serve token | ak_ API key | publisher placement tag. Required. */
|
|
32
|
+
token: string;
|
|
33
|
+
/** Stable per-install id. Auto-generated + persisted (localStorage) if omitted. */
|
|
34
|
+
uid?: string;
|
|
35
|
+
/** Privacy signals passed through to serving (GDPR TCF string / US Privacy). */
|
|
36
|
+
consent?: {
|
|
37
|
+
gdpr?: string;
|
|
38
|
+
usPrivacy?: string;
|
|
39
|
+
};
|
|
40
|
+
/** SHA-256 of a lowercased email, for cross-surface identity (opt-in). */
|
|
41
|
+
emailSha256?: string;
|
|
42
|
+
/** Override the serve base (default https://serve.adviverse.com). */
|
|
43
|
+
base?: string;
|
|
44
|
+
/** Fetch timeout (default 2500ms). Never blocks the UI regardless. */
|
|
45
|
+
timeoutMs?: number;
|
|
46
|
+
/** App/client label for reporting + integrity (default "desktop"). */
|
|
47
|
+
client?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Fetch an ad (cached ~60s per token so the network is hit at most once/minute).
|
|
51
|
+
* Resolves to null on no-fill / offline / error — callers render nothing then.
|
|
52
|
+
*/
|
|
53
|
+
export declare function fetchAd(cfg: AdviverseConfig): Promise<Ad | null>;
|
|
54
|
+
/** Open the ad's landing page in the user's DEFAULT browser (never in-app). */
|
|
55
|
+
export declare function openClick(ad: Ad): void;
|
|
56
|
+
/** Fire the impression beacon exactly once (idempotent per Ad object). */
|
|
57
|
+
export declare function fireImpression(ad: Ad & {
|
|
58
|
+
__imp?: boolean;
|
|
59
|
+
}): void;
|
|
60
|
+
/**
|
|
61
|
+
* Watch an element and fire the impression once it is genuinely VIEWABLE: at
|
|
62
|
+
* least 50% on-screen AND the window/tab visible AND focused, continuously for
|
|
63
|
+
* `dwellMs` (MRC-style). Returns a disposer. Pure DOM — works in any webview.
|
|
64
|
+
*/
|
|
65
|
+
export declare function observeViewability(el: Element, ad: Ad, opts?: {
|
|
66
|
+
dwellMs?: number;
|
|
67
|
+
}): () => void;
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @adviverse/desktop — core ad loop for native desktop apps that host a webview
|
|
3
|
+
* (Electron, Tauri, CEF, WKWebView, WebView2). Framework-agnostic: fetch → cache
|
|
4
|
+
* → render → viewability → impression → click. The React and vanilla surfaces
|
|
5
|
+
* are thin wrappers over this.
|
|
6
|
+
*
|
|
7
|
+
* Serving mirrors the terminal/IDE SDKs: an `ADVIVERSE_TAG` routes by prefix —
|
|
8
|
+
* `ts_` (public serve token) and `ak_` (secret API key) go to the self-network
|
|
9
|
+
* serve endpoint; anything else is treated as a publisher placement tag.
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_BASE = 'https://serve.adviverse.com';
|
|
12
|
+
const TTL_MS = 60000; // network back-off; the widget re-renders from cache freely
|
|
13
|
+
const UID_KEY = 'adviverse_uid';
|
|
14
|
+
const w = globalThis;
|
|
15
|
+
function persistedUid() {
|
|
16
|
+
try {
|
|
17
|
+
const cur = w.localStorage?.getItem(UID_KEY);
|
|
18
|
+
if (cur)
|
|
19
|
+
return cur;
|
|
20
|
+
}
|
|
21
|
+
catch { /* no storage */ }
|
|
22
|
+
const id = 'ins_' + Math.random().toString(36).slice(2, 10) + Date.now().toString(36);
|
|
23
|
+
try {
|
|
24
|
+
w.localStorage?.setItem(UID_KEY, id);
|
|
25
|
+
}
|
|
26
|
+
catch { /* ignore */ }
|
|
27
|
+
return id;
|
|
28
|
+
}
|
|
29
|
+
const memCache = new Map();
|
|
30
|
+
function serveUrl(cfg) {
|
|
31
|
+
const base = cfg.base || DEFAULT_BASE;
|
|
32
|
+
const q = new URLSearchParams({
|
|
33
|
+
uid: cfg.uid || persistedUid(),
|
|
34
|
+
client: cfg.client || 'desktop',
|
|
35
|
+
});
|
|
36
|
+
if (cfg.emailSha256)
|
|
37
|
+
q.set('em', cfg.emailSha256);
|
|
38
|
+
if (cfg.consent?.gdpr)
|
|
39
|
+
q.set('gdpr_consent', cfg.consent.gdpr);
|
|
40
|
+
if (cfg.consent?.usPrivacy)
|
|
41
|
+
q.set('us_privacy', cfg.consent.usPrivacy);
|
|
42
|
+
const headers = {};
|
|
43
|
+
if (/^ts_/.test(cfg.token)) {
|
|
44
|
+
q.set('token', cfg.token);
|
|
45
|
+
return { url: `${base}/terminal/serve?${q}`, headers };
|
|
46
|
+
}
|
|
47
|
+
if (/^ak_/.test(cfg.token)) {
|
|
48
|
+
headers['x-api-key'] = cfg.token;
|
|
49
|
+
return { url: `${base}/terminal/serve?${q}`, headers };
|
|
50
|
+
}
|
|
51
|
+
q.set('tag', cfg.token);
|
|
52
|
+
return { url: `${base}/terminal?${q}`, headers };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Fetch an ad (cached ~60s per token so the network is hit at most once/minute).
|
|
56
|
+
* Resolves to null on no-fill / offline / error — callers render nothing then.
|
|
57
|
+
*/
|
|
58
|
+
export async function fetchAd(cfg) {
|
|
59
|
+
const key = cfg.token;
|
|
60
|
+
const hit = memCache.get(key);
|
|
61
|
+
if (hit && Date.now() - hit.at < TTL_MS)
|
|
62
|
+
return hit.ad;
|
|
63
|
+
let ad = null;
|
|
64
|
+
try {
|
|
65
|
+
const { url, headers } = serveUrl(cfg);
|
|
66
|
+
const ctrl = new AbortController();
|
|
67
|
+
const timer = setTimeout(() => ctrl.abort(), cfg.timeoutMs ?? 2500);
|
|
68
|
+
const res = await fetch(url, { headers, signal: ctrl.signal, credentials: 'omit' });
|
|
69
|
+
clearTimeout(timer);
|
|
70
|
+
const body = (await res.json());
|
|
71
|
+
if (body && body.text)
|
|
72
|
+
ad = body;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
ad = null; // offline / timeout / no-fill — cache the miss to back off for TTL
|
|
76
|
+
}
|
|
77
|
+
memCache.set(key, { at: Date.now(), ad });
|
|
78
|
+
return ad;
|
|
79
|
+
}
|
|
80
|
+
/** Open the ad's landing page in the user's DEFAULT browser (never in-app). */
|
|
81
|
+
export function openClick(ad) {
|
|
82
|
+
const url = ad.url || ad.short_url;
|
|
83
|
+
if (!url)
|
|
84
|
+
return;
|
|
85
|
+
// Electron: the app's preload should expose window.adviverse.openExternal →
|
|
86
|
+
// shell.openExternal (see electron.ts). Tauri: the shell-open plugin. Else the
|
|
87
|
+
// webview's window.open, which most hosts route to the system browser.
|
|
88
|
+
try {
|
|
89
|
+
if (typeof w.adviverse?.openExternal === 'function') {
|
|
90
|
+
w.adviverse.openExternal(url);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const tauriOpen = w.__TAURI__?.shell?.open;
|
|
94
|
+
if (typeof tauriOpen === 'function') {
|
|
95
|
+
tauriOpen(url);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (w.__TAURI_INTERNALS__ && typeof w.__TAURI_INVOKE__ !== 'undefined') {
|
|
99
|
+
// Tauri v2 with the shell plugin registered.
|
|
100
|
+
w.__TAURI__?.core?.invoke?.('plugin:shell|open', { path: url });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
w.open?.(url, '_blank', 'noopener,noreferrer');
|
|
104
|
+
}
|
|
105
|
+
catch { /* best-effort */ }
|
|
106
|
+
}
|
|
107
|
+
/** Fire the impression beacon exactly once (idempotent per Ad object). */
|
|
108
|
+
export function fireImpression(ad) {
|
|
109
|
+
if (ad.__imp || !ad.impression_url)
|
|
110
|
+
return;
|
|
111
|
+
ad.__imp = true;
|
|
112
|
+
try {
|
|
113
|
+
fetch(ad.impression_url, { credentials: 'omit', keepalive: true }).catch(() => { });
|
|
114
|
+
}
|
|
115
|
+
catch { /* ignore */ }
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Watch an element and fire the impression once it is genuinely VIEWABLE: at
|
|
119
|
+
* least 50% on-screen AND the window/tab visible AND focused, continuously for
|
|
120
|
+
* `dwellMs` (MRC-style). Returns a disposer. Pure DOM — works in any webview.
|
|
121
|
+
*/
|
|
122
|
+
export function observeViewability(el, ad, opts = {}) {
|
|
123
|
+
const dwell = opts.dwellMs ?? 1000;
|
|
124
|
+
let timer = null;
|
|
125
|
+
let done = false;
|
|
126
|
+
const visible = () => (typeof document === 'undefined' || document.visibilityState === 'visible') &&
|
|
127
|
+
(typeof document === 'undefined' || typeof document.hasFocus !== 'function' || document.hasFocus());
|
|
128
|
+
const stop = () => { if (timer) {
|
|
129
|
+
clearTimeout(timer);
|
|
130
|
+
timer = null;
|
|
131
|
+
} };
|
|
132
|
+
const arm = () => {
|
|
133
|
+
if (done || timer)
|
|
134
|
+
return;
|
|
135
|
+
timer = setTimeout(() => {
|
|
136
|
+
if (!done && visible()) {
|
|
137
|
+
done = true;
|
|
138
|
+
fireImpression(ad);
|
|
139
|
+
io.disconnect();
|
|
140
|
+
window.removeEventListener('blur', stop);
|
|
141
|
+
}
|
|
142
|
+
}, dwell);
|
|
143
|
+
};
|
|
144
|
+
const io = new IntersectionObserver((entries) => {
|
|
145
|
+
for (const e of entries) {
|
|
146
|
+
if (e.isIntersecting && e.intersectionRatio >= 0.5 && visible())
|
|
147
|
+
arm();
|
|
148
|
+
else
|
|
149
|
+
stop();
|
|
150
|
+
}
|
|
151
|
+
}, { threshold: [0, 0.5, 1] });
|
|
152
|
+
io.observe(el);
|
|
153
|
+
window.addEventListener('blur', stop);
|
|
154
|
+
return () => { done = true; stop(); io.disconnect(); window.removeEventListener('blur', stop); };
|
|
155
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Call in the MAIN process: handle open-external requests, allowing http(s) only. */
|
|
2
|
+
export declare function registerAdviverseIpc(): void;
|
|
3
|
+
/** Call in the PRELOAD script: expose window.adviverse.openExternal to the renderer. */
|
|
4
|
+
export declare function exposeAdviverse(): void;
|
|
5
|
+
/** Path to a ready-made preload that calls exposeAdviverse(); use in webPreferences.preload. */
|
|
6
|
+
export declare function adviversePreloadPath(): string;
|
|
7
|
+
/** Convenience: register the IPC handler (main process). Pair with a preload that
|
|
8
|
+
* calls exposeAdviverse(), e.g. adviversePreloadPath(). */
|
|
9
|
+
export declare function registerAdviverse(): void;
|
package/dist/electron.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Electron MAIN-process helper. A renderer webview can't open the system
|
|
3
|
+
* browser directly (and shouldn't navigate the app window to an ad landing
|
|
4
|
+
* page), so the main process exposes a tiny bridge the renderer calls through
|
|
5
|
+
* `window.adviverse.openExternal()`.
|
|
6
|
+
*
|
|
7
|
+
* main.ts:
|
|
8
|
+
* import { registerAdviverse } from '@adviverse/desktop/electron';
|
|
9
|
+
* registerAdviverse(); // once, after app is ready
|
|
10
|
+
* // ...and in your BrowserWindow webPreferences: preload: adviversePreloadPath()
|
|
11
|
+
*
|
|
12
|
+
* If you already have a preload, instead add ONE line to it:
|
|
13
|
+
* import { exposeAdviverse } from '@adviverse/desktop/electron';
|
|
14
|
+
* exposeAdviverse(); // in the preload script
|
|
15
|
+
* and in main: registerAdviverseIpc();
|
|
16
|
+
*/
|
|
17
|
+
import { ipcMain, shell, contextBridge } from 'electron';
|
|
18
|
+
import * as path from 'path';
|
|
19
|
+
const CHANNEL = 'adviverse:openExternal';
|
|
20
|
+
/** Call in the MAIN process: handle open-external requests, allowing http(s) only. */
|
|
21
|
+
export function registerAdviverseIpc() {
|
|
22
|
+
ipcMain.handle(CHANNEL, (_evt, url) => {
|
|
23
|
+
if (typeof url === 'string' && /^https?:\/\//i.test(url)) {
|
|
24
|
+
return shell.openExternal(url);
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/** Call in the PRELOAD script: expose window.adviverse.openExternal to the renderer. */
|
|
30
|
+
export function exposeAdviverse() {
|
|
31
|
+
// Lazy require so this file is importable in the main process too.
|
|
32
|
+
const { ipcRenderer } = require('electron');
|
|
33
|
+
contextBridge.exposeInMainWorld('adviverse', {
|
|
34
|
+
openExternal: (url) => ipcRenderer.invoke(CHANNEL, url),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/** Path to a ready-made preload that calls exposeAdviverse(); use in webPreferences.preload. */
|
|
38
|
+
export function adviversePreloadPath() {
|
|
39
|
+
return path.join(__dirname, 'preload.js');
|
|
40
|
+
}
|
|
41
|
+
/** Convenience: register the IPC handler (main process). Pair with a preload that
|
|
42
|
+
* calls exposeAdviverse(), e.g. adviversePreloadPath(). */
|
|
43
|
+
export function registerAdviverse() {
|
|
44
|
+
registerAdviverseIpc();
|
|
45
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @adviverse/desktop — ads for native desktop apps that host a webview
|
|
3
|
+
* (Electron, Tauri, CEF, WKWebView, WebView2).
|
|
4
|
+
*
|
|
5
|
+
* Renderer entry (no React): core loop + a framework-free mount.
|
|
6
|
+
* import { mountAd, fetchAd } from '@adviverse/desktop';
|
|
7
|
+
* React: import { AdviverseAd } from '@adviverse/desktop/react';
|
|
8
|
+
* Electron main: import { registerAdviverse } from '@adviverse/desktop/electron';
|
|
9
|
+
*/
|
|
10
|
+
export type { Ad, AdviverseConfig } from './core';
|
|
11
|
+
export { fetchAd, openClick, fireImpression, observeViewability } from './core';
|
|
12
|
+
export { mountAd } from './vanilla';
|
|
13
|
+
export type { MountOptions } from './vanilla';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/preload.js
ADDED
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Ad, AdviverseConfig } from './core';
|
|
2
|
+
export interface AdviverseAdProps extends AdviverseConfig {
|
|
3
|
+
/** Re-fetch on an interval (ms). Omit for a single fetch on mount. */
|
|
4
|
+
refreshMs?: number;
|
|
5
|
+
className?: string;
|
|
6
|
+
style?: React.CSSProperties;
|
|
7
|
+
/** Render your own card instead of the built-in one. */
|
|
8
|
+
children?: (ad: Ad, onClick: () => void) => React.ReactNode;
|
|
9
|
+
}
|
|
10
|
+
export declare function AdviverseAd(props: AdviverseAdProps): JSX.Element | null;
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* <AdviverseAd/> — drop-in React ad card for Electron/Tauri renderers.
|
|
4
|
+
*
|
|
5
|
+
* import { AdviverseAd } from '@adviverse/desktop/react';
|
|
6
|
+
* <AdviverseAd token="ts_yourtoken" />
|
|
7
|
+
*
|
|
8
|
+
* Fetches an ad, renders a disclosed card, fires the impression on true
|
|
9
|
+
* viewability, and opens clicks in the default browser. Renders nothing on
|
|
10
|
+
* no-fill. Styling is minimal + theme-aware; pass `className`/`style` to skin it.
|
|
11
|
+
*/
|
|
12
|
+
import { useEffect, useRef, useState } from 'react';
|
|
13
|
+
import { fetchAd, observeViewability, openClick } from './core';
|
|
14
|
+
export function AdviverseAd(props) {
|
|
15
|
+
const { refreshMs, className, style, children, ...cfg } = props;
|
|
16
|
+
const [ad, setAd] = useState(null);
|
|
17
|
+
const ref = useRef(null);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
let alive = true;
|
|
20
|
+
const load = () => { fetchAd(cfg).then((a) => { if (alive)
|
|
21
|
+
setAd(a); }); };
|
|
22
|
+
load();
|
|
23
|
+
const id = refreshMs ? setInterval(load, refreshMs) : undefined;
|
|
24
|
+
return () => { alive = false; if (id)
|
|
25
|
+
clearInterval(id); };
|
|
26
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
27
|
+
}, [cfg.token, refreshMs]);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!ad || !ref.current)
|
|
30
|
+
return;
|
|
31
|
+
return observeViewability(ref.current, ad);
|
|
32
|
+
}, [ad]);
|
|
33
|
+
if (!ad)
|
|
34
|
+
return null;
|
|
35
|
+
const onClick = () => openClick(ad);
|
|
36
|
+
if (children) {
|
|
37
|
+
return _jsx("div", { ref: ref, className: className, style: style, children: children(ad, onClick) });
|
|
38
|
+
}
|
|
39
|
+
return (_jsxs("div", { ref: ref, className: className, onClick: onClick, role: "link", tabIndex: 0, onKeyDown: (e) => { if (e.key === 'Enter' || e.key === ' ')
|
|
40
|
+
onClick(); }, style: {
|
|
41
|
+
display: 'flex', alignItems: 'center', gap: 10, cursor: 'pointer',
|
|
42
|
+
padding: '10px 12px', borderRadius: 10, fontSize: 13, lineHeight: 1.35,
|
|
43
|
+
border: '1px solid rgba(127,127,127,0.22)', background: 'rgba(127,127,127,0.06)',
|
|
44
|
+
color: 'inherit', maxWidth: '100%', boxSizing: 'border-box', ...style,
|
|
45
|
+
}, children: [_jsx("span", { style: {
|
|
46
|
+
fontSize: 10, textTransform: 'uppercase', letterSpacing: '0.06em',
|
|
47
|
+
opacity: 0.6, flex: 'none',
|
|
48
|
+
}, children: ad.label || 'ad' }), _jsxs("span", { style: { flex: 1, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }, children: [ad.brand ? _jsx("b", { children: ad.brand }) : null, ad.brand ? ' — ' : '', ad.text, ad.body ? _jsx("span", { style: { opacity: 0.7 }, children: ' ' + ad.body }) : null] }), ad.cta ? (_jsxs("span", { style: {
|
|
49
|
+
flex: 'none', fontWeight: 600, color: '#1b50e0',
|
|
50
|
+
whiteSpace: 'nowrap',
|
|
51
|
+
}, children: [ad.cta, " \u2192"] })) : null] }));
|
|
52
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-free ad mount for any webview renderer (no React).
|
|
3
|
+
*
|
|
4
|
+
* import { mountAd } from '@adviverse/desktop';
|
|
5
|
+
* const dispose = mountAd(document.getElementById('ad'), { token: 'ts_...' });
|
|
6
|
+
*
|
|
7
|
+
* Builds a disclosed ad card in `el`, fires the impression on viewability, and
|
|
8
|
+
* opens clicks in the default browser. Returns a disposer. Renders nothing on
|
|
9
|
+
* no-fill. Re-render with `refreshMs` for rotation (min 30s; paused while hidden).
|
|
10
|
+
*/
|
|
11
|
+
import { AdviverseConfig } from './core';
|
|
12
|
+
export interface MountOptions extends AdviverseConfig {
|
|
13
|
+
refreshMs?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function mountAd(el: Element, opts: MountOptions): () => void;
|
package/dist/vanilla.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-free ad mount for any webview renderer (no React).
|
|
3
|
+
*
|
|
4
|
+
* import { mountAd } from '@adviverse/desktop';
|
|
5
|
+
* const dispose = mountAd(document.getElementById('ad'), { token: 'ts_...' });
|
|
6
|
+
*
|
|
7
|
+
* Builds a disclosed ad card in `el`, fires the impression on viewability, and
|
|
8
|
+
* opens clicks in the default browser. Returns a disposer. Renders nothing on
|
|
9
|
+
* no-fill. Re-render with `refreshMs` for rotation (min 30s; paused while hidden).
|
|
10
|
+
*/
|
|
11
|
+
import { fetchAd, observeViewability, openClick } from './core';
|
|
12
|
+
function card(ad) {
|
|
13
|
+
const root = document.createElement('div');
|
|
14
|
+
root.setAttribute('role', 'link');
|
|
15
|
+
root.tabIndex = 0;
|
|
16
|
+
Object.assign(root.style, {
|
|
17
|
+
display: 'flex', alignItems: 'center', gap: '10px', cursor: 'pointer',
|
|
18
|
+
padding: '10px 12px', borderRadius: '10px', fontSize: '13px', lineHeight: '1.35',
|
|
19
|
+
border: '1px solid rgba(127,127,127,0.22)', background: 'rgba(127,127,127,0.06)',
|
|
20
|
+
boxSizing: 'border-box', maxWidth: '100%',
|
|
21
|
+
});
|
|
22
|
+
const label = document.createElement('span');
|
|
23
|
+
label.textContent = ad.label || 'ad';
|
|
24
|
+
Object.assign(label.style, { fontSize: '10px', textTransform: 'uppercase', letterSpacing: '0.06em', opacity: '0.6', flex: 'none' });
|
|
25
|
+
const body = document.createElement('span');
|
|
26
|
+
Object.assign(body.style, { flex: '1', minWidth: '0', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' });
|
|
27
|
+
body.textContent = (ad.brand ? ad.brand + ' — ' : '') + ad.text + (ad.body ? ' ' + ad.body : '');
|
|
28
|
+
root.append(label, body);
|
|
29
|
+
if (ad.cta) {
|
|
30
|
+
const cta = document.createElement('span');
|
|
31
|
+
cta.textContent = ad.cta + ' →';
|
|
32
|
+
Object.assign(cta.style, { flex: 'none', fontWeight: '600', color: '#1b50e0', whiteSpace: 'nowrap' });
|
|
33
|
+
root.append(cta);
|
|
34
|
+
}
|
|
35
|
+
return root;
|
|
36
|
+
}
|
|
37
|
+
export function mountAd(el, opts) {
|
|
38
|
+
let disposeView = null;
|
|
39
|
+
let alive = true;
|
|
40
|
+
const render = (ad) => {
|
|
41
|
+
if (!alive)
|
|
42
|
+
return;
|
|
43
|
+
if (disposeView) {
|
|
44
|
+
disposeView();
|
|
45
|
+
disposeView = null;
|
|
46
|
+
}
|
|
47
|
+
el.innerHTML = '';
|
|
48
|
+
if (!ad)
|
|
49
|
+
return;
|
|
50
|
+
const node = card(ad);
|
|
51
|
+
const onClick = () => openClick(ad);
|
|
52
|
+
node.addEventListener('click', onClick);
|
|
53
|
+
node.addEventListener('keydown', (e) => { const k = e.key; if (k === 'Enter' || k === ' ')
|
|
54
|
+
onClick(); });
|
|
55
|
+
el.append(node);
|
|
56
|
+
disposeView = observeViewability(node, ad);
|
|
57
|
+
};
|
|
58
|
+
const load = () => fetchAd(opts).then(render);
|
|
59
|
+
load();
|
|
60
|
+
// Rotation floor 30s (faster refreshes are voided as repeat views anyway); skip while the window is hidden.
|
|
61
|
+
const refresh = () => { if (typeof document === 'undefined' || document.visibilityState === 'visible')
|
|
62
|
+
load(); };
|
|
63
|
+
const id = opts.refreshMs ? setInterval(refresh, Math.max(30000, opts.refreshMs)) : undefined;
|
|
64
|
+
return () => {
|
|
65
|
+
alive = false;
|
|
66
|
+
if (id)
|
|
67
|
+
clearInterval(id);
|
|
68
|
+
if (disposeView)
|
|
69
|
+
disposeView();
|
|
70
|
+
el.innerHTML = '';
|
|
71
|
+
};
|
|
72
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adviverse/desktop",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Adviverse ads for native desktop apps that host a webview (Electron, Tauri, CEF, WKWebView, WebView2).",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
11
|
+
"./react": { "types": "./dist/react.d.ts", "import": "./dist/react.js" },
|
|
12
|
+
"./electron": { "types": "./dist/electron.d.ts", "import": "./dist/electron.js" }
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist", "README.md"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"react": ">=17",
|
|
21
|
+
"electron": ">=22"
|
|
22
|
+
},
|
|
23
|
+
"peerDependenciesMeta": {
|
|
24
|
+
"react": { "optional": true },
|
|
25
|
+
"electron": { "optional": true }
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/react": "^18.3.0",
|
|
29
|
+
"@types/node": "^22.0.0",
|
|
30
|
+
"electron": "^30.0.0",
|
|
31
|
+
"react": "^18.3.0",
|
|
32
|
+
"typescript": "^5.6.2"
|
|
33
|
+
},
|
|
34
|
+
"keywords": ["adviverse", "ads", "desktop", "electron", "tauri", "webview"]
|
|
35
|
+
}
|