-
Notifications
You must be signed in to change notification settings - Fork 3
/
content.js
100 lines (82 loc) · 3.9 KB
/
content.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
chrome.runtime.sendMessage({todo: "showPageAction"});
const imgURL = chrome.runtime.getURL("filmReelIcon16.png");
const altImgURL = chrome.runtime.getURL("noDownloadIcon16.png");
/**
* Parse microfilms as in the format below :
* https://www.familysearch.org/search/film/005845257?cc=2036997&cat=108418
* and return parameters necessary to download the microfilm
*
*/
function parseMicrofilmUrlParams(film_url){
const url_pattern = new RegExp("https://www.familysearch.org/(search/film/(.*))\\?+[?:=&a-zA-Z0-9]*cat=([0-9]*)");
const url_pattern_results = url_pattern.exec(film_url);
const image_or_film_url = url_pattern_results[1];
const film_number = url_pattern_results[2];
const catalog_context = url_pattern_results[3];
return { film_number: film_number, catalog_context : catalog_context, image_or_film_url : image_or_film_url}
}
$(function(){
const params_array =[];
self = this;
/**
* avoid that index searchs appear as a download option a.film-format-index (index links )found in: https://www.familysearch.org/search/catalog/2656516?availability=Family%20History%20Library
*/
$('a.film-format-image').not('a.film-format-index').not(':has(img.restricted-film)').each(function( index, self ) {
//get href property and strip Microfilm parameters
const film_url= $(this).attr("href");
const film_link_id = 'film_link'+index;
const params = parseMicrofilmUrlParams(film_url)
const title = chrome.i18n.getMessage("allImgsFromMicrofim");
params_array.push(params)
//append microfilm Reel Image and attach click event to it
$(this).parent().append('<img id="'+film_link_id+'"src="'+imgURL+'" alt="MicroFilm reel" title="'+title+'" />');
$('#'+film_link_id).click( ()=> {
chrome.runtime.sendMessage({todo: "downloadImages", params : params } , function (dataReturned) {
// console.log(dataReturned);
});
});
});
$('a.film-format-image:has(img.restricted-film)').each(function( index, self ) {
//get href property and strip Microfilm parameters
const film_link_id = 'film_link'+index;
const title = chrome.i18n.getMessage("restrictedFilm");
//append microfilm Reel Image and attach click event to it
$(this).parent().append('<img id="'+film_link_id+'"src="'+altImgURL+'" alt="'+title+'" title="'+title+'" />');
$('#'+film_link_id).click( ()=> {
alert(title);
});
});
});
/**
* Strip dsgNum parameter from page content.
* It's found in the div with .film-number class
*/
function getFilmNumber (film_number_div) {
const film_number_pattern = new RegExp("([0-9]+)");
const film_number_pattern_results = film_number_pattern.exec(film_number_div);
film_number_pattern_results[1]
return film_number_pattern_results[1];
}
function getImageCount (image_count_label) {
const image_count_pattern = new RegExp("([0-9]+)");
const image_count_pattern_results = image_count_pattern.exec(image_count_label);
image_count_pattern_results[1]
return image_count_pattern_results[1];
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.todo == "getFilmData"){
if (location.href.indexOf("catalog")=== -1 ){
const film_number_div = $('.film-number').text();
const film_number = getFilmNumber (film_number_div);
const image_count_label = $('label.afterInput').text();
const image_count = getImageCount (image_count_label);
const is_restricted = !!$("#saveLi > a.disabled").length;
const film_data = { film_number: film_number, image_count : image_count, is_restricted:is_restricted };
console.log('film_data :', film_data);
sendResponse(film_data);
}else{
sendResponse("hi there");
}
return true;
}
});