Update src/libpoketube/init/pages-api.js
This commit is contained in:
parent
dadfbc2183
commit
5bc604c420
@ -223,8 +223,7 @@ app.get("/api/stats/optout", (req, res) => {
|
|||||||
</body>
|
</body>
|
||||||
</html>`)
|
</html>`)
|
||||||
})
|
})
|
||||||
|
app.get("/api/stats", (req, res) => {
|
||||||
app.get("/api/stats", (req, res) => {
|
|
||||||
const view = (req.query.view || "").toString()
|
const view = (req.query.view || "").toString()
|
||||||
|
|
||||||
// JSON view – explicit: /api/stats?view=json
|
// JSON view – explicit: /api/stats?view=json
|
||||||
@ -336,12 +335,14 @@ app.get("/api/stats/optout", (req, res) => {
|
|||||||
<h2>API usage</h2>
|
<h2>API usage</h2>
|
||||||
<p class="note">
|
<p class="note">
|
||||||
• Human view (this page): <code><a href="/api/stats?view=human">/api/stats?view=human</a></code><br>
|
• Human view (this page): <code><a href="/api/stats?view=human">/api/stats?view=human</a></code><br>
|
||||||
• JSON view (for scripts/tools): <code><a href="/api/stats?view=json">/api/stats?view=json</a></code>
|
• JSON view (for scripts/tools): <code><a href="/api/stats?view=json">/api/stats?view=json</a></code><br>
|
||||||
|
• Opt out for this browser: <code><a href="/api/stats/optout">/api/stats/optout</a></code>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const TELEMETRY_ON = ${telemetryOn ? "true" : "false"};
|
const TELEMETRY_ON = ${telemetryOn ? "true" : "false"};
|
||||||
|
const OPT_KEY = "poke_stats_optout";
|
||||||
|
|
||||||
const statsNote = document.getElementById("stats-note");
|
const statsNote = document.getElementById("stats-note");
|
||||||
const statsList = document.getElementById("stats-list");
|
const statsList = document.getElementById("stats-list");
|
||||||
@ -353,54 +354,69 @@ app.get("/api/stats/optout", (req, res) => {
|
|||||||
statsList.innerHTML = "";
|
statsList.innerHTML = "";
|
||||||
topVideos.innerHTML = "<li>No data (telemetry disabled).</li>";
|
topVideos.innerHTML = "<li>No data (telemetry disabled).</li>";
|
||||||
} else {
|
} else {
|
||||||
fetch("/api/stats?view=json")
|
// respect per-browser opt-out for the human UI
|
||||||
.then(function (res) { return res.json(); })
|
var optedOut = false;
|
||||||
.then(function (data) {
|
try {
|
||||||
var videos = data.videos || {};
|
optedOut = localStorage.getItem(OPT_KEY) === "1";
|
||||||
var browsers = data.browsers || {};
|
} catch (e) {
|
||||||
var os = data.os || {};
|
// ignore localStorage errors; treat as not opted out
|
||||||
var totalUsers = data.totalUsers || 0;
|
}
|
||||||
|
|
||||||
var videoCount = Object.keys(videos).length;
|
if (optedOut) {
|
||||||
|
statsNote.textContent =
|
||||||
|
"You have opted out of anonymous stats in this browser. Poke will not load stats for you here.";
|
||||||
|
statsList.innerHTML = "";
|
||||||
|
topVideos.innerHTML = "<li>Opt-out active (no stats loaded).</li>";
|
||||||
|
} else {
|
||||||
|
fetch("/api/stats?view=json")
|
||||||
|
.then(function (res) { return res.json(); })
|
||||||
|
.then(function (data) {
|
||||||
|
var videos = data.videos || {};
|
||||||
|
var browsers = data.browsers || {};
|
||||||
|
var os = data.os || {};
|
||||||
|
var totalUsers = data.totalUsers || 0;
|
||||||
|
|
||||||
statsNote.textContent = "";
|
var videoCount = Object.keys(videos).length;
|
||||||
statsList.innerHTML = "";
|
|
||||||
|
|
||||||
var summaryItems = [
|
statsNote.textContent = "";
|
||||||
"Anonymous users (unique local IDs): " + totalUsers,
|
statsList.innerHTML = "";
|
||||||
"Videos with recorded views: " + videoCount,
|
|
||||||
"Browser types seen: " + Object.keys(browsers).length,
|
|
||||||
"OS families seen: " + Object.keys(os).length
|
|
||||||
];
|
|
||||||
|
|
||||||
summaryItems.forEach(function (text) {
|
var summaryItems = [
|
||||||
var li = document.createElement("li");
|
"Anonymous users (unique local IDs): " + totalUsers,
|
||||||
li.textContent = text;
|
"Videos with recorded views: " + videoCount,
|
||||||
statsList.appendChild(li);
|
"Browser types seen: " + Object.keys(browsers).length,
|
||||||
});
|
"OS families seen: " + Object.keys(os).length
|
||||||
|
];
|
||||||
|
|
||||||
var videoKeys = Object.keys(videos);
|
summaryItems.forEach(function (text) {
|
||||||
if (videoKeys.length === 0) {
|
|
||||||
topVideos.innerHTML = "<li>No stats recorded yet.</li>";
|
|
||||||
} else {
|
|
||||||
topVideos.innerHTML = "";
|
|
||||||
videoKeys.forEach(function (id) {
|
|
||||||
var li = document.createElement("li");
|
var li = document.createElement("li");
|
||||||
var a = document.createElement("a");
|
li.textContent = text;
|
||||||
a.href = "/watch?v=" + encodeURIComponent(id);
|
statsList.appendChild(li);
|
||||||
a.textContent = id;
|
|
||||||
li.appendChild(a);
|
|
||||||
li.appendChild(document.createTextNode(" – " + videos[id] + " views"));
|
|
||||||
topVideos.appendChild(li);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
})
|
var videoKeys = Object.keys(videos);
|
||||||
.catch(function () {
|
if (videoKeys.length === 0) {
|
||||||
statsNote.textContent =
|
topVideos.innerHTML = "<li>No stats recorded yet.</li>";
|
||||||
"Could not load stats (maybe they are disabled or there was an error).";
|
} else {
|
||||||
statsList.innerHTML = "";
|
topVideos.innerHTML = "";
|
||||||
topVideos.innerHTML = "<li>Error loading data.</li>";
|
videoKeys.forEach(function (id) {
|
||||||
});
|
var li = document.createElement("li");
|
||||||
|
var a = document.createElement("a");
|
||||||
|
a.href = "/watch?v=" + encodeURIComponent(id);
|
||||||
|
a.textContent = id;
|
||||||
|
li.appendChild(a);
|
||||||
|
li.appendChild(document.createTextNode(" – " + videos[id] + " views"));
|
||||||
|
topVideos.appendChild(li);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(function () {
|
||||||
|
statsNote.textContent =
|
||||||
|
"Could not load stats (maybe they are disabled or there was an error).";
|
||||||
|
statsList.innerHTML = "";
|
||||||
|
topVideos.innerHTML = "<li>Error loading data.</li>";
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
@ -487,7 +503,8 @@ app.get("/api/stats/optout", (req, res) => {
|
|||||||
<h2>API usage</h2>
|
<h2>API usage</h2>
|
||||||
<p class="note">
|
<p class="note">
|
||||||
• Human view (stats UI): <code><a href="/api/stats?view=human">/api/stats?view=human</a></code><br>
|
• Human view (stats UI): <code><a href="/api/stats?view=human">/api/stats?view=human</a></code><br>
|
||||||
• JSON view (for scripts/tools): <code><a href="/api/stats?view=json">/api/stats?view=json</a></code>
|
• JSON view (for scripts/tools): <code><a href="/api/stats?view=json">/api/stats?view=json</a></code><br>
|
||||||
|
• Opt out for this browser: <code><a href="/api/stats/optout">/api/stats/optout</a></code>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
@ -495,6 +512,7 @@ app.get("/api/stats/optout", (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.get("/avatars/:v", async function (req, res) {
|
app.get("/avatars/:v", async function (req, res) {
|
||||||
var url = `https://yt3.ggpht.com/${req.params.v}`;
|
var url = `https://yt3.ggpht.com/${req.params.v}`;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user