-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules_oldreddit.js.html
356 lines (295 loc) · 21 KB
/
modules_oldreddit.js.html
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>modules/oldreddit.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
<script src="scripts/nav.js" defer></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav >
<input type="text" id="nav-search" placeholder="Search" />
<h2><a href="index.html">Home</a></h2><h2><a href="https://github.com/toolbox-team/reddit-moderator-toolbox" target="_blank" class="menu-item" >Github</a></h2><h2><a href="https://www.reddit.com/r/toolbox" target="_blank" class="menu-item" >Subreddit</a></h2><h3>Classes</h3><ul><li><a href="Module.html">Module</a><ul class='methods'><li data-type='method'><a href="Module.html#get">get</a></li><li data-type='method'><a href="Module.html#getEnabled">getEnabled</a></li><li data-type='method'><a href="Module.html#init">init</a></li><li data-type='method'><a href="Module.html#set">set</a></li><li data-type='method'><a href="Module.html#setEnabled">setEnabled</a></li></ul></li><li><a href="TBListener.html">TBListener</a><ul class='methods'><li data-type='method'><a href="TBListener.html#clear">clear</a></li><li data-type='method'><a href="TBListener.html#on">on</a></li><li data-type='method'><a href="TBListener.html#start">start</a></li><li data-type='method'><a href="TBListener.html#stop">stop</a></li></ul></li></ul><h3>Global</h3><ul><li><a href="global.html#DISPLAY_BOTTOM">DISPLAY_BOTTOM</a></li><li><a href="global.html#DISPLAY_CENTER">DISPLAY_CENTER</a></li><li><a href="global.html#FEEDBACK_NEGATIVE">FEEDBACK_NEGATIVE</a></li><li><a href="global.html#FEEDBACK_NEUTRAL">FEEDBACK_NEUTRAL</a></li><li><a href="global.html#FEEDBACK_POSITIVE">FEEDBACK_POSITIVE</a></li><li><a href="global.html#ModNotesBadge">ModNotesBadge</a></li><li><a href="global.html#ModNotesPager">ModNotesPager</a></li><li><a href="global.html#ModNotesPopup">ModNotesPopup</a></li><li><a href="global.html#NoteTableRow">NoteTableRow</a></li><li><a href="global.html#RandomFeedback">RandomFeedback</a></li><li><a href="global.html#RandomQuote">RandomQuote</a></li><li><a href="global.html#TBsettingsObject">TBsettingsObject</a></li><li><a href="global.html#actionButton">actionButton</a></li><li><a href="global.html#addModSubsToSidebar">addModSubsToSidebar</a></li><li><a href="global.html#addToSiteTable">addToSiteTable</a></li><li><a href="global.html#addTrophiesToSidebar">addTrophiesToSidebar</a></li><li><a href="global.html#alert">alert</a></li><li><a href="global.html#baseDomain">baseDomain</a></li><li><a href="global.html#browserName">browserName</a></li><li><a href="global.html#buildSha">buildSha</a></li><li><a href="global.html#buildType">buildType</a></li><li><a href="global.html#button">button</a></li><li><a href="global.html#checkForActions">checkForActions</a></li><li><a href="global.html#cleanSubredditName">cleanSubredditName</a></li><li><a href="global.html#clearCache">clearCache</a></li><li><a href="global.html#colorNameToHex">colorNameToHex</a></li><li><a href="global.html#contextTrigger">contextTrigger</a></li><li><a href="global.html#createDeferredProcessQueue">createDeferredProcessQueue</a></li><li><a href="global.html#daysToMilliseconds">daysToMilliseconds</a></li><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#debugInformation">debugInformation</a></li><li><a href="global.html#defaultNoteLabelValueToLabelType">defaultNoteLabelValueToLabelType</a></li><li><a href="global.html#delay">delay</a></li><li><a href="global.html#displayNotes">displayNotes</a></li><li><a href="global.html#domain">domain</a></li><li><a href="global.html#escapeHTML">escapeHTML</a></li><li><a href="global.html#fetchModSubs">fetchModSubs</a></li><li><a href="global.html#fetchNewsNotes">fetchNewsNotes</a></li><li><a href="global.html#figureOutMulti">figureOutMulti</a></li><li><a href="global.html#filterModdable">filterModdable</a></li><li><a href="global.html#getActions">getActions</a></li><li><a href="global.html#getAllModNotes">getAllModNotes</a></li><li><a href="global.html#getAnonymizedSettings">getAnonymizedSettings</a></li><li><a href="global.html#getCache">getCache</a></li><li><a href="global.html#getContextURL">getContextURL</a></li><li><a href="global.html#getLastVersion">getLastVersion</a></li><li><a href="global.html#getLatestModNote">getLatestModNote</a></li><li><a href="global.html#getModSubs">getModSubs</a></li><li><a href="global.html#getModlog">getModlog</a></li><li><a href="global.html#getRandomNumber">getRandomNumber</a></li><li><a href="global.html#getSetting">getSetting</a></li><li><a href="global.html#getSettingAsync">getSettingAsync</a></li><li><a href="global.html#getSettings">getSettings</a></li><li><a href="global.html#getSubmissionFullname">getSubmissionFullname</a></li><li><a href="global.html#getSubredditColors">getSubredditColors</a></li><li><a href="global.html#getTime">getTime</a></li><li><a href="global.html#getToolboxDevs">getToolboxDevs</a></li><li><a href="global.html#handleMessage">handleMessage</a></li><li><a href="global.html#handleTBThings">handleTBThings</a></li><li><a href="global.html#handleThing">handleThing</a></li><li><a href="global.html#hideModActionsThings">hideModActionsThings</a></li><li><a href="global.html#htmlDecode">htmlDecode</a></li><li><a href="global.html#htmlEncode">htmlEncode</a></li><li><a href="global.html#humaniseDays">humaniseDays</a></li><li><a href="global.html#init">init</a></li><li><a href="global.html#initialLoadPromise">initialLoadPromise</a></li><li><a href="global.html#isConfigValidVersion">isConfigValidVersion</a></li><li><a href="global.html#isEquivalent">isEquivalent</a></li><li><a href="global.html#isModSub">isModSub</a></li><li><a href="global.html#isNewModmail">isNewModmail</a></li><li><a href="global.html#isOldReddit">isOldReddit</a></li><li><a href="global.html#labelColors">labelColors</a></li><li><a href="global.html#labelNames">labelNames</a></li><li><a href="global.html#link">link</a></li><li><a href="global.html#listenerAliases">listenerAliases</a></li><li><a href="global.html#literalRegExp">literalRegExp</a></li><li><a href="global.html#makeCommentThread">makeCommentThread</a></li><li><a href="global.html#makeQueueOverlay">makeQueueOverlay</a></li><li><a href="global.html#makeSingleComment">makeSingleComment</a></li><li><a href="global.html#makeSubmissionEntry">makeSubmissionEntry</a></li><li><a href="global.html#makeUserSidebar">makeUserSidebar</a></li><li><a href="global.html#messageHandlers">messageHandlers</a></li><li><a href="global.html#millisecondsToDays">millisecondsToDays</a></li><li><a href="global.html#minutesToMilliseconds">minutesToMilliseconds</a></li><li><a href="global.html#modbarExists">modbarExists</a></li><li><a href="global.html#moveArrayItem">moveArrayItem</a></li><li><a href="global.html#newModmailSidebar">newModmailSidebar</a></li><li><a href="global.html#niceDateDiff">niceDateDiff</a></li><li><a href="global.html#notification">notification</a></li><li><a href="global.html#overlay">overlay</a></li><li><a href="global.html#pager">pager</a></li><li><a href="global.html#pagerForItems">pagerForItems</a></li><li><a href="global.html#parseComments">parseComments</a></li><li><a href="global.html#parser">parser</a></li><li><a href="global.html#popup">popup</a></li><li><a href="global.html#progressivePager">progressivePager</a></li><li><a href="global.html#purify">purify</a></li><li><a href="global.html#purifyObject">purifyObject</a></li><li><a href="global.html#regExpEscape">regExpEscape</a></li><li><a href="global.html#relativeTime">relativeTime</a></li><li><a href="global.html#reloadIframe">reloadIframe</a></li><li><a href="global.html#reloadToolbox">reloadToolbox</a></li><li><a href="global.html#remove">remove</a></li><li><a href="global.html#removeLastDirectoryPartOf">removeLastDirectoryPartOf</a></li><li><a href="global.html#removeQuotes">removeQuotes</a></li><li><a href="global.html#replaceAll">replaceAll</a></li><li><a href="global.html#replaceTokens">replaceTokens</a></li><li><a href="global.html#saneSort">saneSort</a></li><li><a href="global.html#saneSortAs">saneSortAs</a></li><li><a href="global.html#saveSettingsToBrowser">saveSettingsToBrowser</a></li><li><a href="global.html#searchProfile">searchProfile</a></li><li><a href="global.html#setCache">setCache</a></li><li><a href="global.html#setSetting">setSetting</a></li><li><a href="global.html#setSettingAsync">setSettingAsync</a></li><li><a href="global.html#settings">settings</a></li><li><a href="global.html#settingsToObject">settingsToObject</a></li><li><a href="global.html#shortVersion">shortVersion</a></li><li><a href="global.html#showNote">showNote</a></li><li><a href="global.html#sortBy">sortBy</a></li><li><a href="global.html#standardColors">standardColors</a></li><li><a href="global.html#stringToColor">stringToColor</a></li><li><a href="global.html#submissionFullnamesCache">submissionFullnamesCache</a></li><li><a href="global.html#tbRedditEvent">tbRedditEvent</a></li><li><a href="global.html#textFeedback">textFeedback</a></li><li><a href="global.html#timeConverterRead">timeConverterRead</a></li><li><a href="global.html#title_to_url">title_to_url</a></li><li><a href="global.html#toolboxVersion">toolboxVersion</a></li><li><a href="global.html#toolboxVersionName">toolboxVersionName</a></li><li><a href="global.html#typeNames">typeNames</a></li><li><a href="global.html#unescapeHTML">unescapeHTML</a></li><li><a href="global.html#unescapeJSON">unescapeJSON</a></li><li><a href="global.html#verifiedSettingsSave">verifiedSettingsSave</a></li><li><a href="global.html#watchForURLChanges">watchForURLChanges</a></li><li><a href="global.html#wrapWithLastValue">wrapWithLastValue</a></li><li><a href="global.html#zlibDeflate">zlibDeflate</a></li><li><a href="global.html#zlibInflate">zlibInflate</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">modules/oldreddit.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import $ from 'jquery';
import * as TBCore from '../tbcore.js';
import {Module} from '../tbmodule.jsx';
const self = new Module({
name: 'Old Reddit',
id: 'oldreddit',
alwaysEnabled: true,
}, () => {
// Looks like we are on old reddit. Activate!
if (TBCore.isOldReddit) {
setTimeout(() => {
thingCrawler();
window.addEventListener('TBNewThings', () => {
thingCrawler();
});
}, 500);
}
if (TBCore.isNewModmail) {
const $body = $('body');
$body.on('click', '.icon-user', () => {
setTimeout(() => {
newModmailSidebar();
}, 500);
});
$body.on('click', '.Thread__grouped', () => {
setTimeout(() => {
newModmailConversationAuthors();
}, 500);
});
window.addEventListener('TBNewPage', event => {
if (event.detail.pageType === 'modmailConversation') {
setTimeout(() => {
newModmailSidebar();
newModmailConversationAuthors();
}, 500);
}
});
}
});
export default self;
function dispatchApiEvent (element, object) {
const apiEvent = new CustomEvent('tbReddit', {detail: object});
try {
element.dispatchEvent(apiEvent);
} catch (error) {
self.log('Could not dispatch event', object);
}
}
/**
* Handles `thing` items as they become visible in the viewport.
* @function
* @param {IntersectionObserverEntry[]} entries
* @param {IntersectionObserver} observer
*/
function handleThing (entries, observer) {
entries.forEach(async entry => {
// The observer fires for everything on page load.
// This makes sure that we really only act on those items that are visible.
if (!entry.isIntersecting) {
return;
}
// Element is visible, we only want to handle it once. Stop observing.
observer.unobserve(entry.target);
const $thing = $(entry.target);
// If the element's parent is updated, sometimes it gets emitted again anyway.
// Check for existing containers and avoid adding duplicates.
if ($thing.find('> .entry > .tb-jsapi-container').length) {
return;
}
const info = await TBCore.getThingInfo($thing);
requestAnimationFrame(() => {
const $jsApiThingPlaceholder = $('<div class="tb-jsapi-container"></div>').appendTo(
$thing.find('.entry:first'),
);
$jsApiThingPlaceholder.append('<span data-name="toolbox">');
const jsApiThingPlaceholder = $jsApiThingPlaceholder[0];
$thing.find('.entry:first .author:first, .entry:first .tagline:first > span:contains("[deleted]")').after(
'<span class="tb-jsapi-author-container"></span>',
);
const $jsApiPlaceholderAuthor = $thing.find('.tb-jsapi-author-container');
$jsApiPlaceholderAuthor.append('<span data-name="toolbox">');
const jsApiPlaceholderAuthor = $jsApiPlaceholderAuthor[0];
if (!$jsApiThingPlaceholder.length) {
return;
}
if (info.kind === 'submission') {
if (!$jsApiThingPlaceholder.hasClass('tb-frontend-container')) {
const detailObject = {
type: 'TBpost',
data: {
author: info.author || '[deleted]',
id: info.id,
isRemoved: info.ham || info.spam,
permalink: `https://www.reddit.com/${
info.postlink.replace(/https?:\/\/...?\.reddit\.com\/?/, '').replace(/^\//, '')
}`,
subreddit: {
name: info.subreddit,
type: info.subredditType,
},
},
};
dispatchApiEvent(jsApiThingPlaceholder, detailObject);
}
// We don't want to send events for things already handled.
if (!$jsApiPlaceholderAuthor.hasClass('tb-frontend-container')) {
const detailObject = {
type: 'TBpostAuthor',
data: {
author: info.author || '[deleted]',
post: {
id: info.id,
},
subreddit: {
name: info.subreddit,
type: info.subredditType,
},
},
};
dispatchApiEvent(jsApiPlaceholderAuthor, detailObject);
}
}
if (info.kind === 'comment') {
// Comment
if (!$jsApiThingPlaceholder.hasClass('tb-frontend-container')) {
const detailObject = {
type: 'TBcommentOldReddit',
data: {
author: info.author || '[deleted]',
post: {
id: info.postID,
},
isRemoved: info.ham || info.spam,
id: info.id,
subreddit: {
name: info.subreddit,
type: info.subredditType,
},
},
};
dispatchApiEvent(jsApiThingPlaceholder, detailObject);
}
// Author
// We don't want to send events for things already handled.
if (!$jsApiPlaceholderAuthor.hasClass('tb-frontend-container')) {
const detailObject = {
type: 'TBcommentAuthor',
data: {
author: info.author || '[deleted]',
post: {
id: info.postID,
},
comment: {
id: info.id,
},
subreddit: {
name: info.subreddit,
type: info.subredditType,
},
},
};
dispatchApiEvent(jsApiPlaceholderAuthor, detailObject);
}
}
});
});
}
const viewportObserver = new IntersectionObserver(handleThing, {
rootMargin: '200px',
});
function thingCrawler () {
const $things = $('div.content .thing:not(.tb-seen) .entry').closest('.thing');
$things.each(function () {
requestAnimationFrame(() => {
$(this).addClass('tb-seen');
viewportObserver.observe(this);
});
});
}
function newModmailConversationAuthors () {
const $body = $('body');
const subreddit = $body.find('.ThreadTitle__community').text();
$body.find('.Thread__message:not(.tb-seen)').each(function () {
const $this = $(this);
$this.addClass('tb-seen');
const $jsApiThingPlaceholder = $(`
<span class="tb-jsapi-container">
<span data-name="toolbox"></span>
</span>
`).insertAfter($this.find('.Message__divider').eq(0));
const jsApiThingPlaceholder = $jsApiThingPlaceholder[0];
const authorHref = $this.find('.Message__header .Message__author').attr('href');
const author = authorHref === undefined ? '[deleted]' : authorHref.replace(/.*\/user\/([^/]+).*/, '$1');
const idDetails = $this.find('.m-link').attr('href').match(/\/mail\/.*?\/(.*?)\/(.*?)$/i);
const detailObject = {
type: 'TBmodmailCommentAuthor',
data: {
author,
post: {
id: idDetails[1],
},
comment: {
id: idDetails[2],
},
subreddit: {
name: subreddit,
},
},
};
dispatchApiEvent(jsApiThingPlaceholder, detailObject);
});
}
/**
* Makes sure to fire a jsAPI `TBuserHovercard` event for new modmail sidebar instances.
* @function
*/
function newModmailSidebar () {
setTimeout(() => {
const $body = $('body');
if ($body.find('.ThreadViewer').length) {
const $modmailSidebar = $body.find(
'.ThreadViewer__infobar:not(.tb-seen), .ThreadViewerHeader__infobar:not(.tb-seen), .InfoBar__idCard:not(.tb-seen)',
);
const jsApiPlaceHolder = `
<div class="tb-jsapi-container tb-modmail-sidebar-container">
<div class="InfoBar__recentsTitle">Toolbox functions:</div>
<span data-name="toolbox"></span>
</div>
`;
$modmailSidebar.each(async function () {
const $infobar = $(this);
$infobar.addClass('tb-seen');
const info = await TBCore.getThingInfo(this, true);
const $jsApiThingPlaceholder = $(jsApiPlaceHolder).appendTo($infobar);
const jsApiThingPlaceholder = $jsApiThingPlaceholder[0];
const detailObject = {
type: 'TBuserHovercard',
data: {
user: {
username: info.user || '[deleted]',
},
contextID: info.id,
subreddit: {
name: info.subreddit,
},
},
};
dispatchApiEvent(jsApiThingPlaceholder, detailObject);
});
}
}, 500);
}
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Mon Dec 30 2024 00:15:02 GMT+0000 (Coordinated Universal Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
<script src="scripts/search.js" defer></script>
</body>
</html>