-
Notifications
You must be signed in to change notification settings - Fork 34
/
DiscussedOnMeta.user.js
214 lines (194 loc) · 6.08 KB
/
DiscussedOnMeta.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// ==UserScript==
// @name Discussed on Meta
// @description For questions and answers, displays info if it's discussed on Meta. On arrow mouseover, displays the Meta posts
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 4.1.13
//
// @match https://*.stackoverflow.com/*
// @match https://*.serverfault.com/*
// @match https://*.superuser.com/*
// @match https://*.askubuntu.com/*
// @match https://*.mathoverflow.net/*
// @match https://*.stackapps.com/*
// @match https://*.stackexchange.com/*
//
// @exclude https://stackoverflowteams.com/*
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
// @exclude *chat.*
// @exclude *blog.*
// @exclude *meta.*
// @exclude */tour
//
// @connect *
// @connect self
// @connect stackoverflow.com
// @connect serverfault.com
// @connect superuser.com
// @connect askubuntu.com
// @connect mathoverflow.com
// @connect stackapps.com
// @connect stackexchange.com
//
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/se-ajax-common.js
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/common.js
//
// @grant GM_xmlhttpRequest
// ==/UserScript==
/* globals StackExchange */
/// <reference types="./globals" />
'use strict';
const siteMetaHostname = metaUrl.replace('https://', '');
const doSearch = (postElem, searchQuery, searchDomain) => {
const pid = postElem.dataset.answerid || postElem.dataset.questionid;
const searchUrl = `https://${searchDomain}/search?tab=newest&q=${searchQuery}&deleted=any`;
//console.log(`Searching ${searchDomain} for post ${pid}`, searchUrl);
ajaxPromise(searchUrl).then(function (data) {
const count = Number($('.results-header h2, .fs-body3', data).first().text().replace(/[^\d]+/g, ''));
if (!count) return;
const results = $('.js-search-results .js-post-summary', data);
const lastMentioned = results.first().find('.relativetime').text();
const lastPermalink = results.first().find('a').first().attr('href');
results.find('a').attr('href', (i, v) => 'https://' + searchDomain + v).attr('target', '_blank');
// Create meta posts container
const metaName = searchDomain === 'meta.stackexchange.com' ? 'Meta Stack Exchange' : 'Meta';
const metaPosts = $(`
<div class="meta-mentioned" target="_blank">
<a href="${searchUrl}" target="_blank">${count} posts</a> on ${metaName},
last seen <a href="https://${searchDomain}${lastPermalink}" target="_blank">${lastMentioned}</a>
<span class="meta-mentions-toggle"></span>
</div>`).insertBefore(postElem);
// Append search results
$(`<div class="meta-mentions"></div>`).append(results).appendTo(metaPosts);
});
};
// Append styles
addStylesheet(`
.meta-mentioned {
position: relative;
width: 100%;
height: 38px;
margin: 0 0 15px;
padding: 10px 12px;
background: var(--yellow-050);
border: 1px solid var(--yellow-200);
box-sizing: border-box;
z-index: 1;
}
#answers .meta-mentioned {
margin: 15px 0 0;
}
.meta-mentioned * {
box-sizing: border-box;
}
.meta-mentioned:hover {
z-index: 100;
}
.meta-mentions-toggle {
position: absolute;
top: 4px;
right: 4px;
display: block;
width: 28px;
height: 32px;
cursor: pointer;
}
.meta-mentions-toggle:before {
content: "";
position: absolute;
top: 0;
right: 0;
bottom: 4px;
left: 0;
background: var(--black-100);
border-radius: 3px;
}
.meta-mentions-toggle:after {
content: "";
position: absolute;
top: 10px;
left: 8px;
display: block;
width: 0;
height: 0;
border-style: solid;
border-width: 8px 6.5px 0 6.5px;
border-color: var(--black-500) transparent transparent transparent;
}
.meta-mentions-toggle:hover:before {
background: var(--black-400);
}
.meta-mentions-toggle:hover:after {
border-color: var(--white) transparent transparent transparent;
}
.meta-mentions-toggle:hover + .meta-mentions,
.meta-mentions:hover {
display: block;
}
.meta-mentions {
display: none;
position: absolute;
top: 100%;
max-width: calc(100% + 2px);
width: calc(100% + 2px);
min-height: 40px;
margin-left: -13px;
padding: 12px;
background: var(--white);
border: 1px solid var(--black-150);
box-shadow: 5px 5px 5px -3px rgba(0, 0, 0, 0.1);
z-index: 1;
}
.meta-mentions .js-post-summary {
max-width: 100%;
padding: 10px 0;
}
.meta-mentions .js-post-summary:last-child {
border: none;
}
.s-post-summary .s-post-summary--content-title {
margin-bottom: 6px;
line-height: 1;
}
.meta-mentions .js-post-summary .s-post-summary--content-title a {
font-size: 14px;
}
.meta-mentions .js-post-summary .s-post-summary--content-excerpt {
font-size: 12px;
line-height: 1.3;
-webkit-line-clamp: 2;
}
.meta-mentions .js-post-summary .js-tags .js-post-tag-list-wrapper {
margin-bottom: 0;
}
.meta-mentions .js-post-summary .js-tags .post-tag {
font-size: 10px;
pointer-events: none;
}
.meta-mentions .s-post-summary--stats {
margin-top: 3px;
}
.meta-mentions .s-post-summary--stats .s-post-summary--stats-item {
font-size: 10px;
}
`); // end stylesheet
// On script run
(function init() {
// Up to first three questions and answers, unless user is a moderator
$('.question, .js-question, .answer, .js-answer').slice(0, isModerator() ? 20 : 3).each(function () {
const pid = this.dataset.answerid || this.dataset.questionid;
// Ignore if too short, will generate lots of false positives
if (pid <= 99999) return;
// Build search query
const searchQuery = encodeURIComponent(`url://${location.hostname}/*/${pid}`);
// Check if post is discussed on site meta
doSearch(this, searchQuery, siteMetaHostname);
// If we are on Stack Overflow, also check if post is discussed on MSE
if (location.hostname === 'stackoverflow.com') {
doSearch(this, searchQuery, 'meta.stackexchange.com');
}
});
})();