-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixeldrain.user.js
More file actions
324 lines (283 loc) · 12.6 KB
/
Pixeldrain.user.js
File metadata and controls
324 lines (283 loc) · 12.6 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// ==UserScript==
// @name Pixeldrain Mods
// @namespace http://tampermonkey.net/
// @version 0.11.0
// @description Saves the current time of a video on Pixeldrain.com and does other stuff as well
// @author fides
// @match https://pixeldrain.com/u/*
// @match https://pixeldrain.com/l/*
// @match https://www.patreon.com/posts/*
// @grant GM_registerMenuCommand
// ==/UserScript==
// TODO:
// Remove 5 Second Save Interval When Using Real Time Saves
(function () {
let stateCheck = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(stateCheck);
'use strict';
console.log("Pixeldrain Mods script started.");
// Variables
let guiContainer = null;
let autoSaveInterval = parseInt(localStorage.getItem('autoSaveInterval')) || 5000; // Save interval
let volumeLevel = parseFloat(localStorage.getItem('volumeLevel')) || 1.0; // Video volume
let defVolState = localStorage.getItem('defVolState') || "disabled"; // Should the volume be adjusted automatically
let realTimeUpState = localStorage.getItem('realTimeUpState') || "disabled"; //
let savingState = localStorage.getItem('savingState') || "enabled"; //
const isMobile = /Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
let videoElement = document.querySelector('video');
let autoSaveTimer = setInterval(saveVideoTime, autoSaveInterval);
// Check if a video is present on the page
if (videoElement && savingState === "enabled") {
console.log("Video element found on page. Video time tracking enabled.");
// Load the saved video time
loadVideoTime();
// Set the volume level
videoElement.volume = volumeLevel;
if (!isMobile) {
// Add spacebar focus toggle feature
addSpacebarFocusToggle();
}
}
else {
console.error("ERROR: Couldn't load video time.\nREASON: No video element found on page or savingState option is disabled.");
}
// Function to add spacebar focus toggle feature
function addSpacebarFocusToggle() {
const videoElements = document.querySelectorAll('video');
for (const video of videoElements) {
video.addEventListener('focus', () => {
document.addEventListener('keydown', handleSpacebar, true);
});
video.addEventListener('blur', () => {
document.removeEventListener('keydown', handleSpacebar, true);
});
}
}
function handleSpacebar(event) {
if (event.keyCode === 32 && document.activeElement.tagName === 'VIDEO') {
event.preventDefault();
}
}
// Function to save the current time of the video
function saveVideoTime() {
checkRealTimeUpState();
// This makes the 'Video Time Tracking' setting work without refreshing the page.
let savingState = localStorage.getItem('savingState');
if (videoElement && savingState === "enabled") {
// Get the current video time
var currentTime = Math.floor(videoElement.currentTime);
// Get the unique key based on the URL
var key = getKeyFromURL();
// Save the current time in local storage with the unique key
localStorage.setItem(key, currentTime);
console.log("Video time saved:", currentTime, "seconds");
} else {
console.error("ERROR: Couldn't save video time.\nREASON: Saving state is disabled.");
}
}
// Function to toggle real-time updates
function checkRealTimeUpState() {
let realTimeUpState = localStorage.getItem('realTimeUpState'); // Check realTimeUpState item
if (realTimeUpState === "enabled") {
// Add event listeners
videoElement.addEventListener('play', saveVideoTime);
videoElement.addEventListener('pause', saveVideoTime);
videoElement.addEventListener('timeupdate', saveVideoTime);
} else {
// Remove event listeners if already added
videoElement.removeEventListener('play', saveVideoTime);
videoElement.removeEventListener('pause', saveVideoTime);
videoElement.removeEventListener('timeupdate', saveVideoTime);
}
}
// Function to load the saved video time
function loadVideoTime() {
// Get the unique key based on the URL
var key = getKeyFromURL();
// Retrieve the saved video time from local storage using the unique key
var savedTime = localStorage.getItem(key);
// Check if a saved time exists
if (savedTime) {
// Set the video time to the saved time
document.querySelector('video').currentTime = parseInt(savedTime);
console.log("Loaded saved video time:", savedTime, "seconds");
}
}
// Function to get the unique key from the URL
function getKeyFromURL() {
// Get the pathname of the URL (excluding the hostname and protocol)
var pathname = window.location.pathname;
// Use the pathname as the key
return pathname.replace(/\//g, ''); // Remove slashes
}
// Function to create the UI
function createUI() {
console.log("UI opened.");
if (guiContainer) {
document.body.removeChild(guiContainer);
guiContainer = null;
console.log("UI closed.");
return;
}
const root = createElement("div", {
id: "root",
styles: {
zIndex: "9999", position: "fixed", top: "20px", right: "20px",
background: "rgba(60, 60, 60, 0.2)", backdropFilter: "blur(10px) saturate(180%)",
borderRadius: "1rem", padding: "1em", width: "300px", height: "400px",
cursor: "move"
},
events: { mousedown: startDrag }
});
const container = createElement("div", {
styles: {
position: "fixed", top: "50px", right: "0px", background: "rgb(40, 40, 40)",
borderRadius: "0 0 1rem 1rem", padding: "1em", width: "300px", height: "350px",
cursor: "default", overflow: "auto"
}
});
const closeButton = createElement("a", {
innerHTML: `<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-x" viewBox="0 0 16 16">
<path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708"/>
</svg>`,
styles: {
position: "fixed", top: "8px", right: "8px", background: "transparent",
border: "none", width: "32px", height: "32px", color: "#999", fontWeight: "bold",
transition: "color 0.15s ease", cursor: "pointer"
},
events: {
click: () => {
document.body.removeChild(root);
guiContainer = null;
},
mouseenter: () => closeButton.style.color = "#fff",
mouseleave: () => closeButton.style.color = "#999"
}
});
const settings = [
{ text: "Persistent Volume", key: 'defVolState', stateKey: 'volumeLevel', callback: setVolume },
{ text: "Real-Time saves", key: 'realTimeUpState', callback: checkRealTimeUpState },
{ text: "Video Time Tracking", key: 'savingState' }
];
settings.forEach(({ text, key, stateKey, callback }) => {
const settingDiv = createSetting(text, key, stateKey, callback);
container.appendChild(settingDiv);
});
root.appendChild(closeButton);
root.appendChild(container);
document.body.appendChild(root);
guiContainer = root;
function startDrag(e) {
if (e.target === root) {
let offsetX = e.clientX - root.getBoundingClientRect().left;
let offsetY = e.clientY - root.getBoundingClientRect().top;
function moveElement(e) {
root.style.left = e.clientX - offsetX + "px";
root.style.top = e.clientY - offsetY + "px";
}
function stopDrag() {
document.removeEventListener("mousemove", moveElement);
document.removeEventListener("mouseup", stopDrag);
}
document.addEventListener("mousemove", moveElement);
document.addEventListener("mouseup", stopDrag);
}
const guiElement = document.getElementById('root');
// Function to get the element's position relative to the viewport
function getGuiCoordinates() {
const boundingRect = guiElement.getBoundingClientRect();
return {
top: boundingRect.top + window.pageYOffset,
left: boundingRect.left + window.pageXOffset
};
}
// Call the function to get coordinates whenever you need them
const coordinates = getGuiCoordinates();
console.log(`GUI element is at: top - ${coordinates.top}, left - ${coordinates.left}`);
}
function createElement(tag, { id, innerHTML, textContent, styles, events } = {}) {
const el = document.createElement(tag);
if (id) el.id = id;
if (innerHTML) el.innerHTML = innerHTML;
if (textContent) el.textContent = textContent;
if (styles) Object.assign(el.style, styles);
if (events) Object.keys(events).forEach(e => el.addEventListener(e, events[e]));
return el;
}
function createSetting(text, key, stateKey, callback) {
const div = createElement("div", {
textContent: text,
styles: {
position: "relative", textAlign: "center", lineHeight: "40px",
marginTop: "10px", width: "calc(100% - 80px)", height: "40px",
background: "#222", borderRadius: "10px 0px 0px 10px", userSelect: "none"
}
});
const btn = createElement("a", {
textContent: "Enabled",
styles: {
position: "absolute", right: "-80px", width: "80px", height: "40px",
background: "#383838", borderRadius: "0px 10px 10px 0px", color: "#00FF00",
textAlign: "center", lineHeight: "40px", cursor: "pointer", userSelect: "none"
},
events: {
click: () => {
toggleButtonState(btn, key);
if (callback) callback(parseFloat(localStorage.getItem(stateKey)) || 1.0);
}
}
});
let state = localStorage.getItem(key);
if (state === "disabled") {
btn.textContent = "Disabled";
btn.style.color = "#FF0000";
}
div.appendChild(btn);
return div;
}
}
// Function to toggle button state and save it to localStorage
function toggleButtonState(button, key) {
if (button.textContent === "Enabled") {
button.textContent = "Disabled";
button.style.color = "#FF0000"; // Red color for "disabled" text
localStorage.setItem(key, "disabled");
console.log("Setting", key, "to 'disabled'.");
} else {
button.textContent = "Enabled";
button.style.color = "#00FF00"; // Green color for "enabled" text
localStorage.setItem(key, "enabled");
console.log("Setting", key, "to 'enabled'.");
}
}
// Function to set the volume
function setVolume(volumeLevel) {
// Select all audio and video elements on the page
const mediaElements = document.querySelectorAll("audio, video");
// Loop through each media element
mediaElements.forEach(element => {
// Set the volume level
element.volume = volumeLevel;
});
console.log("Volume set to:", volumeLevel);
}
if (videoElement) {
videoElement.addEventListener('volumechange', handleVolumeChange);
}
else {
console.warn("No video element found. Volume change handler not attached.");
}
function handleVolumeChange() {
const newVolumeLevel = videoElement.volume;
localStorage.setItem('volumeLevel', newVolumeLevel.toString());
console.log("Volume changed:", newVolumeLevel);
}
// Register user menu command to open the UI
GM_registerMenuCommand("Open/Close Settings", () => {
createUI();
});
// Get a reference to your movable GUI element
}
}, 1);
})();