Skip to content

Commit 12a6a04

Browse files
author
hoang.tran12
committed
ufs statistic graph
1 parent 1c4a724 commit 12a6a04

File tree

5 files changed

+132
-30
lines changed

5 files changed

+132
-30
lines changed

popup/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ function createScriptButton(script, isFavorite = false) {
208208
if (script.icon && typeof script.icon === "string") {
209209
// image icon
210210
if (
211-
script.icon.indexOf("http://") === 0 ||
212-
script.icon.indexOf("https://") === 0 ||
213-
script.icon.indexOf("data:image") === 0
211+
script.icon.startsWith("/") ||
212+
script.icon.startsWith("http://") ||
213+
script.icon.startsWith("https://") ||
214+
script.icon.startsWith("data:image")
214215
) {
215216
const icon = document.createElement("img");
216217
icon.classList.add("icon-img");

popup/tabs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const tabs = [
3030
...CATEGORY.search,
3131
scripts: [
3232
s._test,
33+
s._ufs_statistic,
3334
{
3435
id: "recommend_search_userscript",
3536
icon: "https://www.userscript.zone/favicon.ico",

scripts/_test.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,5 @@ export default {
99
vi: "",
1010
},
1111

12-
onDocumentStart: async () => {
13-
function blobToDataURL(blob, cb) {
14-
var a = new FileReader();
15-
a.readAsDataURL(blob);
16-
a.onload = function (e) {
17-
cb(e.target.result);
18-
};
19-
a.onerror = function (e) {
20-
cb(null);
21-
};
22-
}
23-
24-
window.blobUrlMap = new Map();
25-
const createObjectURLProxy = new Proxy(window.URL.createObjectURL, {
26-
apply: function (target, thisArg, argumentsList) {
27-
const blob = argumentsList[0];
28-
const blobUrl = target.apply(thisArg, argumentsList);
29-
window.blobUrlMap.set(blobUrl, blob);
30-
console.log(blobUrl, blob);
31-
blobToDataURL(blob, (dataUrl) => {
32-
console.log(dataUrl);
33-
});
34-
return blobUrl;
35-
},
36-
});
37-
window.URL.createObjectURL = createObjectURLProxy;
38-
},
12+
onDocumentEnd: async () => {},
3913
};

scripts/_ufs_statistic.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
export default {
2+
icon: "/assets/icon32.png",
3+
name: {
4+
en: "Useful-scripts statistic",
5+
vi: "Useful-scripts statistic",
6+
},
7+
description: {
8+
en: "Dev only",
9+
vi: "Dev only",
10+
},
11+
12+
whiteList: ["https://useful-script-statistic.glitch.me/log*"],
13+
14+
onDocumentEnd: async () => {
15+
javascript: (function () {
16+
UfsGlobal.DOM.injectScriptSrc(
17+
"https://cdn.jsdelivr.net/npm/chart.js",
18+
(success, fail) => {
19+
if (fail) {
20+
alert("ERROR: " + fail);
21+
return;
22+
}
23+
24+
let logData = document.body.innerText.split("\n");
25+
26+
// ======================== Per hours ========================
27+
28+
// Function to extract time from log data
29+
function extractTime(log) {
30+
let lastColon = log.lastIndexOf(":");
31+
let time = log.substring(0, lastColon);
32+
return new Date(time);
33+
}
34+
35+
// Count logs per hour
36+
const logsPerHour = Array(24).fill(0);
37+
logData.forEach((log) => {
38+
const hour = extractTime(log).getHours();
39+
logsPerHour[hour]++;
40+
});
41+
42+
// Chart.js code to draw the graph
43+
const canvas = document.createElement("canvas");
44+
canvas.style.cssText = "max-width: 900px; max-height: 300px;";
45+
document.body.prepend(canvas);
46+
const ctx = canvas.getContext("2d");
47+
const logTimelineChart = new Chart(ctx, {
48+
type: "line",
49+
data: {
50+
labels: Array.from({ length: 24 }, (_, i) => `${i}:00`),
51+
datasets: [
52+
{
53+
label: "Number of Logs",
54+
data: logsPerHour,
55+
borderColor: "rgb(75, 192, 192)",
56+
tension: 0.1,
57+
fill: false,
58+
},
59+
],
60+
},
61+
options: {
62+
responsive: true,
63+
interaction: {
64+
intersect: false,
65+
},
66+
scales: {
67+
y: {
68+
beginAtZero: true,
69+
},
70+
},
71+
},
72+
});
73+
74+
// ======================== Per script name ========================
75+
const scriptNameCount = new Map();
76+
logData.forEach((log) => {
77+
let lastColon = log.lastIndexOf(":");
78+
let lastArrow = log.lastIndexOf("->");
79+
let scriptName = log.substring(lastColon + 1, lastArrow - 1);
80+
scriptNameCount.set(
81+
scriptName,
82+
(scriptNameCount.get(scriptName) || 0) + 1
83+
);
84+
});
85+
const canvas2 = document.createElement("canvas");
86+
canvas2.style.cssText = "max-width: 300px; max-height: 300px;";
87+
document.body.prepend(canvas2);
88+
const ctx2 = canvas2.getContext("2d");
89+
const scriptNameChart = new Chart(ctx2, {
90+
type: "doughnut",
91+
data: {
92+
labels: Array.from(scriptNameCount.keys()),
93+
datasets: [
94+
{
95+
data: Array.from(scriptNameCount.values()),
96+
backgroundColor: [
97+
"rgb(255, 99, 132)",
98+
"rgb(255, 159, 64)",
99+
"rgb(255, 205, 86)",
100+
"rgb(75, 192, 192)",
101+
"rgb(54, 162, 235)",
102+
"rgb(153, 102, 255)",
103+
"rgb(201, 203, 207)",
104+
],
105+
hoverOffset: 4,
106+
},
107+
],
108+
},
109+
options: {
110+
plugins: {
111+
legend: {
112+
display: false,
113+
},
114+
},
115+
responsive: true,
116+
},
117+
});
118+
119+
// ======================== Per script name Per hour ========================
120+
}
121+
);
122+
})();
123+
},
124+
};

scripts/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ import auto_redirectLargestImageSrc from "./auto_redirectLargestImageSrc.js";
161161
import textToQrCode from "./textToQrCode.js";
162162
import insta_anonymousStoryViewer from "./insta_anonymousStoryViewer.js";
163163
import removeWebLimit from "./removeWebLimit.js";
164+
import _ufs_statistic from "./_ufs_statistic.js";
164165

165166
// inject badges
166167
const allScripts = {
@@ -338,6 +339,7 @@ const allScripts = {
338339
textToQrCode: addBadge(textToQrCode, BADGES.new),
339340
insta_anonymousStoryViewer: addBadge(insta_anonymousStoryViewer, BADGES.new),
340341
removeWebLimit: addBadge(removeWebLimit, BADGES.hot),
342+
_ufs_statistic: _ufs_statistic,
341343
};
342344

343345
// alert(Object.keys(allScripts).length);

0 commit comments

Comments
 (0)