-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs2.html
53 lines (50 loc) · 2.28 KB
/
rs2.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
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
</head>
<body>
<h1>Reddit Search</h1>
<p>Enter a search term and specify the percentage of top comments and maximum age of the results to find the most upvoted comments on Reddit.</p>
<form>
<label for="search">Search:</label><br>
<input type="text" id="search" name="search"><br>
<label for="percentage">Top Comments:</label><br>
<input type="range" min="0" max="100" value="50" class="slider" id="percentage"><br>
<label for="age">Maximum Age:</label><br>
<input type="range" min="1" max="365" value="7" class="slider" id="age"><br>
<input type="button" value="Submit" onclick="searchReddit()">
</form>
<div id="results"></div>
<script>
function searchReddit() {
var searchTerm = $('#search').val();
var percentage = $('#percentage').val();
var age = $('#age').val();
var url = 'https://www.reddit.com/search.json?q=' + searchTerm + '&t=' + age;
$.get(url, function(data) {
var results = '';
data.data.children.forEach(function(item) {
// Get the comments for the post
var commentsUrl = 'https://www.reddit.com' + item.data.permalink + '.json';
$.get(commentsUrl, function(commentsData) {
// Sort the comments by the number of upvotes
var sortedComments = commentsData[1].data.children.sort(function(a, b) {
return b.data.ups - a.data.ups;
});
// Calculate the number of comments to display based on the percentage
var numComments = Math.round(sortedComments.length * (percentage / 100));
// Display the top-ranked comments
results += '<h2>' + item.data.title + '</h2>';
for (var i = 0; i < numComments; i++) {
results += '<p>' + sortedComments[i].data.body + '</p>';
}
// Add a link to the original Reddit post
results += '<p><a href="https://www.reddit.com' + item.data.permalink + '">View on Reddit</a></p>';
results += '<hr>';
$('#results').html(results);
});
});
});
}
</script>
</