forked from MadaraUchiha/star-thumn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpando.user.js
147 lines (120 loc) · 4.74 KB
/
expando.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
// ==UserScript==
// @name Star Thumbnail Expando
// @resource STYLE https://rawgit.com/MadaraUchiha/star-thumn/master/style.css
// @version 0.2.8
// @match *://chat.stackexchange.com/*
// @match *://chat.stackoverflow.com/*
// @match *://chat.meta.stackexchange.com/*
// @grant GM_addStyle
// @grant GM_getResourceText
// ==/UserScript==
(function(){window.addEventListener('load', function() {
'use strict';
var cssTxt = GM_getResourceText ("STYLE");
GM_addStyle (cssTxt);
var stars = document.getElementById('starred-posts');
var thumbs = document.createElement('div');
thumbs.id = 'thumbs';
stars.appendChild(thumbs);
if (!String.prototype.includes) {
String.prototype.includes = function(needle) {
return this.indexOf(needle) !== -1;
};
}
function xhr(url, method, data) {
data = data || {};
method = method || "GET";
var serialize = function(obj) {
return Object.keys(obj).map(function(key) {
return key + '=' + obj[key];
}).join('&');
};
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.onload = function() { resolve(req.responseText); };
req.onerror = reject;
req.open(method, url);
if (method !== 'GET') {
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
req.send(serialize(data));
});
}
function isOnebox(msgId) {
return xhr('/message/' + msgId, 'POST', {plain: true})
.then(function(response) {
// vvvvvvvvvvvvvv just URL vvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvv just linked image vvvvvvvvvvvvvvvvv
return /^(?:!?https?:\/\/[^ ]+\.(?:jpe?g|png)|\[[^]]+\]\(!?https?:\/\/[^ ]+\.(?:jpe?g|png)\))$/.test(response);
});
}
function isLiOnebox(li) {
return isOnebox(li.id.replace('summary_', ''));
}
function toThumbnail(li) {
// Purely so that the current scripts won't break!
var newLi = document.createElement('li');
newLi.id = li.id;
var figure = document.createElement('figure');
var imgA = li.querySelector('a').cloneNode(true);
var starSpan = li.querySelector('span').cloneNode(true);
var voteSpan = starSpan.querySelector('.img.vote');
voteSpan.textContent = starSpan.classList.contains('owner-star') ? '✪' : '★';
voteSpan.classList.remove('img');
var img = new Image();
if (imgA.href.includes('googledrive.com')) {
var hasThumb = imgA.href.includes('_thumb');
if (hasThumb) {
img.src = imgA.href;
imgA.href = imgA.href.replace('_thumb', '');
}
else {
img.src = imgA.href.replace(/(\.[^.]+$)/, '_thumb$1');
}
}
else {
img.src = imgA.href;
}
// Not dots! This is a single character! vvv
if (!imgA.href.includes(imgA.textContent.replace('…', ''))) { imgA.title = imgA.textContent; }
imgA.textContent = '';
imgA.appendChild(img);
figure.appendChild(imgA);
figure.appendChild(starSpan);
newLi.appendChild(figure);
return newLi;
}
function emptyElement(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
function renderAllThumbnails() {
emptyElement(thumbs);
var thumbnailWorthy = [].filter.call(stars.querySelectorAll('a'), function justThoseWithImageLinks(link) {
return /(?:jpe?g|png)$/.test(link.href);
})
.map(function getParent(link) {
return link.parentNode;
});
Promise.all(thumbnailWorthy)
.then(function(thumbnailWorthyArray) {
return thumbnailWorthyArray.filter(isLiOnebox);
})
.then(function prepareGround(confirmedThumbnails) {
confirmedThumbnails.forEach(function(li) {
li.classList.add('hidden');
});
return confirmedThumbnails;
})
.then(function(confirmedThumbnails) {
return confirmedThumbnails.map(toThumbnail);
})
.then(function(images) {
images.forEach(thumbs.appendChild.bind(thumbs));
});
}
(new MutationObserver(renderAllThumbnails))
.observe(stars.parentNode, {childList: true, subTree: true, characterData: true, attributes: true});
setTimeout(renderAllThumbnails, 0);
});})();