-
Notifications
You must be signed in to change notification settings - Fork 26
/
mb_CHATLOGS-POWER-UP.user.js
126 lines (124 loc) · 5.33 KB
/
mb_CHATLOGS-POWER-UP.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
// ==UserScript==
// @name mb. CHATLOGS POWER-UP
// @version 2022.9.16
// @description chatlogs.metabrainz.org: swicth between #musicbrainz and #metabrainz channels; centre highlight message (for post permalink URL)
// @namespace https://github.com/jesus2099/konami-command
// @supportURL https://github.com/jesus2099/konami-command/labels/mb_CHATLOGS-POWER-UP
// @downloadURL https://github.com/jesus2099/konami-command/raw/master/mb_CHATLOGS-POWER-UP.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 2012-03-05; https://web.archive.org/web/20131103163408/userscripts.org/scripts/show/127580 / https://web.archive.org/web/20141011084021/userscripts-mirror.org/scripts/show/127580
// @icon data:image/gif;base64,R0lGODlhEAAQAKEDAP+/3/9/vwAAAP///yH/C05FVFNDQVBFMi4wAwEAAAAh/glqZXN1czIwOTkAIfkEAQACAwAsAAAAABAAEAAAAkCcL5nHlgFiWE3AiMFkNnvBed42CCJgmlsnplhyonIEZ8ElQY8U66X+oZF2ogkIYcFpKI6b4uls3pyKqfGJzRYAACH5BAEIAAMALAgABQAFAAMAAAIFhI8ioAUAIfkEAQgAAwAsCAAGAAUAAgAAAgSEDHgFADs=
// @grant none
// @include /^https?:\/\/chatlogs\.metabrainz\.org\/(brainzbot|libera)\//
// @run-at document-start
// ==/UserScript==
"use strict";
var userjs = "j2userjs127580";
var date = self.location.pathname.match(/\/(\d{4})[-/](\d{2})[-/](\d{2})\b/);
if (date) date = date[1] + "-" + date[2] + "-" + date[3];
var loc = self.location.href.match(/https?:\/\/chatlogs\.metabrainz\.org\/(brainzbot|libera)\/([^/]+)\/|mbja/);
if (loc) {
var cat = loc[2];
var mbCHATLOGSPOWERUPinterval = setInterval(function() {
if (document.head && document.body) {
clearInterval(mbCHATLOGSPOWERUPinterval);
var css = document.createElement("style");
css.setAttribute("type", "text/css");
document.head.appendChild(css);
css = css.sheet;
// remove top black bar which takes much space
css.insertRule("header#Site-Header { display: none; }", 0);
css.insertRule("header#Log-Header { padding: 0px; }", 0);
css.insertRule("header#Log-Header { top: 0px; }", 0);
// toolbar
css.insertRule("div#" + userjs + "toolbar { position: fixed; bottom: 0; right: 0; background-color: #ccc; padding: 2px 0 0 4px; border: 2px solid #eee; border-width: 2px 0 0 2px; z-index: 50; }", 0);
css.insertRule("body { padding-bottom: .5em; }", 0);
var ctt = document.createElement("div");
ctt.setAttribute("id", userjs + "toolbar");
/* cross linking */
separate(ctt);
var tgt = (cat.match(/^musicbrainz$/) ? "meta" : "music") + "brainz";
var tgtA = createA("#" + tgt, (self.location.pathname.match(/\/search\/$/) ? self.location.href : self.location.pathname).replace(/\/(meta|music)brainz\//, "/" + tgt + "/"));
if (cat == "musicbrainz") {
ctt.appendChild(document.createTextNode("#musicbrainz"));
} else {
ctt.appendChild(tgtA);
}
separate(ctt);
if (cat == "metabrainz") {
ctt.appendChild(document.createTextNode("#metabrainz"));
} else {
ctt.appendChild(tgtA);
}
/* prev./next day */
if (date) {
separate(ctt);
ctt.appendChild(createA("« prev.", shiftDate(-1)));
separate(ctt);
ctt.appendChild(createA("next »", shiftDate(+1)));
}
if (document.body.firstChild) {
document.body.insertBefore(ctt, document.body.firstChild);
} else {
document.body.appendChild(ctt);
}
if (self.location.search.match(/\bmsg=\d+/)) {
// Bring permalinked post back on screen
var center_highlight_message = setInterval(function() {
var highlight_message = document.querySelector("ul#Log > li.highlight");
if (highlight_message) {
clearInterval(center_highlight_message);
if (!checkVisible(highlight_message, 50)) {
highlight_message.scrollIntoView({block: "center", behavior: "smooth"});
}
}
}, 1234);
}
}
}, 123);
}
function shiftDate(shift) {
var sdate = (new Date(date));
sdate.setDate(sdate.getDate() + shift);
var yyyy = zeroPad(sdate.getFullYear(), 4);
var mm = zeroPad(sdate.getMonth() + 1, 2);
var dd = zeroPad(sdate.getDate(), 2);
return self.location.pathname.match(/[^\d]+/) + yyyy + "-" + mm + "-" + dd + "/";
}
function zeroPad(i, cols) {
var str = "" + i;
while (str.length < cols) {
str = "0" + str;
}
return str;
}
function createA(text, link, title) {
var a = document.createElement("a");
if (link && typeof link == "string") {
a.setAttribute("href", link);
} else {
if (link && typeof link == "function") {
a.addEventListener("click", link, false);
}
a.style.setProperty("cursor", "pointer");
a.style.setProperty("text-decoration", "underline");
}
if (title) { a.setAttribute("title", title); }
a.appendChild(typeof text == "string" ? document.createTextNode(text) : text);
return a;
}
function separate(cont, sep) {
if (cont.firstChild) cont.appendChild(document.createTextNode(sep ? sep : " | "));
}
function checkVisible(elm, threshold, mode) {
// awesome function by Tokimon https://stackoverflow.com/a/5354536/2236179
threshold = threshold || 0;
mode = mode || "visible";
var rect = elm.getBoundingClientRect();
var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
var above = rect.bottom - threshold < 0;
var below = rect.top - viewHeight + threshold >= 0;
return mode === "above" ? above : (mode === "below" ? below : !above && !below);
}