forked from LenAnderson/SillyTavern-VideoBackgrounds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
168 lines (160 loc) · 6.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { delay } from '../../../utils.js';
(function() {
'use strict';
const wait = async(millis)=>(new Promise(resolve=>setTimeout(resolve,millis)));
const debounce = (func, delay = 100)=>{
let to;
return (...args) => {
if (to) clearTimeout(to);
to = setTimeout(()=>func.apply(this, args), delay);
};
};
let videoEl;
Array.from(document.querySelectorAll('#bg1, #bg_custom')).forEach(it=>it.style.backgroundPosition = 'center');
const replaceBg = async(muts = [])=>{
console.log('[STVBG]', 'replaceBg', muts);
await wait(1000);
const el = document.querySelector('#bg1');
const customEl = document.querySelector('#bg_custom');
const bg = window.getComputedStyle(el).background;
const bgUrl = bg.replace(/^.*?url\(['"]?([^'"\)]+)['"]\).*?$/, '$1');
const customBg = window.getComputedStyle(customEl).background;
const customUrl = customBg.replace(/^.*?url\(['"]?([^'"\)]+)['"]\).*?$/, '$1');
let url;
if (customUrl && customUrl != customBg) {
url = customUrl;
} else if (bgUrl && bgUrl != bg) {
url = bgUrl;
}
console.log('[STVBG]', url);
if (url.match(/^.+\.[a-z0-z]+\.[a-z0-9]+$/i)) {
const vurl = url.replace(/^(.+\.[a-z0-z]+)\.[a-z0-9]+$/i, '$1');
console.log('[STVBG]', vurl);
const resp = await fetch(vurl, {
method: 'HEAD',
});
console.log('[STVBG]', resp);
if (resp.ok) {
const v = document.createElement('video'); {
v.loop = true;
v.autoplay = true;
v.muted = true;
v.style.position = 'absolute';
v.style.height = '100%';
v.style.width = '100%';
v.style.objectFit = 'cover';
v.style.opacity = '0';
v.style.transition = '400ms linear';
v.src = vurl;
}
(customEl ?? el).append(v);
await delay(20);
v.style.opacity = '1';
while (true) {
try {
await v.play();
break;
} catch(ex) {
await wait(100);
}
}
await delay(450);
videoEl?.remove();
videoEl = v;
}
} else if (videoEl) {
videoEl.remove();
}
};
replaceBg();
const mo = new MutationObserver(debounce(replaceBg));
mo.observe(document.querySelector('#bg1'), { attributes:true });
mo.observe(document.querySelector('#bg_custom'), { attributes:true });
const replaceThumbs = async(muts = [])=>{
rtp = new Promise(async(resolve)=>{
for (const bg of [...document.querySelectorAll('#bg_menu_content .bg_example[bgfile]:not(.stvbg)')]) {
bg.classList.add('stvbg');
const url = bg.getAttribute('bgfile');
if (url.match(/^.+\.[a-z0-z]+\.[a-z0-9]+$/i)) {
const vurl = `/backgrounds/${url.replace(/^(.+\.[a-z0-z]+)\.[a-z0-9]+$/i, '$1')}`;
console.log('[STVBG]', vurl);
const resp = await fetch(vurl, {
method: 'HEAD',
});
console.log('[STVBG]', resp);
if (resp.ok) {
const v = document.createElement('video'); {
v.loop = true;
// v.autoplay = true;
v.muted = true;
v.style.position = 'absolute';
v.style.height = '100%';
v.style.width = '100%';
v.style.objectFit = 'cover';
v.style.zIndex = '-1';
v.src = vurl;
}
bg.style.background = 'none';
bg.addEventListener('pointerover', ()=>v.play());
bg.addEventListener('pointerout', ()=>v.pause());
bg.append(v);
// await delay(20);
// while (true) {
// try {
// await v.play();
// break;
// } catch(ex) {
// await wait(100);
// }
// }
}
}
}
resolve();
});
};
let rtq = 0;
let rtp = Promise.resolve();
replaceThumbs();
const queueReplaceThumbs = async()=>{
rtq++;
await rtp;
if (--rtq == 0) replaceThumbs();
};
const moThumbs = new MutationObserver(debounce(queueReplaceThumbs));
moThumbs.observe(document.querySelector('#bg_menu_content'), { childList:true, subtree:true });
})();
$(document).ready(function () {
const addSettings = () => {
const html = `
<div class="vbg--settings">
<div class="inline-drawer">
<div class="inline-drawer-toggle inline-drawer-header">
<b>Video Backgrounds</b>
<div class="inline-drawer-icon fa-solid fa-circle-chevron-down down"></div>
</div>
<div class="inline-drawer-content" style="font-size:small;">
<h2>How To Use</h2>
<ol style="padding-left: 1.5em;">
<li>
Use your file explorer to place the video file you want to use into SillyTavern's backgrounds folder:
<pre>.../SillyTavern/data/vdbackgrounds/</pre>
</li>
<li>
Place any image file with the exact same name (including the video's filename extension) plus the image files extension into the backgrounds folder.<br>
Example:<br>
video file: <code>MyVideo.mp4</code><br>
image file: <code>MyVideo.mp4.jpg</code><br>
</li>
<li>
Start / reload SillyTavern and select the image file as your background.
</li>
</ol>
</div>
</div>
</div>
`;
$('#extensions_settings').append(html);
};
addSettings();
});