-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
38 lines (31 loc) · 1.03 KB
/
popup.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
$(function() {
$('#submit').click(() => {
const from = document.getElementById('from').valueAsDate;
const to = document.getElementById('to').valueAsDate;
searchParameters = [dateToUTC(from), dateToUTC(to)];
updateTab(searchParameters);
$('#from').val('');
$('#to').val('');
return false;
})
})
function dateToUTC(date) {
return Math.floor(date.getTime() / 1000);
}
function redditQuery(searchParameters, url) {
const [from, to] = searchParameters;
var query = `https://www.reddit.com/search?q=timestamp:${from}..${to}&syntax=cloudsearch`;
const urlElements = url.split('/');
if (urlElements[3] === 'r') {
const subreddit = urlElements[4];
query = `https://www.reddit.com/r/${subreddit}/search?q=timestamp:${from}..${to}&syntax=cloudsearch`;
}
return query;
}
function updateTab(searchParameters) {
chrome.tabs.query({currentWindow: true, active: true}, (tabs) => {
const url = tabs[0].url;
const query = redditQuery(searchParameters, url);
chrome.tabs.update({url: query});
})
}