Repository navigation
Expand file tree
/
Copy pathpixiv-bookmark-milestone-analytics.js
More file actions
172 lines (150 loc) · 5.73 KB
/
Copy pathpixiv-bookmark-milestone-analytics.js
File metadata and controls
172 lines (150 loc) · 5.73 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// ==UserScript==
// @name Pixiv Bookmark Milestone Analytics
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Display a dashboard table of times when artworks reach 1000 bookmark milestones. The script must remain running to collect statistics accurately.
// @author cro
// @match https://www.pixiv.net/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=pixiv.net
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// ==/UserScript==
/* jshint esversion: 6 */
(function() {
'use strict';
GM_addStyle(".scrolltable { display:block; border-spacing:0px; display:block; overflow:auto; height: 400px; }");
GM_addStyle(".scrolltable td { min-width: 100px; width: 100px; }");
let time_interval = 1000 * 60;
let milestone_interval = 1000;
let fetch_headers = {
"headers": {
"accept": "application/json",
"accept-language": "en-US,en;q=0.9,ja;q=0.8",
"sec-ch-ua": "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
},
"referrer": "https://www.pixiv.net/dashboard",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
};
let settings_key = "cro_pixiv_bma_settings";
let settings = {
order_asc: false,
};
Object.assign(settings, JSON.parse(GM_getValue(settings_key, "{}")));
let data_key = "cro_pixiv_bma";
let data = JSON.parse(GM_getValue(data_key, "{}"));
let table = document.createElement("table");
table.id = data_key;
table.classList.add('scrolltable');
let order_button = document.createElement("button");
let order_button_set_text = () => void(order_button.innerText = `ORDER: ${settings.order_asc ? "ASC" : "DESC"}`);
order_button_set_text();
let get_latest_data = function() {
return fetch("https://www.pixiv.net/ajax/dashboard/works/illust/request_strategy?lang=en", fetch_headers);
};
let initial_record = function(work) {
return {
title: work.illust.title,
thumbnail: work.illust.url,
last_milestone: 0,
milestones: [],
};
};
let update_record = function(record, bookmarks) {
let last_milestone = Math.floor(bookmarks / milestone_interval) * milestone_interval;
if (last_milestone <= record.last_milestone) {
return;
}
record.last_milestone = last_milestone;
record.milestones.push([last_milestone, Date.now()]);
};
let update_data = function(pixiv_data) {
pixiv_data.body.data.works.forEach(function(work, index)
{
work.illust = pixiv_data.body.thumbnails.illust[index];
if (!(work.workId in data))
{
data[work.workId] = initial_record(work);
}
update_record(data[work.workId], work.bookmarkCount);
});
GM_setValue(data_key, JSON.stringify(data));
};
let update_table = function() {
let scrollTop = table.scrollTop;
let scrollLeft = table.scrollLeft;
table.innerHTML = "";
let ids = Object.keys(data);
ids = ids.sort((a, b) => b - a);
for (let id of ids)
{
let record = data[id];
let row = table.insertRow();
let cell = row.insertCell();
let link = document.createElement('a');
link.href = `/artworks/${id}`;
let thumbnail = document.createElement('img');
thumbnail.src = record.thumbnail;
thumbnail.height = 50;
link.append(thumbnail);
cell.append(link);
cell.style.minWidth = 50;
cell = row.insertCell();
link = document.createElement('a');
link.href = `/artworks/${id}`;
link.append(record.title);
cell.append(link);
let milestones = [...record.milestones];
if (settings.order_asc == false) {
milestones.reverse();
}
for (let [count, timestamp] of milestones)
{
row.insertCell().innerHTML = `${count} - ${new Date(timestamp).toLocaleString()}`;
}
}
table.scrollTop = scrollTop;
table.scrollLeft = scrollLeft;
};
let inject_table = function() {
if (window.location.pathname != "/dashboard") {
return;
}
let maybe_table = document.querySelector(`#${data_key}`);
if (!maybe_table) {
let dock = document.querySelector("body");
dock.prepend(table);
dock.prepend(order_button);
}
};
order_button.onclick = function() {
settings.order_asc = !settings.order_asc;
GM_setValue(settings_key, JSON.stringify(settings));
order_button_set_text();
update_table();
};
let update = function(pixiv_data) {
update_data(pixiv_data);
update_table();
};
let process = function() {
if (window.location.pathname != "/dashboard") {
return;
}
get_latest_data()
.then(x => x.json())
.then(x => update(x));
};
process();
let interval = setInterval(process, time_interval);
setInterval(inject_table, 1000);
})();