-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
199 lines (155 loc) · 6.81 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
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
/**
* Strip url to get the cat and imageOrFilmUrl params
* i is not necessary
* example urls :
* https://www.familysearch.org/ark:/61903/3:1:3Q9M-C3MX-4TKW?cat=101779
* and
* https://www.familysearch.org/ark:/61903/3:1:3Q9M-C3MX-4TN9?i=1&cat=101779
*/
function parseUrlParams(request_url) {
//let url_pattern = new RegExp("https://www.familysearch.org/(ark:/[0-9]*/.*)\?.*&cat=(\d*)");
//const url_pattern = new RegExp("https://www.familysearch.org/(ark:/[0-9]*/.*)\\?+(?:.=.&)*cat=(\\d*)");
//const url_pattern = new RegExp("https://www.familysearch.org/(ark:/[0-9]*/.*)\\?+(?:.=.&)*cat=([0-9]*)");
/**
* Somehow the pattern above failed to match the following url:
* https://www.familysearch.org/search/film/005845257?cc=2036997&cat=108418
* because of the "cc=2036997" url parameter.
* I updated it to solve the bug
*/
const url_pattern = new RegExp("https://www.familysearch.org/(.*/.*/.*)\\?+[?:=&a-zA-Z0-9]*cat=([0-9]*)");
const url_pattern_results = url_pattern.exec(request_url);
const cat = url_pattern_results[2];
const image_or_film_url = url_pattern_results[1];
return { 'cat': cat, 'image_or_film_url': image_or_film_url }
}
/**
* Keep number of records downloaded in the last 24 hours updated
*/
chrome.storage.onChanged.addListener(function(changes, namespace) {
if (!!changes.FSFilmLog){
FSFilmLog = changes.FSFilmLog.newValue;
const sum_of_records = countDownloadedRecords(FSFilmLog);
$('#downloadCountNumber').text(sum_of_records);
}
});
$(function () {
function showHide(state) {
if (!!state){
$('ul.list-group.below>li:nth-child(1)').hide();
$('ul.list-group.below>li:nth-child(2)').hide();
$('ul.list-group.below>li:nth-child(3)').hide();
}else{
$('ul.list-group.below>li:nth-child(1)').show();
$('ul.list-group.below>li:nth-child(2)').show();
$('ul.list-group.below>li:nth-child(3)').show();
}
}
getShowHideStatus(function (state) {
showHide(state);
})
/**
* Fetch necessary parameters to download all images
*/
function getMicrofilmParams() {
const film_number = $('span#film_number').text();
const catalog_context = $('input#cat').val();
const image_or_film_url = $('input#image_or_film_url').val();
return { film_number: film_number, catalog_context: catalog_context, image_or_film_url: image_or_film_url }
}
/**
* Fetch necessary parameters to download all images
* plus the desired download range
*/
function getMicrofilmParamsMinMaxImageNumbers() {
params = getMicrofilmParams()
params.image_min = $('#fromImage').val();
params.image_max = $('#toImage').val();
return params;
}
/**
* Provides Internalization
*/
$('.translate').each(function (index) {
if (typeof (this.getAttribute('value')) === "string") { //text is set by attribute value and not inner text (as in form components)
$(this).val(chrome.i18n.getMessage(this.id));
} else {
$(this).text(chrome.i18n.getMessage(this.id));
}
});
$('#allImgs').click(function () {
const params = getMicrofilmParams();
//download images on the background script
chrome.runtime.sendMessage({ todo: "downloadImages", params: params }, function (dataReturned) {
// console.log(dataReturned);
});
});
$('#someImgs').click(function () {
const params = getMicrofilmParamsMinMaxImageNumbers();
//download images on the background script
chrome.runtime.sendMessage({ todo: "downloadImages", params: params }, function (dataReturned) {
// console.log(dataReturned);
});
});
$('#hideInfo').parent().click( async function () {
toggleShowHide(function (state) {
showHide(state);
})
});
/**
* If the user wishes to stop all downloads, this option will do it
*/
$('#stopDownloads').click(function () {
chrome.runtime.sendMessage({ todo: "stopDownloads" });
});
/**
* Load film_number and image_count to generate download options
*/
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
request_url = tabs[0].url;
/**
* it's necessary to change the popup content according to the
* page the user is visiting. If he is still the catalog search page,
* do not load film number data.
*/
if (request_url.indexOf("catalog") === -1) {
chrome.tabs.sendMessage(tabs[0].id, { todo: "getFilmData" }, function (film_data) {
if (typeof(film_data) == "undefined") {
$('#film_data > ul > li:nth-child(1)').text( chrome.i18n.getMessage("popupTooSoon") );
$('#film_data > ul > li:nth-child(2)').hide();
$('#film_data > ul > li:nth-child(3)').hide();
$('#film_data > ul > li:nth-child(4)').hide();
} else {
console.log('film_data :', film_data);
$('span#film_number').text(film_data.film_number);
$('span#image_count').text(film_data.image_count);
$('#fromImage').val('1');
$("#fromImage").attr("max", film_data.image_count);
$('#toImage').val(film_data.image_count);
$('#toImage').attr("max", film_data.image_count);
const url_params = parseUrlParams(request_url);
$('input#cat').val(url_params.cat);
$('input#image_or_film_url').val(url_params.image_or_film_url);
if (film_data.is_restricted){
$('#film_data > ul > li:nth-child(4)').hide();
$('#film_data > ul > li:nth-child(3)').text( chrome.i18n.getMessage("restrictedFilm") );
// $('#film_data > ul > li:nth-child(2)').text("Hello world!");
}
}
return true;
});
} else {
$('div#film_data').hide();
}
});
/**
* Keep number of records downloaded in the last 24 hours updated
*/
return chrome.storage.local.get( {FSFilmLog : []} , function (result) {
FSFilmLog = result.FSFilmLog;
if (!!FSFilmLog){
FSFilmLog = deleteOldRecords(FSFilmLog);
const sum_of_records = countDownloadedRecords(FSFilmLog);
$('#downloadCountNumber').text(sum_of_records);
}
});
});