-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranscribersOfReddit comment link.user.js
115 lines (72 loc) · 5.18 KB
/
TranscribersOfReddit comment link.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
(() => {
// ==UserScript==
// @name Transcription Finder for Reddit
// @description Finds Reddit post transcriptions submitted by r/TranscribersOfReddit volunteers and inserts a link to them after the post title.
// @version 0.1.2
// @grant none
// @match https://*.reddit.com/r/*
// @match https://*.reddit.com/
// @match https://*.reddit.com/?*
// @author Mr_Transcriber
// @run-at document-end
// @noframes
// @license MIT
// @namespace https://grafeas.org
// ==/UserScript==
//Update the list of subreddits if we're past the "next time we should check for updates" timestamp, or if we've never checked
var subreddits;
if (typeof localStorage['TranscriptionFinder_subreddits_update'] === "undefined" || localStorage['TranscriptionFinder_subreddits_update'] < Date.now()) {
fetch('https://' + location.host + '/r/TranscribersOfReddit/wiki/subreddits.json').then(response => response.json().then(contents => {
subreddits = contents.data.content_md.toLowerCase().split(/\r?\n/);
localStorage['TranscriptionFinder_subreddits'] = subreddits.join `,`;
localStorage['TranscriptionFinder_subreddits_update'] = Date.now() + (3600 * 24 * 1000); //Check again in 1 day
main(subreddits);
})).catch(exception => console.error("Failed to get subreddits list", exception));
} else {
subreddits = localStorage['TranscriptionFinder_subreddits'].split `,`;
main(subreddits);
}
//Functions defined like this get detected by the JS engine before the script is executed, so it'll be available above.
function main(subreddits) {
// No need to iterate over the siteTable on non-participating subreddits
if (location.pathname.substr(0, 3) === '/r/') {
let subredditFromURL = location.pathname.split `/` [2].toLowerCase();
if (subredditFromURL !== 'all' && subredditFromURL !== 'mod' && subredditFromURL !== 'friends' && subredditFromURL !== 'popular' && subredditFromURL !== 'dashboard' && subredditFromURL.indexOf('+') === -1 && subredditFromURL.indexOf('-') === -1 && subreddits.indexOf(subredditFromURL) === -1) return; //Will never return for filtered (e.g. /r/all-test) or combined (e.g. /r/test+example). (Room for further optimization.)
}
var siteTable = document.getElementById `siteTable`;
if (!(siteTable && siteTable.getAttribute('class').indexOf('linklisting') > -1)) return;
var listOfIds = [];
Array.prototype.forEach.call(siteTable.childNodes, (element) => {
if (element.id.substr(0, 9) !== 'thing_t3_') return;
if (element.getAttribute('class').indexOf('self') > -1) return;
if (subreddits.indexOf(element.getAttribute('data-subreddit').toLowerCase()) === -1) return; // If we're scanning the siteTable on r/all, r/popular, the front page, user pages, multireddits etc., we only need to check for transcripts for links to participating subreddits
var submissionId = element.id.replace('thing_t3_', '');
((submissionId) => {
//Right, now let me fetch comments and check for transcript
fetch('https://' + location.host + '/' + submissionId + '.json?limit=999999&raw_json=1').then(resp => resp.json().then(submission => {
if (!(submission instanceof Array)) {
throw new Error("Submission isn't array", comments);
}
var comments = submission[1].data.children;
//Assumes the transcript is in a top-level comment
comments.forEach(comment => {
if (comment.kind !== 't1') {
console.warn("Wrong type, should be t1", comment);
return;
} //Shouldn't happen, but just in case
if (comment.data.body.indexOf("^^I'm a human volunteer content transcriber for Reddit and you could be too! [If you'd like more information on what we do and why we do it, click here!](https://www.reddit.com/r/TranscribersOfReddit/wiki/index)") > -1) {
var url = "https://" + location.host + "/r/" + submission[0].data.children[0].data.subreddit + "/comments/" + submissionId + "/_/" + comment.data.id;
console.log(url);
var linkToComment = document.createElement('a');
linkToComment.textContent = 'View Transcript';
linkToComment.href = url;
var entryTitle = document.getElementById('thing_t3_' + submissionId).querySelector `div.entry div.top-matter p.title`;
entryTitle.appendChild(document.createTextNode(" "));
entryTitle.appendChild(linkToComment);
}
});
})).catch(exception => console.error(exception));
})(submissionId)
});
}
})()