forked from forkphorus/cat-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra-metadata.user.js
122 lines (102 loc) · 3.17 KB
/
extra-metadata.user.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// ==UserScript==
// @name Extra Metadata for Scratch 3
// @description Adds extra metadata to the Scratch 3 interface
// @version 0.1.3
// @namespace https://github.com/forkphorus/cat-plus
// @homepageURL https://github.com/forkphorus/cat-plus#readme
// @match https://scratch.mit.edu/projects/*
// ==/UserScript==
(function() {
'use strict';
/**
* @param {Date} date Date
* @returns {string} Human readable date.
*/
function dateToString(date) {
return date.toLocaleString();
}
function handleProjectPage() {
if (location.pathname.includes('editor')) {
return;
}
/**
* @typedef {Object} ProjectData
* @property {Date} created
* @property {Date} modified
* @property {Date} shared
* @property {number} comments
*/
/**
* @type {ProjectData}
*/
var projectData = {};
/**
* @returns {string} The project ID, if any.
*/
function getProjectId() {
const re = location.pathname.match(/\/projects\/(\d*)/);
return re[1];
}
/**
* @param {ProjectData} id
*/
function getProjectData(id) {
return fetch('https://api.scratch.mit.edu/projects/' + id)
.then((r) => r.json())
.then((data) => {
return {
created: new Date(data.history.created),
shared: new Date(data.history.shared),
modified: new Date(data.history.modified),
comments: data.stats.comments,
};
});
}
function showDateInformation() {
let text = '';
text += 'Created: ' + dateToString(projectData.created) + '\n';
text += 'Shared: ' + dateToString(projectData.shared) + '\n';
text += 'Modified: ' + dateToString(projectData.modified);
alert(text);
}
function addDateElement() {
const shareDate = document.querySelector('.share-date');
if (!shareDate) return;
const dateContainer = shareDate.querySelector('span');
if (!dateContainer || dateContainer._extraMetadata) return;
dateContainer._extraMetadata = true;
dateContainer.textContent += ' ';
const button = document.createElement('a');
button.textContent = '\u2026';
button.title = 'More information about dates';
button.onclick = showDateInformation;
dateContainer.appendChild(button);
}
function addCommentsElement() {
const commentsHeader = document.querySelector('.comments-header');
if (!commentsHeader) return;
const commentsHeaderText = commentsHeader.querySelector('span');
if (!commentsHeaderText || commentsHeaderText._extraMetadata) return;
commentsHeaderText._extraMetadata = true;
commentsHeaderText.textContent += ' (' + projectData.comments + ')';
}
function updateInterface() {
addDateElement();
addCommentsElement();
}
const projectId = getProjectId();
if (!projectId) return;
getProjectData(projectId)
.then((data) => {
projectData = data;
updateInterface();
setInterval(updateInterface, 1000);
})
.catch((err) => {
// that's fine
});
}
window.addEventListener('load', function() {
handleProjectPage();
});
})();