This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
83 lines (74 loc) · 2.32 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
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
$(function() {
function consoleReloadDeals() {
chrome.tabs.query(
{ active: true, currentWindow: true },
(tabs) => refresh(tabs[0].id)
);
}
function refresh(tabId) {
$('#error').hide();
$('#result').hide();
$('#progress').show();
var options = {
region: $('#region').val(),
platform: $('#platform').val(),
start: $('#next').data('start') || 0,
size: 90,
};
chrome.tabs.sendMessage(tabId, options, (response) => {
if (!response) {
showError('Did not receive any data. Try reloading the page and retrying again. If the error persists - please check console logs and report to <a href="https://reddit.com/u/kpumukus">/u/kpumukus</a>.');
} else if (response.error) {
showError(response.error);
} else {
showResult(response);
}
if (response.retryIn) {
window.setTimeout(() => refresh(tabId), response.retryIn);
}
});
}
function showError(htmlMessage) {
$('#error-message').html(htmlMessage);
$('#progress').hide();
$('#error').show();
}
function showResult(response) {
$('#reddit-table').text(response.reddit);
$('#preview-table').html(response.html);
$('#progress').hide();
switchResultTable();
$('#result').show();
// Simple trick to make sure reddit table contents is selected
switchResultTable();
// Do we have more results?
if (response.start + response.size < response.total) {
$('#next').
show().
html('Next (from ' + (0 + response.start + response.size) + ') »').
data('start', response.start + response.size);
} else if (response.total > response.size) {
$('#next').
show().
html('From Beginning »').
data('start', 0);
} else {
$('#next').hide();
}
}
function switchResultTable() {
var table = $('#options input[name=show-table]:checked').val();
if (table === "preview") {
$('#reddit-table').hide();
$('#preview').show();
} else {
$('#preview').hide();
$('#reddit-table').show().focus().select();
}
}
consoleReloadDeals();
$('#region').change(consoleReloadDeals);
$('#platform').change(consoleReloadDeals);
$('#options input[name=show-table]').change(switchResultTable);
$('#next').click(consoleReloadDeals);
});