forked from ndm13/plex-token-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
103 lines (88 loc) · 3.84 KB
/
script.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
function fetchPlexPin(product, clientId) {
const data = new URLSearchParams();
data.append("strong", "false");
data.append("X-Plex-Product", product);
data.append("X-Plex-Client-Identifier", clientId);
return fetch("https://plex.tv/api/v2/pins", {
method: "POST",
headers: {
"Accept": "application/json"
},
body: data
})
.then(r => r.json());
}
function fetchPlexAuthToken(clientId, pin) {
let data2 = new URLSearchParams();
data2.append("code", pin.code);
data2.append("X-Plex-Client-Identifier", clientId);
return fetch("https://plex.tv/api/v2/pins/" + pin.id + "?" + data2.toString(), {
headers: {
"Accept": "application/json"
}
})
.then(r => r.json());
}
document.addEventListener('DOMContentLoaded', () => {
// Initialize form logic
document.forms[0].onsubmit = e => e.preventDefault();
document.getElementById("random-id").onclick = () => {
document.getElementById("client-id").value = crypto.randomUUID();
}
// Handle generation
let checkForPin = null;
const infobox = document.getElementById("infobox");
const pinContainer = document.getElementById("pin-container");
const tokenContainer = document.getElementById("token-container");
const generateButton = document.getElementById("generate");
const cancelButton = document.getElementById("cancel");
cancelButton.onclick = () => {
window.clearInterval(checkForPin);
pinContainer.style.display = "none";
tokenContainer.style.display = "none";
infobox.style.display = "block";
cancelButton.style.display = "none";
generateButton.innerText = "Generate PIN";
};
generateButton.onclick = () => {
// Refresh check if clicked again
if (checkForPin !== null)
window.clearInterval(checkForPin);
let product = document.getElementById("product").value;
let clientId = document.getElementById("client-id").value;
if (product === '' || clientId === '') return;
fetchPlexPin(product, clientId)
.then(pin => {
// Clarify reclick intent
generateButton.innerText = "New PIN";
// Set QR code
pinContainer.querySelector("img").setAttribute('src', pin.qr);
// Display PIN
document.getElementById("pin").innerText = pin.code;
infobox.style.display = "none";
tokenContainer.style.display = "none";
pinContainer.style.display = "block";
// Update link to include PIN
pinContainer.querySelector("a").setAttribute("href", `https://plex.tv/link?pin=${pin.code}`);
// Display Cancel
cancelButton.style.display = "block";
checkForPin = window.setInterval(() => {
fetchPlexAuthToken(clientId, pin)
.then(token => {
// If we're going to expire before the next pull, make the user generate again
if (token.expiresIn < 5) {
window.clearInterval(checkForPin);
pinContainer.style.display = "none";
}
// If there's no token, keep waiting
if (!token.authToken) return;
pinContainer.style.display = "none";
cancelButton.style.display = "none";
document.getElementById("plex-token").innerText = token.authToken;
tokenContainer.style.display = "block";
window.clearInterval(checkForPin);
})
}, 5000);
});
};
})