forked from fireattack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyandere_additional_functionality.user.js
More file actions
298 lines (266 loc) · 10.4 KB
/
yandere_additional_functionality.user.js
File metadata and controls
298 lines (266 loc) · 10.4 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// ==UserScript==
// @name Yande.re additional functionality
// @namespace org.fireattack.yandere
// @description
// @match *://yande.re/*
// @version 4.4
// ==/UserScript==
// Yande.re has Cookie.get and Cookie.put, derived from cookie-js lib
/* function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
} */
function reloadPage() {
window.location.reload();
}
var doNotTransfer;
var a = localStorage.getItem('doNotTransfer');
if (a) {
doNotTransfer = a.split(' ');
} else {
doNotTransfer = ['duplicate', 'fixed', 'wallpaper', 'possible_duplicate'];
jQuery
.ajax({
url: "/tag.json",
data: {
type: 6
},
dataType: "json",
})
.done(resp => {
doNotTransfer.push(...resp.map(ele => ele.name));
localStorage.setItem('doNotTransfer', doNotTransfer.join(' '));
});
}
const defaultOldTagsToBeRemoved = ['possible_duplicate'];
function ajaxGetPostObj(id, lookForChild = false) {
var deferred = jQuery.Deferred();
let index = (lookForChild)? 1 : 0;
jQuery
.ajax({
url: "/post.json",
data: {
tags: "parent:" + id
},
dataType: "json",
})
.done(function (resp) {
deferred.resolve(resp[index]);
});
return deferred.promise();
}
function transferTagsPrepare(postObj, targetID, oldTagsToBeRemoved = defaultOldTagsToBeRemoved) {
let tags = (Array.isArray(postObj.tags))? postObj.tags : postObj.tags.split(" ");
tags.push(postObj.rating);
tags = tags.filter(el => !doNotTransfer.includes(el));
return toBeUpdated = {
id: targetID,
tags: tags.join(" "),
old_tags: oldTagsToBeRemoved.join(" ")
};
}
function batchTransferTags(method, post_ids) {
if (!post_ids) { // By default, apply to all
post_ids = Object.keys(Post.posts._object);
}
var toBeUpdatedArr = [],
promises = [];
post_ids.forEach(post_id => {
var post = Post.posts.get(post_id);
switch (method) {
case 'toparent':
if (post.parent_id)
toBeUpdatedArr.push(transferTagsPrepare(post, post.parent_id));
break;
case 'fromparent':
if (post.parent_id) {
var promise = ajaxGetPostObj(post.parent_id).done(obj => {
toBeUpdatedArr.push(transferTagsPrepare(obj, post_id));
});
promises.push(promise);
}
break;
case 'fromchild':
if (post.has_children) {
var promise = ajaxGetPostObj(post_id, true).done(obj => {
toBeUpdatedArr.push(transferTagsPrepare(obj, post_id));
});
promises.push(promise);
}
break;
}
});
jQuery.when.apply(jQuery, promises).done(() => {
Post.update_batch(toBeUpdatedArr);
});
}
function batchTransferPoolshipToParent() {
var toBeUpdatedArr = [];
for (var id in Post.posts._object) {
var post = Post.posts._object[id];
if (post.parent_id && post.pool_posts && Object.keys(post.pool_posts._object).length === 1) {
var poolID = Object.keys(post.pool_posts._object)[0];
toBeUpdatedArr.push({
id: id,
tags: "-pool:" + poolID,
old_tags: ""
});
toBeUpdatedArr.push({
id: post.parent_id,
tags: "pool:" + poolID + ":" + post.pool_posts._object[poolID].sequence,
old_tags: ""
});
}
}
Post.update_batch(toBeUpdatedArr);
}
// 魔改 TagScript.run
TagScript.run = function (post_ids, tag_script, finished) {
if (!Object.isArray(post_ids)) post_ids = $A([post_ids]);
let re = /\$(\S+)\$/;
let match = re.exec(tag_script);
if (match) {
batchTransferTags(match[1], post_ids);
} else {
var commands = TagScript.parse(tag_script) || [] //预处理tagscript
var posts = new Array; //posts = 批量处理的obj
post_ids.each(function (post_id) { // 对于每个选中的posts
var post = Post.posts.get(post_id)
var old_tags = post.tags.join(" ") //获取原有tag
commands.each(function (x) {
post.tags = TagScript.process(post.tags, x)
})
posts.push({
id: post_id,
old_tags: old_tags, //移除所有的老的先
tags: post.tags.join(" ") //加新的
});
});
notice("Updating " + posts.length + (post_ids.length == 1 ? " post" : " posts"));
Post.update_batch(posts, finished);
}
};
if (/post\/show/i.test(window.location.href)) {
var post_id = window.location.href.match(/\d+/)[0];
var statusNotice = document.querySelectorAll('.status-notice');
if (statusNotice) {
for (let notice of statusNotice) {
if (notice.textContent.includes('child post')) {
var childID = notice.querySelector('a:last-child').textContent;
let node = document.createElement('a');
node.href = '#';
node.textContent = '[Transfer tags]';
node.onclick = function () {
ajaxGetPostObj(childID).done(obj => {
Post.update_batch([transferTagsPrepare(obj, post_id)], reloadPage);
});
};
notice.appendChild(node);
break;
}
}
}
}
if (/pool\/show/i.test(window.location.href)) {
let newButton = document.createElement('input');
newButton.type = 'button';
newButton.value = 'Transfer tags to parent';
newButton.style.verticalAlign = 'middle';
newButton.onclick = () => {
batchTransferTags('to');
};
let newButton2 = document.createElement('input');
newButton2.type = 'button';
newButton2.value = 'Transfer poolship to parent';
newButton2.style.verticalAlign = 'middle';
newButton2.onclick = () => {
batchTransferPoolshipToParent();
};
let myH4 = document.querySelector('h4');
myH4.style.display = 'inline';
myH4.style.verticalAlign = 'middle';
let insertPoint = document.querySelector('#pool-show');
let insertBefore = insertPoint.childElements()[1];
insertPoint.insertBefore(document.createTextNode(' '), insertBefore);
insertPoint.insertBefore(newButton, insertBefore);
insertPoint.insertBefore(document.createTextNode(' '), insertBefore);
insertPoint.insertBefore(newButton2, insertBefore);
}
// Pool update (edit) page modification
if (/pool\/update/i.test(window.location.href)) {
// Make pool_name textarea longer
var poolName = document.querySelector('#pool_name');
poolName.style.width = '1500px';
// Add a button to quickly change pool name from Exhentai style to Yande.re style
let newButton = document.createElement('input');
newButton.type = 'button';
newButton.value = 'Fix name';
newButton.style.verticalAlign = 'middle';
newButton.onclick = () => {
var myMatch = poolName.value.match(/\[(.+)\] *(.+?)( \(.+\))*$/);
if (myMatch) {
var desc = document.querySelector('#pool_description');
//desc.value = poolName.value + '\n' + desc.value;
if (!desc.value) {
desc.value = poolName.value;
}
poolName.value = myMatch[1] + ' - ' + myMatch[2];
}
document.querySelector('#pool_is_public').checked = false;
document.querySelector('#pool_is_active').checked = false;
};
let insertPoint = document.querySelector('div#content');
let myH3 = document.querySelector('h3');
myH3.style.display = 'inline';
myH3.style.verticalAlign = 'middle';
let insertBefore = insertPoint.querySelector('form');
insertPoint.insertBefore(document.createTextNode(' '), insertBefore);
insertPoint.insertBefore(newButton, insertBefore);
}
// Post/pool index modification
if (/post$|post\?|post\/$|pool\/show/i.test(window.location.href)) {
// Feature: restore PNG image's direct link URL and resolution display to original, instead of its JPEG version's
let postLis = document.querySelectorAll('ul#post-list-posts > li');
postLis.forEach(li => {
let directLink = li.querySelector('a.directlink');
if (/.*re\/jpeg\/.*/i.test(directLink)) { // You can directly test DOM element??
directLink.href = directLink.href.replace(/^(.+)\/jpeg\/(.+)\.jpg$/i, '$1/image/$2.png');
let id = li.id.match(/\d+/)[0];
let width = Post.posts._object[id].width;
let height = Post.posts._object[id].height;
directLink.lastChild.textContent = `${width} x ${height}`;
}
});
}
if (window.location.href === 'https://yande.re/post/moderate') {
document.querySelectorAll('#content > div:nth-child(4) > form > table > tbody > tr > td.checkbox-cell').forEach(td => {
text = td.textContent;
let uploader = text.match(/Uploaded by (.+?) /)[1];
let flagger = text.match(/Reason: .+ \((.+?)\)/)[1];
if (uploader === flagger) {
td.style.border = '5px solid yellow';
}
});
}
// Pool index modification
if (/pool\/show/i.test(window.location.href)) {
// Feature: add some paras for "Index View" link
let lis = document.querySelectorAll('ul#subnavbar > li');
// When i is >= lis.length, it returns undefined and therefore stops the loop. Tricky..
//From: https://stackoverflow.com/a/6260833/3939155
for (let i = 0, li; li = lis[i]; ++i) {
if (li.textContent.includes('Index View')) {
li.firstChild.href += '+limit%3A100+holds%3Aall';
break;
}
}
}
// User page modification
if (/user\/show/i.test(window.location.href)) {
// Feature: add a link to show all deleted posts of this user
let node1 = document.querySelector("a[href*='/post?tags=user']");
let node2 = document.querySelector("a[href*='deleted_index']");
let myHTML = " (<a href=" + node1.href + "+deleted:true>index show</a>)";
node2.parentNode.innerHTML += myHTML;
}