|
| 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 | +}; |
0 commit comments