forked from ChalmersLibrary/chami
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
202 lines (170 loc) · 5.8 KB
/
app.js
File metadata and controls
202 lines (170 loc) · 5.8 KB
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
199
200
201
202
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const cron = require("./scheduling/cron.js");
// Load dev config if suitable.
try {
let config = require("./config.json");
for (let prop in config) {
if (config.hasOwnProperty(prop)) {
process.env[prop] = config[prop];
}
}
} catch (err) {
console.error("No local config present.");
}
if (process.env.debug) {
// Some debugging
//var librisFolioDataMover = new (require('./librisfoliodatamover.js'))();
//librisFolioDataMover.moveData(10939573);
//librisFolioDataMover.moveData(null, '2018-11-13T12:00:00Z');
//librisFolioDataMover.moveData();
//var fetchScheduler = new (require('./scheduling/fetchscheduler.js'))();
//fetchScheduler.getLatestFetchTimestamp();
}
cron.initialize(process.env.cronenabled || false);
const apiRouter = require("./routes/api");
const app = express();
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use("/api", apiRouter);
app.get("/bookmarklet.js", (req, res) => {
let jsString = `
if (location.host === "libris.kb.se") {
if (/^\\/katalogisering\\/[\\w\\d]{15}/.test(location.pathname)) {
if (((document.getElementById("formPath-mainEntity.@type").childNodes[6].textContent.trim().indexOf("Tryck") > -1) ||
(document.getElementById("formPath-mainEntity.@type").childNodes[6].textContent.trim().indexOf("Instans") > -1)) &&
(document.getElementsByClassName("CreateItem")[0].textContent.trim().indexOf("Visa bestånd") > -1)) {
let librisurl = \`https://\${location.host}\${location.pathname.replace(
"/katalogisering",
""
)}\`;
location.href = \`${process.env.serverurl}/api/PostAndRedirect?id=\${librisurl}\`;
} else {
alert("Posten kunde ej hämtas. Kolla om du står på instansen, om det finns bestånd och att posten inte är elektronisk");
}
} else {
alert("Du är inte i katalogiseringsverktyget.");
}
} else {
alert("Du besöker inte Libris!");
}
`;
res.set("Content-Type", "application/javascript");
res.send(jsString);
});
app.get("/eds-to-folio", function(req, res) {
res.send(
`<a href="javascript:(function(){window.s0=document.createElement('script');window.s0.setAttribute('type','text/javascript');window.s0.setAttribute('src','${process.env.serverurl}/eds-to-folio.js?t='+Date.now());document.getElementsByTagName('body')[0].appendChild(window.s0);})();">EDS->FOLIO</a>`
);
});
app.get("/eds-to-folio.js", (req, res) => {
let jsString = `
if(location.host.includes("ebscohost.com")) {
if(location.pathname.includes('eds/detail')) {
let clc=/.+clc\\.([0-9a-f\.\-]{36})/.exec(window.location.hash)[1];
let uidparts=/(.{8}).(.{4}).(.{4}).(.{4}).(.{12})/.exec(clc);
let uid=uidparts[1]+'-'+uidparts[2]+'-'+uidparts[3]+'-'+uidparts[4]+'-'+uidparts[5];
let url=\`${process.env.folioUrl}/inventory/view/\${uid}?qindex=id&query=\${uid}\`;
location.href=url;
} else {
alert('Du står inte på detaljsidesvyn.')
}
} else {
alert("Du besöker inte eds!")
}
`;
res.set("Content-Type", "application/javascript");
res.send(jsString);
});
app.get("/folio-to-eds", function(req, res) {
res.send(
`<a href="javascript:(function(){window.s0=document.createElement('script');window.s0.setAttribute('type','text/javascript');window.s0.setAttribute('src','${process.env.serverurl}/folio-to-eds.js?t='+Date.now());document.getElementsByTagName('body')[0].appendChild(window.s0);})();">FOLIO->EDS</a>`
);
});
app.get("/folio-to-eds.js", (req, res) => {
let jsString = `
if(location.host.includes("chalmers.folio.ebsco.com")) {
if(location.pathname.includes('inventory/view/')) {
let uuid = location.pathname.split('/')[3];
let url='https://search.ebscohost.com/login.aspx?direct=true&scope=site&site=eds-live&authtype=guest&custid=s3911979&groupid=main&profile=eds&lang=en&bquery=' + uuid;
window.open(url);
} else {
alert('Du står inte på detaljsidesvyn.')
}
} else {
alert("Du besöker inte Folio!")
}
`;
res.set("Content-Type", "application/javascript");
res.send(jsString);
});
/**
* Module dependencies.
*/
let debug = require("debug")("chami:server");
let http = require("http");
/**
* Get port from environment and store in Express.
*/
let port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
/**
* Create HTTP server.
*/
let server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
let port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== "listen") {
throw error;
}
let bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
let addr = server.address();
let bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}