-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_content.js
86 lines (79 loc) · 3.09 KB
/
accounts_content.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
async function scrapeModeratorNotes(url) {
const response = await fetch(url);
if (response.ok) {
const pageText = await response.text();
var doc = document.implementation.createHTMLDocument();
doc.open();
doc.write(pageText);
doc.close();
return getNotesFromPage(doc);
} else {
console.log(`Error getting response for ${url}`)
return null;
}
}
function getNotesFromPage(doc) {
const noteNodes = doc.querySelectorAll("div.report-notes__item")
if (noteNodes.length == 0) {
return [];
}
var res = [];
for (const note of noteNodes.values()) {
var author = note.querySelector("span.username a").innerText;
var text = note.querySelector("div.report-notes__item__content").innerText.trim();
var time = note.querySelector("time").getAttribute("datetime")
res.push({author:author, text:text, time:time})
}
return res;
}
function saveNotes(notes, key) {
var storedObject = {
checkDate: new Date().toUTCString(),
id: key,
notes: notes
}
window.localStorage.setItem(key, JSON.stringify(storedObject))
}
async function timeout(ms) {return new Promise(resolve=>window.setTimeout(resolve, ms))}
async function showModeratorNotes () {
const pendingDivs = document.querySelectorAll(".batch-table__row--attention div.batch-table__row__content")
for(let div of pendingDivs.values()) {
let a = div.querySelector("a.account__display-name")
let href = a.getAttribute("href");
let key = /[^/]*$/.exec(href)[0];
let cachedNotes = window.localStorage.getItem(key);
let notes = null;
if (cachedNotes) {
cachedNotes = JSON.parse(cachedNotes);
notes = cachedNotes.notes;
//console.log(`loaded notes from storage`);
//console.log(cachedNotes);
const checkDate = new Date(cachedNotes.checkDate);
const invalidTime = (notes == null || notes.length == 0) ? 5 * 60 * 1000 : 12 * 60 * 60 * 1000;
if (new Date() - checkDate > invalidTime) {
notes = null;
}
}
if (notes == null) {
console.log(`loading notes from ${href}`)
notes = await scrapeModeratorNotes(href, key);
saveNotes(notes, key);
await timeout(500);
}
if (notes.length > 0) {
for (i = 0; i < notes.length; i++) {
let note = notes[i];
let d = document.createElement("div");
d.innerText = `${note.author}: ${note.text} (${new Date(note.time).toLocaleDateString()})`;
d.classList.add("batch-table__row__content__quote", "report-notes__item__content")
div.appendChild(d);
}
}
}
}
if (window.location.pathname == "/admin/accounts") {
showModeratorNotes().then(()=>console.log("showed notes"))
} else if (/\/admin\/accounts\/[^/]+$/.test(window.location.pathname)) {
let key = /[^/]*$/.exec(window.location.pathname)[0];
saveNotes(getNotesFromPage(document), key);
}