diff --git a/src/routes.js b/src/routes.js index c5dd31d..a4e83ce 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1,7 +1,7 @@ module.exports = (app, utils) => { const config = require('../config') const path = require('path') - + app.all('*', (req, res, next) => { let themeOverride = req.query.theme if(themeOverride) { @@ -11,7 +11,7 @@ module.exports = (app, utils) => { } else if(!req.cookies.theme && req.cookies.theme !== '') { req.cookies.theme = config.theme } - + let langOverride = req.query.default_lang if(langOverride) { langOverride = langOverride.toLowerCase() @@ -20,19 +20,19 @@ module.exports = (app, utils) => { } else if(!req.cookies.default_lang) { req.cookies.default_lang = config.default_lang } - + return next() }) - + app.get('*', async (req, res, next) => { if(req.url.startsWith('/w/load.php')) { return res.sendStatus(404) } - + if(req.url.startsWith('/media')) { let media let mime = '' - + if(req.url.startsWith('/media/maps_wikimedia_org/')) { media = await proxyMedia(req, 'maps.wikimedia.org') } else if(req.url.startsWith('/media/api/rest_v1/media')) { @@ -43,29 +43,29 @@ module.exports = (app, utils) => { } else { media = await proxyMedia(req) } - + if(media.success === true) { if(mime != '') { res.setHeader('Content-Type', mime) } - + return res.sendFile(media.path) } else { return res.sendStatus(404) } } - + if(req.url.startsWith('/static/images/project-logos/')) { return res.sendFile(wikilessLogo()) } - + return next() }) - + app.get('/wiki/:page?/:sub_page?', (req, res, next) => { return handleWikiPage(req, res, '/wiki/') }) - + // This route handles wiki-pages starting with forward slash (issue #21)) app.get('/wiki//:page?/:sub_page?', (req, res, next) => { const page = req.params.page @@ -75,22 +75,22 @@ module.exports = (app, utils) => { } return handleWikiPage(req, res, '/wiki/') }) - + app.get('/w/:file', (req, res, next) => { return handleWikiPage(req, res, '/w/') }) - + app.get('/wiki/Special:Map/*', (req, res, next) => { return handleWikiPage(req, res, '/wiki/Map') }) - + app.get('/api/rest_v1/page/pdf/:page', async (req, res, next) => { if(!req.params.page) { return red.redirect('/') } - + let media = await proxyMedia(req, '/api/rest_v1/page/pdf') - + if(media.success === true) { let filename = `${req.params.page}.pdf` return res.download(media.path, filename) @@ -98,41 +98,41 @@ module.exports = (app, utils) => { return res.sendStatus(404) } }) - + app.get('/', (req, res, next) => { return handleWikiPage(req, res, '/') }) - + app.get('/about', (req, res, next) => { return res.sendFile(path.join(__dirname, '../static/about.html')) }) - + app.get('/preferences', (req, res, next) => { return res.send(preferencesPage(req, res)) }) - + app.post('/preferences', (req, res, next) => { let theme = req.body.theme let default_lang = req.body.default_lang let back = req.url.split('?back=')[1] - + res.cookie('theme', theme, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true }) res.cookie('default_lang', default_lang, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true }) - - if(back == 'undefined' || !back.startsWith('/')) { + + if(back === 'undefined' || !back.startsWith('/')) { back = '/' } - + return res.redirect(back) }) - + app.post(/DownloadAsPdf/, (req, res, next) => { if(!req.body.page) { return res.redirect('/') } - + let lang = req.body.lang || req.cookies.default_lang || config.default_lang - + return res.redirect(`/w/index.php?title=Special%3ADownloadAsPdf&page=${req.body.page}&action=redirect-to-electron&lang=${lang}`) }) } diff --git a/src/utils.js b/src/utils.js index dc2d1c9..e31a357 100644 --- a/src/utils.js +++ b/src/utils.js @@ -4,27 +4,27 @@ module.exports = function(redis) { const parser = require('node-html-parser') const fs = require('fs') const path = require('path') - + this.download = async (url, params = '') => { return new Promise(resolve => { if(!url) { resolve({ success: false, reason: 'MISSING_URL' }) } - + if(url.includes('?')) { let wikipage = url.split('wikipedia.org/wiki/')[1] let uriencoded_wikipage = '' - + if(wikipage) { uriencoded_wikipage = encodeURIComponent(wikipage) } url = url.replace(wikipage, uriencoded_wikipage) } - - if(params != '') { + + if(params) { url = `${url}?${params}` } - + redis.get(url, (error, data) => { if(data) { console.log(`Got key ${url} from cache.`) @@ -46,7 +46,7 @@ module.exports = function(redis) { return resolve({ success: false, reason: `INVALID_HTTP_RESPONSE: ${res.statusCode}` }) } } - + let data = '' res.on('data', (chunk) => { @@ -65,13 +65,13 @@ module.exports = function(redis) { }) }) } - + this.applyUserMods = (data, user_preferences) => { /** * We have already processed the HTML, but we haven't applied the user's * cookie specific modifications to it yet. Let's do it. */ - + if(user_preferences.theme === 'white') { // if the user has chosen the white theme from the preferences data = data.replace('', ``) @@ -85,20 +85,20 @@ module.exports = function(redis) { // default, auto theme data = data.replace('', ``) } - + return data } - + this.processHtml = (data, url, params, lang, user_preferences, process_user_prefs) => { return new Promise(resolve => { if(validHtml(data.html)) { url = encodeURI(url) - if(params != '') { + if(params) { url = `${url}?${params}` } - + data.html = parser.parse(data.html) - + // replace default wikipedia top right nav bar links let nav = data.html.querySelector('nav#p-personal .vector-menu-content-list') if(nav) { @@ -109,43 +109,41 @@ module.exports = function(redis) {
  • [ preferences ]
  • - + ` } - + // append the lang query param to the URLs starting with /wiki/ or /w/ let links = data.html.querySelectorAll('a') for(let i = 0; i < links.length; i++) { let href = links[i].getAttribute('href') - if(href) { - if(href.startsWith('/wiki/') || href.startsWith('/w/')) { - href = `${protocol}${config.domain}${href}` - let u = new URL(href) - u.searchParams.append('lang', lang) - href = `${u.pathname}${u.search}` - links[i].setAttribute('href', href) - } + if(href && (href.startsWith('/wiki/') || href.startsWith('/w/'))) { + href = `${protocol}${config.domain}${href}` + let u = new URL(href) + u.searchParams.append('lang', lang) + href = `${u.pathname}${u.search}` + links[i].setAttribute('href', href) } } - + // remove #p-wikibase-otherprojects let wikibase_links = data.html.querySelector('#p-wikibase-otherprojects') if(wikibase_links) { wikibase_links.remove() } - + // remove all