-
Notifications
You must be signed in to change notification settings - Fork 26
/
mb_PRINT-ALL-PAGES.user.js
171 lines (169 loc) · 8.36 KB
/
mb_PRINT-ALL-PAGES.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
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
// ==UserScript==
// @name mb. PRINT ALL PAGES
// @version 2022.9.26.1
// @description musicbrainz.org: Print your complete collections to make your shopping lists or check lists. Maybe it will work on more than just collections, in the future.
// @namespace https://github.com/jesus2099/konami-command
// @supportURL https://github.com/jesus2099/konami-command/labels/mb_PRINT-ALL-PAGES
// @downloadURL https://github.com/jesus2099/konami-command/raw/master/mb_PRINT-ALL-PAGES.user.js
// @author jesus2099
// @licence CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/
// @licence GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @since 2021-03-02; https://community.metabrainz.org/t/script-for-printing-collections/521437
// @icon data:image/gif;base64,R0lGODlhEAAQAKEDAP+/3/9/vwAAAP///yH/C05FVFNDQVBFMi4wAwEAAAAh/glqZXN1czIwOTkAIfkEAQACAwAsAAAAABAAEAAAAkCcL5nHlgFiWE3AiMFkNnvBed42CCJgmlsnplhyonIEZ8ElQY8U66X+oZF2ogkIYcFpKI6b4uls3pyKqfGJzRYAACH5BAEIAAMALAgABQAFAAMAAAIFhI8ioAUAIfkEAQgAAwAsCAAGAAUAAgAAAgSEDHgFADs=
// @require https://github.com/jesus2099/konami-command/raw/de88f870c0e6c633e02f32695e32c4f50329fc3e/lib/SUPER.js?version=2022.3.24.224
// @grant none
// @include /^https?:\/\/(\w+\.)?musicbrainz\.org\/collection\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}(\?order=.*)?$/
// @run-at document-ready
// ==/UserScript==
"use strict";
var userjs = {
id: GM_info.script.name.replace(/\.\s/, "_").replace(/\s/g, "-"),
};
var form = document.querySelector("div#content h2 + form[method='post']");
if (form) {
var lastPage;
// locate the pagination toolbar or create it if none
var pagination = form.querySelector("nav ul.pagination");
if (!pagination) {
lastPage = 1;
pagination = form.insertBefore(document.createElement("nav"), form.firstChild).appendChild(createTag("ul", {a: {class: "pagination"}}));
pagination.appendChild(document.createElement("li"));
} else {
lastPage = pagination.querySelector("ul.pagination > li:nth-last-child(3) > a");
if (lastPage) {
lastPage = parseInt(lastPage.getAttribute("href").match(/\d+$/)[0], 10);
}
}
// put the little button instead of the first page disabled Previous button
replaceChildren(createTag("li", {}, createTag(
"a",
{
a: {title: GM_info.script.name + " version " + GM_info.script.version},
s: {background: "#FF6"},
e: {click: preparePage}
}, [
createTag("img", {a: {alt: "loading", src: GM_info.script.icon}, s: {verticalAlign: "text-bottom"}}),
" Load all pages for print"
]
)), pagination.firstChild);
}
function preparePage(event) {
var css = {
all: document.createElement("style"),
print: document.createElement("style")
};
css.all.setAttribute("type", "text/css");
css.print.setAttribute("type", "text/css");
css.print.setAttribute("media", "print");
document.head.appendChild(css.all);
document.head.appendChild(css.print);
css.all = css.all.sheet;
css.print = css.print.sheet;
// hide stuff from print
// ---------------------
css.print.insertRule("div#page, div#content { padding: 0; }", 0); // full width
css.print.insertRule("body > div:not(#page) { display: none; }", 0); // hide all except #page (#content, #sidebar)
css.print.insertRule("div#sidebar { display: none; }", 0); // hide #sidebar from #page
css.print.insertRule("div#content > :not(.description):not(form):not(.collectionheader) { display: none; }", 0); // hide tabs and sub headers from #content
css.print.insertRule(".subheader > .small { display: none; }", 0); // hide "See all your collections" link
css.print.insertRule("div#content h2 ~ form[method='post'] > :not(table) { display: none; }", 0); // hide merge button from form
css.print.insertRule("th > a > span { display: none !important; }", 0); // hide inactive sort icons inside table column headers
css.print.insertRule("." + userjs.id + "toolbar { display: none; }", 0); // hide this script toolbar
css.print.insertRule("td > span.mp { background-color: initial !important; }", 0); // disable highlighting for pending edits
css.print.insertRule("a, a:visited { color: initial; }", 0); // display entity links without the usual blue highlighting
// hide stuff from both screen and print
// -------------------------------------
// hide columns from print:
// TODO: Add checkboxes in <thead> to let user say what columns to hide from print
// hide checkboxes
var checkboxColumnIndex = form.querySelector("table.tbl > thead > tr > th.checkbox-cell");
if (checkboxColumnIndex) {
checkboxColumnIndex = checkboxColumnIndex.cellIndex + 1;
console.debug(checkboxColumnIndex);
css.all.insertRule("thead th:nth-child(" + checkboxColumnIndex + "), tbody td:nth-child(" + checkboxColumnIndex + ") { display: none; }", 0);
}
// hide ratings
var ratingColumnIndex = form.querySelector("table.tbl > thead > tr > th.rating");
if (ratingColumnIndex) {
ratingColumnIndex = ratingColumnIndex.cellIndex + 1;
css.all.insertRule("thead th:nth-child(" + ratingColumnIndex + "), tbody td:nth-child(" + ratingColumnIndex + ") { display: none; }", 0);
}
// hide label comments (including mod pending, thus the :nth-child)
var labelColumnIndex = form.querySelector("table.tbl > thead > tr > th a[href*='order='][href$='label']");
if (labelColumnIndex) {
labelColumnIndex = getParent(labelColumnIndex, "th").cellIndex + 1;
css.all.insertRule("tbody td:nth-child(" + labelColumnIndex + ") span.comment { display: none; }", 0);
}
// hide caa icons (only)
css.all.insertRule("a[href$='/cover-art'] { display: none; }", 0);
// hide irrelevant pagination buttons
css.all.insertRule("div#content > form > nav > ul { display: none; }", 0);
// hide SPOT_AC
css.all.insertRule(".name-variation { background: unset; border: unset; }", 0);
// hide mb_FUNKEY-ILLUSTRATED-RECORDS bigpics
var bigpics = document.querySelector("div.jesus2099userjs154481bigbox");
if (bigpics) {
// remove bigpics to skip further image loadings
removeNode(bigpics);
}
// disable collection highlighter
var collectionHighlights = document.querySelectorAll("[class*='collectionHighlighter']");
for (var i = 0; i < collectionHighlights.length; i++) {
collectionHighlights[i].className = collectionHighlights[i].className.replace(/\bcollectionHighlighter(Box|Item|Row)\b/g);
}
if (lastPage !== 1) {
appendPage(2, lastPage);
} else {
finalTouch();
}
}
function appendPage(page, last) {
var loading = document.getElementById(userjs.id + "loading");
if (!loading) {
loading = createTag("div", {a: {id: userjs.id + "loading"}, s: {textAlign: "center"}}, [
createTag("img", {a: {alt: "loading", src: "/static/images/icons/loading.gif"}, s: {verticalAlign: "text-bottom"}}),
" ",
document.createElement("span")
]);
addAfter(loading, document.querySelector("div#content > h2"));
}
replaceChildren(document.createTextNode("Loading page " + page + "/" + last + "…"), loading.lastChild);
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function() {
if (this.status === 200) {
var responseDOM = document.createElement("html");
responseDOM.innerHTML = this.responseText;
// append each page releases to expanding release table of page 1
var releaseTable = document.querySelector("div#content table.tbl > tbody");
var pageReleaseRows = responseDOM.querySelectorAll("div#content table.tbl > tbody > tr");
for (var r = 0; r < pageReleaseRows.length; r++) {
releaseTable.appendChild(pageReleaseRows[r].cloneNode(true));
}
// determine next page and last page
var nextPage = responseDOM.querySelector("ul.pagination > li:last-of-type > a");
if (
nextPage
&& (nextPage = parseInt(nextPage.getAttribute("href").match(/\d+$/)[0], 10))
&& nextPage > page
&& nextPage <= last
) {
appendPage(nextPage, last);
} else {
// last page loaded
removeNode(loading);
finalTouch();
}
} else {
alert("Error " + this.status + "(" + this.statusText + ") while loading page " + page + ".");
}
});
xhr.open("GET", self.location.href + (self.location.href.indexOf("?") > 1 ? "&" : "?") + "page=" + page, true);
xhr.send(null);
}
function finalTouch() {
// reveal main artist name
var nameVariations = form.querySelectorAll("table.tbl > tbody > tr > td span.name-variation a");
for (var n = 0; n < nameVariations.length; n++) {
addAfter(createTag("span", {a: {class: "comment"}}, [" (", nameVariations[n].getAttribute("title").match(/(.+) – /)[1], ")"]), nameVariations[n]);
}
}