forked from tccoin/Jellyfin-Potplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userscript.js
93 lines (88 loc) · 3.09 KB
/
userscript.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
// ==UserScript==
// @name Jellyfin with Potplayer
// @version 0.1
// @description play video with Potplayer
// @author Tccoin
// @match http://192.168.123.9:8096/web/index.html
// ==/UserScript==
(function() {
'use strict';
let openPotplayer = async (itemid) => {
let userid = (await ApiClient.getCurrentUser()).Id;
ApiClient.getItem(userid, itemid).then(r => {
if (r.Path) {
let path = r.Path.replace(/\\/g, '/');
path = path.replace('volume4', '/192.168.123.9');
console.log(path);
window.open('potplayer://' + path)
} else {
ApiClient.getItems(userid, itemid).then(r => openPotplayer(r.Items[0].Id));
}
})
};
let bindEvent = async () => {
let buttons = [];
let retry = 6 + 1;
while (buttons.length == 0 && retry > 0) {
await new Promise(resolve => setTimeout(resolve, 500));
buttons = document.querySelectorAll('[data-mode=play],[data-mode=resume],[data-action=resume]');
retry -= 1;
}
for (let button of buttons) {
let nextElementSibling = button.nextElementSibling;
let parentElement = button.parentElement;
let outerHTML = button.outerHTML;
button.parentElement.removeChild(button);
let newButton = document.createElement('button');
if (nextElementSibling) {
parentElement.insertBefore(newButton, nextElementSibling);
} else {
parentElement.append(newButton);
}
newButton.outerHTML = outerHTML;
}
buttons = document.querySelectorAll('[data-mode=play],[data-mode=resume]');
for (let button of buttons) {
button.removeAttribute('data-mode');
button.addEventListener('click', e => {
e.stopPropagation();
let itemid = /id=(.*?)&serverId/.exec(window.location.hash)[1];
openPotplayer(itemid);
});
}
buttons = document.querySelectorAll('[data-action=resume]');
for (let button of buttons) {
button.removeAttribute('data-action');
button.addEventListener('click', e => {
e.stopPropagation();
let item = e.target;
while (!item.hasAttribute('data-id')) { item = item.parentNode }
let itemid = item.getAttribute('data-id');
openPotplayer(itemid);
});
}
};
let lazyload = () => {
let items = document.querySelectorAll('[data-src].lazy');
let y = document.scrollingElement.scrollTop;
let intersectinglist = [];
for (let item of items) {
let windowHeight = document.body.offsetHeight;
let itemTop = item.getBoundingClientRect().top;
let itemHeight = item.offsetHeight;
if (itemTop + itemHeight >= 0 && itemTop <= windowHeight) {
intersectinglist.push(item);
}
}
for (let item of intersectinglist) {
item.style.setProperty('background-image', `url("${item.getAttribute('data-src')}")`);
item.classList.remove('lazy');
item.removeAttribute('data-src');
};
};
window.addEventListener('scroll', lazyload);
window.addEventListener('viewshow', async() => {
bindEvent();
window.addEventListener('hashchange', bindEvent);
});
})();