-
Notifications
You must be signed in to change notification settings - Fork 0
/
amazon_mit_stadtbibliothek-leipzig.de.user.js
130 lines (125 loc) · 6 KB
/
amazon_mit_stadtbibliothek-leipzig.de.user.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
// ==UserScript==
// @name Amazon mit stadtbibliothek-leipzig.de
// @namespace de.leipzig.library
// @description Reichert die Amazon Buch Detail Seite mit Informationen zur Verfügbarkeit aus der stadtbibliothek-leipzig.de an (Enriches amazon book detaily page by availability information from stadtbibliothek-leipzig.de)
// @version 1.0
// @include *.amazon.*/dp/*
// @include *.amazon.*/gp/product*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @grant GM.xmlHttpRequest
// @author timguy
// @license MIT
// @noframes
// @run-at document-end
// ==/UserScript==
addOwnHTML();
updateProgressBar(10);
var isbn = getIsbn(window.location.href);
console.log("Parsed amazon ISBN:" + isbn);
updateProgressBar(20);
var libraryStartPage = "https://webopac.stadtbibliothek-leipzig.de/webOPACClient/start.do";
var libraryDetailSearchPage = "https://webopac.stadtbibliothek-leipzig.de/webOPACClient/search.do?methodToCall=submit&CSId=USERSESSIONIDplaceholder&methodToCallParameter=submitSearch&searchCategories%5B0%5D=100&searchString%5B0%5D=&combinationOperator%5B1%5D=AND&searchCategories%5B1%5D=540&searchString%5B1%5D=ISBN_placeholder&combinationOperator%5B2%5D=AND&searchCategories%5B2%5D=331&searchString%5B2%5D=&submitSearch=Suchen&callingPage=searchParameters&selectedViewBranchlib=0&selectedSearchBranchlib=&searchRestrictionID%5B0%5D=4&searchRestrictionValue1%5B0%5D=&searchRestrictionValue2%5B0%5D=&searchRestrictionID%5B1%5D=3&searchRestrictionValue1%5B1%5D=";
//Im Request Header muss das hier sein:
//Cookie:JSESSIONID=D37A423603C9C747A637F949BD6CA2BE; USERSESSIONID=18225N35Sd2f78b0ca8462efa8e52887c63e647f65e98852f; BaseURL="/webOPACClient/start.do?BaseURL=this"; USERSESSIONID=13071N17Sf3948e533b6bf5afbdbb2e830bb7f0ff9e8ee6cd; BaseURL=""
//var respHeader = getCookie(libraryStartPage);
var respHeader;
getCookie(libraryStartPage);
updateProgressBar(30);
function continueAfterCookie(respHeader) {
updateProgressBar(40);
console.log('try to parse respHeader');
var cookieHeader = parseCookieHeaders(respHeader);
console.log('response received getCookie: \t jSessionId: ' + cookieHeader.strJSessionID + '\t strUserSessionID: ' + cookieHeader.strUserSessionID);
callSearch(libraryDetailSearchPage, cookieHeader);
}//search for the isbn and include all cookie and session information
function callSearch(libraryDetailSearchPage, cookieHeader) {
updateProgressBar(50);
libraryDetailSearchPage = libraryDetailSearchPage.replace('USERSESSIONIDplaceholder', cookieHeader.strUserSessionID);
libraryDetailSearchPage = libraryDetailSearchPage.replace('ISBN_placeholder', isbn);
console.log('start call libraryDetailSearchPage:' + libraryDetailSearchPage);
GM.xmlHttpRequest({
method: 'GET',
url: libraryDetailSearchPage,
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
'Accept': 'application/atom+xml,application/xml,text/xml',
//'Cookie': cookieHeader.strSetCookieHeader,
},
onload: function (response) {
updateProgressBar(80);
console.log('response received libraryDetailSearchPage: ' + response.status);
var searchResponse = response.responseText;
//console.log(searchResponse);
if (searchResponse.includes('keine Treffer'))
{
console.log('library didnt found a book for given isbn' + isbn);
addHTMLbiboContent('Buch ISBN (' + isbn + ') gibt es nicht in der Bibo');
updateProgressBar(100);
return 0;
}
var dataTable = $(searchResponse).find('#tab-content').html();
//console.log('-----------------------\ntabContent: ' + dataTable);
addHTMLbiboContent(dataTable);
updateProgressBar(100);
}
});
}//################### Help methods ################################
//get the cookie from library page and session information
function getCookie(libraryStartPage) {
console.log('start call:' + libraryStartPage);
GM.xmlHttpRequest({
method: 'GET',
url: libraryStartPage,
headers: {
'User-agent': 'Mozilla/5.0 Greasemonkey',
'Accept': '*/*',
'Host': 'webopac.stadtbibliothek-leipzig.de'
},
onload: function (response) {
continueAfterCookie(response.responseHeaders);
}
});
}// Method based on http://userscripts-mirror.org/scripts/show/1396 - Based on work by Carrick Mundell http://userscripts.org/scripts/show/1396
function getIsbn(url) {
try {
//match if there is a / followed by a 7-9 digit number followed by either another number or an x
//followed by a / or end of url
var isbn = url.match(/\/(\d{7,9}[\d|X])(\/|\?|$)/) [1];
} catch (e) {
return 0;
}
return isbn;
}
function addOwnHTML()
{
var inclLibText = '<div id="inclLibText">stadtbibliothek-leipzig.de<br/> <progress id="biboProgress" value="0" max="100"> <i class="fa fa-car"></i> </progress> <div id="biboContent"> </div> </div> ';
$(inclLibText).insertBefore('#MediaMatrix');
$('#inclLibText').attr('style', 'border-radius: 25px; background: #73AD21; padding: 20px;');
}
function addHTMLbiboContent(biboContent)
{
$('#biboContent').replaceWith(biboContent);
}
function updateProgressBar(value)
{
$('#biboProgress').val(value);
};
//parse cookie and session details
function parseCookieHeaders(strAllRequestHeaders) {
console.log('all Request Headers: ' + strAllRequestHeaders);
var strSetCookieHeader = strAllRequestHeaders.match(/Set-Cookie: ([\s\S]*?)Transfer-Encoding/) [1]; //Should be improved if order of headers change
console.log('parsed 1 finished ' + strSetCookieHeader);
var strUserSessionID = strSetCookieHeader.match(/USERSESSIONID=(.*)/) [1];
console.log('parsed 2 finished ' + strUserSessionID);
var strJSessionID = strSetCookieHeader.match(/JSESSIONID=(.*?);/) [1];
console.log('parsed 3 finished ' + strJSessionID);
if(strSetCookieHeader == null){
console.error("No Cookie found in response");
}
console.log('parseCookieHeaders finished ' + strSetCookieHeader);
return {
strSetCookieHeader: strSetCookieHeader,
strUserSessionID: strUserSessionID,
strJSessionID: strJSessionID
};
};