@scarlett-player/watermark 1.8.0 → 1.9.0
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 +114 -0
- package/dist/index.cjs +47 -5
- package/dist/index.js +47 -5
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# @scarlett-player/watermark
|
|
2
|
+
|
|
3
|
+
Anti-piracy watermark plugin for [Scarlett Player](https://scarlettplayer.com). Overlays text (typically the viewer's email or account id) or an image on the player, at a fixed corner or moving to a random position on a timer, with an optional delay before it first appears. The overlay is shown on play and hidden on pause and ended, so it never sits on the poster.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @scarlett-player/watermark @scarlett-player/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`@scarlett-player/core` is a peer dependency.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createPlayer } from '@scarlett-player/core';
|
|
17
|
+
import { createHLSPlugin } from '@scarlett-player/hls';
|
|
18
|
+
import { createWatermarkPlugin } from '@scarlett-player/watermark';
|
|
19
|
+
|
|
20
|
+
const player = await createPlayer({
|
|
21
|
+
container: '#player',
|
|
22
|
+
src: 'https://example.com/video.m3u8',
|
|
23
|
+
plugins: [
|
|
24
|
+
createHLSPlugin(),
|
|
25
|
+
createWatermarkPlugin({
|
|
26
|
+
text: 'user@example.com',
|
|
27
|
+
position: 'bottom-right',
|
|
28
|
+
opacity: 0.4,
|
|
29
|
+
dynamic: true,
|
|
30
|
+
dynamicInterval: 15000,
|
|
31
|
+
showDelay: 20000,
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Pass `imageUrl` instead of `text` to render a logo. When both are set the image wins.
|
|
38
|
+
|
|
39
|
+
## Configuration
|
|
40
|
+
|
|
41
|
+
| Option | Type | Default | Description |
|
|
42
|
+
|---|---|---|---|
|
|
43
|
+
| `text` | `string` | `undefined` | Text to render. Ignored when `imageUrl` is set |
|
|
44
|
+
| `imageUrl` | `string` | `undefined` | Image to render instead of text |
|
|
45
|
+
| `position` | `'top-left' \| 'top-right' \| 'bottom-left' \| 'bottom-right' \| 'center'` | `'bottom-right'` | Starting position |
|
|
46
|
+
| `opacity` | `number` | `0.5` | Opacity from 0 to 1 |
|
|
47
|
+
| `fontSize` | `number` | `14` | Font size in px for text watermarks |
|
|
48
|
+
| `imageHeight` | `number` | `40` | Maximum image height in px. Only applies with `imageUrl` |
|
|
49
|
+
| `padding` | `number` | `10` (top and sides), `40` (bottom) | Distance from the edges in px. The larger bottom default keeps the mark clear of the control bar. Setting a value applies it to every edge |
|
|
50
|
+
| `dynamic` | `boolean` | `false` | Move to a different random position on a timer |
|
|
51
|
+
| `dynamicInterval` | `number` | `10000` | Milliseconds between moves when `dynamic` is on |
|
|
52
|
+
| `showDelay` | `number` | `0` | Milliseconds to wait after play before the mark first appears. A pause during the delay cancels it |
|
|
53
|
+
|
|
54
|
+
The `dynamic` timer starts when the mark is shown and stops on pause and ended, so a paused player never keeps a timer alive.
|
|
55
|
+
|
|
56
|
+
## Plugin API
|
|
57
|
+
|
|
58
|
+
The plugin is registered under the id `watermark`:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import type { IWatermarkPlugin } from '@scarlett-player/watermark';
|
|
62
|
+
|
|
63
|
+
const watermark = player.getPlugin<IWatermarkPlugin>('watermark');
|
|
64
|
+
|
|
65
|
+
watermark?.setText('session 8f3a');
|
|
66
|
+
watermark?.setImage('/logo.png');
|
|
67
|
+
watermark?.setPosition('top-left');
|
|
68
|
+
watermark?.setOpacity(0.3);
|
|
69
|
+
watermark?.setImageHeight(60);
|
|
70
|
+
watermark?.setPadding(24);
|
|
71
|
+
watermark?.hide();
|
|
72
|
+
watermark?.show();
|
|
73
|
+
watermark?.getConfig();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
| Method | Description |
|
|
77
|
+
|---|---|
|
|
78
|
+
| `setText(text)` | Replace the content with text |
|
|
79
|
+
| `setImage(imageUrl)` | Replace the content with an image |
|
|
80
|
+
| `setPosition(position)` | Move to one of the five positions |
|
|
81
|
+
| `setOpacity(opacity)` | Set opacity, clamped to 0 to 1 |
|
|
82
|
+
| `setImageHeight(height)` | Set the maximum image height in px |
|
|
83
|
+
| `setPadding(padding)` | Set the edge padding in px for every edge and re-apply the current position |
|
|
84
|
+
| `show()` / `hide()` | Toggle visibility without touching the timers |
|
|
85
|
+
| `getConfig()` | The original config merged with the current position, opacity, image height and padding |
|
|
86
|
+
|
|
87
|
+
## Per-track watermarks
|
|
88
|
+
|
|
89
|
+
When `@scarlett-player/playlist` is present the plugin listens for `playlist:change` and reads `metadata.watermarkUrl` or `metadata.watermarkText` from the new track. If either is set the content is replaced for that track; tracks without them keep the current content.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
{ id: '2', src: '/episode-2.mp4', metadata: { watermarkText: 'Screener: press only' } }
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Styling
|
|
96
|
+
|
|
97
|
+
The overlay is a single `div` appended to the player container with inline positioning, `pointer-events: none`, white text with a subtle shadow, and a 0.5s transition. It carries these classes for your own CSS:
|
|
98
|
+
|
|
99
|
+
| Class | When |
|
|
100
|
+
|---|---|
|
|
101
|
+
| `sp-watermark` | Always |
|
|
102
|
+
| `sp-watermark--visible` / `sp-watermark--hidden` | Current visibility |
|
|
103
|
+
| `sp-watermark--top-left`, `sp-watermark--top-right`, `sp-watermark--bottom-left`, `sp-watermark--bottom-right`, `sp-watermark--center` | Current position, applied after the first `setPosition()` or dynamic move |
|
|
104
|
+
| `sp-watermark--dynamic` | Present when `dynamic` is on, applied after the first position change |
|
|
105
|
+
|
|
106
|
+
The element also has a `data-position` attribute holding the current position. No CSS custom properties are used.
|
|
107
|
+
|
|
108
|
+
## Events
|
|
109
|
+
|
|
110
|
+
The plugin emits no events. It listens to `playback:play`, `playback:pause`, `playback:ended` and `playlist:change`.
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -26,7 +26,7 @@ __export(index_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
27
|
|
|
28
28
|
// src/version.ts
|
|
29
|
-
var PKG_VERSION = true ? "1.
|
|
29
|
+
var PKG_VERSION = true ? "1.9.0" : "0.0.0-dev";
|
|
30
30
|
|
|
31
31
|
// src/index.ts
|
|
32
32
|
var POSITIONS = ["top-left", "top-right", "bottom-left", "bottom-right", "center"];
|
|
@@ -53,6 +53,9 @@ function createWatermarkPlugin(config = {}) {
|
|
|
53
53
|
const dynamic = config.dynamic ?? false;
|
|
54
54
|
const dynamicInterval = config.dynamicInterval ?? 1e4;
|
|
55
55
|
const showDelay = config.showDelay ?? 0;
|
|
56
|
+
let currentImageUrl = config.imageUrl;
|
|
57
|
+
let currentText = config.text;
|
|
58
|
+
let mutationObserver = null;
|
|
56
59
|
let positionStyles = getPositionStyles(currentPadding, currentBottomPadding);
|
|
57
60
|
const createElement = () => {
|
|
58
61
|
const el = document.createElement("div");
|
|
@@ -63,8 +66,16 @@ function createWatermarkPlugin(config = {}) {
|
|
|
63
66
|
return el;
|
|
64
67
|
};
|
|
65
68
|
const updateContent = (el, imageUrl, text) => {
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
if (imageUrl !== void 0) {
|
|
70
|
+
currentImageUrl = imageUrl;
|
|
71
|
+
currentText = void 0;
|
|
72
|
+
}
|
|
73
|
+
if (text !== void 0) {
|
|
74
|
+
currentText = text;
|
|
75
|
+
currentImageUrl = void 0;
|
|
76
|
+
}
|
|
77
|
+
const img = currentImageUrl;
|
|
78
|
+
const txt = currentText;
|
|
68
79
|
el.innerHTML = "";
|
|
69
80
|
if (img) {
|
|
70
81
|
const imgEl = document.createElement("img");
|
|
@@ -130,6 +141,10 @@ function createWatermarkPlugin(config = {}) {
|
|
|
130
141
|
clearTimeout(showDelayTimer);
|
|
131
142
|
showDelayTimer = null;
|
|
132
143
|
}
|
|
144
|
+
if (mutationObserver) {
|
|
145
|
+
mutationObserver.disconnect();
|
|
146
|
+
mutationObserver = null;
|
|
147
|
+
}
|
|
133
148
|
if (element?.parentNode) {
|
|
134
149
|
element.parentNode.removeChild(element);
|
|
135
150
|
}
|
|
@@ -146,7 +161,35 @@ function createWatermarkPlugin(config = {}) {
|
|
|
146
161
|
api.logger.debug("Watermark plugin initialized");
|
|
147
162
|
element = createElement();
|
|
148
163
|
api.container.appendChild(element);
|
|
164
|
+
mutationObserver = new MutationObserver((mutations) => {
|
|
165
|
+
if (!element || !api) return;
|
|
166
|
+
for (const mutation of mutations) {
|
|
167
|
+
if (mutation.type === "childList") {
|
|
168
|
+
if (mutation.removedNodes.length > 0) {
|
|
169
|
+
let wasRemoved = false;
|
|
170
|
+
mutation.removedNodes.forEach((node) => {
|
|
171
|
+
if (node === element) wasRemoved = true;
|
|
172
|
+
});
|
|
173
|
+
if (wasRemoved && api) {
|
|
174
|
+
api.container.appendChild(element);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
} else if (mutation.type === "attributes" && mutation.attributeName === "style") {
|
|
178
|
+
if (element) {
|
|
179
|
+
element.style.opacity = String(opacity);
|
|
180
|
+
element.style.pointerEvents = "none";
|
|
181
|
+
element.style.position = "absolute";
|
|
182
|
+
element.style.zIndex = "10";
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
mutationObserver.observe(api.container, { childList: true, subtree: true, attributes: true, attributeFilter: ["style"] });
|
|
149
188
|
const unsubPlay = api.on("playback:play", () => {
|
|
189
|
+
if (showDelayTimer) {
|
|
190
|
+
clearTimeout(showDelayTimer);
|
|
191
|
+
showDelayTimer = null;
|
|
192
|
+
}
|
|
150
193
|
if (showDelay > 0) {
|
|
151
194
|
showDelayTimer = setTimeout(() => {
|
|
152
195
|
show();
|
|
@@ -158,7 +201,6 @@ function createWatermarkPlugin(config = {}) {
|
|
|
158
201
|
}
|
|
159
202
|
});
|
|
160
203
|
const unsubPause = api.on("playback:pause", () => {
|
|
161
|
-
hide();
|
|
162
204
|
stopDynamic();
|
|
163
205
|
if (showDelayTimer) {
|
|
164
206
|
clearTimeout(showDelayTimer);
|
|
@@ -212,7 +254,7 @@ function createWatermarkPlugin(config = {}) {
|
|
|
212
254
|
},
|
|
213
255
|
setPadding(value) {
|
|
214
256
|
currentPadding = Math.max(0, value);
|
|
215
|
-
currentBottomPadding =
|
|
257
|
+
currentBottomPadding = Math.max(value, config.padding ?? 40);
|
|
216
258
|
positionStyles = getPositionStyles(currentPadding, currentBottomPadding);
|
|
217
259
|
setPosition(currentPosition);
|
|
218
260
|
},
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/version.ts
|
|
2
|
-
var PKG_VERSION = true ? "1.
|
|
2
|
+
var PKG_VERSION = true ? "1.9.0" : "0.0.0-dev";
|
|
3
3
|
|
|
4
4
|
// src/index.ts
|
|
5
5
|
var POSITIONS = ["top-left", "top-right", "bottom-left", "bottom-right", "center"];
|
|
@@ -26,6 +26,9 @@ function createWatermarkPlugin(config = {}) {
|
|
|
26
26
|
const dynamic = config.dynamic ?? false;
|
|
27
27
|
const dynamicInterval = config.dynamicInterval ?? 1e4;
|
|
28
28
|
const showDelay = config.showDelay ?? 0;
|
|
29
|
+
let currentImageUrl = config.imageUrl;
|
|
30
|
+
let currentText = config.text;
|
|
31
|
+
let mutationObserver = null;
|
|
29
32
|
let positionStyles = getPositionStyles(currentPadding, currentBottomPadding);
|
|
30
33
|
const createElement = () => {
|
|
31
34
|
const el = document.createElement("div");
|
|
@@ -36,8 +39,16 @@ function createWatermarkPlugin(config = {}) {
|
|
|
36
39
|
return el;
|
|
37
40
|
};
|
|
38
41
|
const updateContent = (el, imageUrl, text) => {
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
if (imageUrl !== void 0) {
|
|
43
|
+
currentImageUrl = imageUrl;
|
|
44
|
+
currentText = void 0;
|
|
45
|
+
}
|
|
46
|
+
if (text !== void 0) {
|
|
47
|
+
currentText = text;
|
|
48
|
+
currentImageUrl = void 0;
|
|
49
|
+
}
|
|
50
|
+
const img = currentImageUrl;
|
|
51
|
+
const txt = currentText;
|
|
41
52
|
el.innerHTML = "";
|
|
42
53
|
if (img) {
|
|
43
54
|
const imgEl = document.createElement("img");
|
|
@@ -103,6 +114,10 @@ function createWatermarkPlugin(config = {}) {
|
|
|
103
114
|
clearTimeout(showDelayTimer);
|
|
104
115
|
showDelayTimer = null;
|
|
105
116
|
}
|
|
117
|
+
if (mutationObserver) {
|
|
118
|
+
mutationObserver.disconnect();
|
|
119
|
+
mutationObserver = null;
|
|
120
|
+
}
|
|
106
121
|
if (element?.parentNode) {
|
|
107
122
|
element.parentNode.removeChild(element);
|
|
108
123
|
}
|
|
@@ -119,7 +134,35 @@ function createWatermarkPlugin(config = {}) {
|
|
|
119
134
|
api.logger.debug("Watermark plugin initialized");
|
|
120
135
|
element = createElement();
|
|
121
136
|
api.container.appendChild(element);
|
|
137
|
+
mutationObserver = new MutationObserver((mutations) => {
|
|
138
|
+
if (!element || !api) return;
|
|
139
|
+
for (const mutation of mutations) {
|
|
140
|
+
if (mutation.type === "childList") {
|
|
141
|
+
if (mutation.removedNodes.length > 0) {
|
|
142
|
+
let wasRemoved = false;
|
|
143
|
+
mutation.removedNodes.forEach((node) => {
|
|
144
|
+
if (node === element) wasRemoved = true;
|
|
145
|
+
});
|
|
146
|
+
if (wasRemoved && api) {
|
|
147
|
+
api.container.appendChild(element);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
} else if (mutation.type === "attributes" && mutation.attributeName === "style") {
|
|
151
|
+
if (element) {
|
|
152
|
+
element.style.opacity = String(opacity);
|
|
153
|
+
element.style.pointerEvents = "none";
|
|
154
|
+
element.style.position = "absolute";
|
|
155
|
+
element.style.zIndex = "10";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
mutationObserver.observe(api.container, { childList: true, subtree: true, attributes: true, attributeFilter: ["style"] });
|
|
122
161
|
const unsubPlay = api.on("playback:play", () => {
|
|
162
|
+
if (showDelayTimer) {
|
|
163
|
+
clearTimeout(showDelayTimer);
|
|
164
|
+
showDelayTimer = null;
|
|
165
|
+
}
|
|
123
166
|
if (showDelay > 0) {
|
|
124
167
|
showDelayTimer = setTimeout(() => {
|
|
125
168
|
show();
|
|
@@ -131,7 +174,6 @@ function createWatermarkPlugin(config = {}) {
|
|
|
131
174
|
}
|
|
132
175
|
});
|
|
133
176
|
const unsubPause = api.on("playback:pause", () => {
|
|
134
|
-
hide();
|
|
135
177
|
stopDynamic();
|
|
136
178
|
if (showDelayTimer) {
|
|
137
179
|
clearTimeout(showDelayTimer);
|
|
@@ -185,7 +227,7 @@ function createWatermarkPlugin(config = {}) {
|
|
|
185
227
|
},
|
|
186
228
|
setPadding(value) {
|
|
187
229
|
currentPadding = Math.max(0, value);
|
|
188
|
-
currentBottomPadding =
|
|
230
|
+
currentBottomPadding = Math.max(value, config.padding ?? 40);
|
|
189
231
|
positionStyles = getPositionStyles(currentPadding, currentBottomPadding);
|
|
190
232
|
setPosition(currentPosition);
|
|
191
233
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scarlett-player/watermark",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Watermark Plugin for Scarlett Player - anti-piracy text/image overlay",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@scarlett-player/core": "^1.
|
|
25
|
+
"@scarlett-player/core": "^1.8.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@vitest/coverage-v8": "^1.6.0",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"tsup": "^8.5.1",
|
|
31
31
|
"typescript": "^5.3.0",
|
|
32
32
|
"vitest": "^1.6.0",
|
|
33
|
-
"@scarlett-player/core": "1.
|
|
33
|
+
"@scarlett-player/core": "1.9.0"
|
|
34
34
|
},
|
|
35
35
|
"keywords": [
|
|
36
36
|
"video",
|