Skip to content

Commit

Permalink
[ADD] download.kiwix load as text content
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Oct 7, 2023
1 parent 1aa0d95 commit de8b510
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
1 change: 1 addition & 0 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ function handleFileDrop (packet) {
document.getElementById('libraryBtn').addEventListener('click', function (e) {
e.preventDefault();
uiUtil.tabTransitionToSection('library', params.showUIAnimations);
window.open('https://download.kiwix.org/zim/gutenberg/gutenberg_af_all_2023-05.zim', '_blank')
});

// Add event listener to link which allows user to show file selectors
Expand Down
72 changes: 54 additions & 18 deletions www/js/lib/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,52 @@ let LIB_URL = {
}

function resizeFrame (height) {
// setTimeout(function () {
// const iframe = document.getElementById('libraryIframe')
// iframe.style.height = height - 20 + 'px'
// }, 1000);
const iframe = document.getElementById('libraryIframe')
iframe.style.height = height - 20 + 'px'
if (iframe) iframe.style.height = height - 20 + 'px'
}

const script = ''

Check notice on line 18 in www/js/lib/library.js

View check run for this annotation

codefactor.io / CodeFactor

www/js/lib/library.js#L18

'script' is assigned a value but never used. (no-unused-vars)
function getHTMLContent (url, callback) {
const xhr = new XMLHttpRequest();

Check failure

Code scanning / CodeQL

Server-side request forgery Critical

The
URL
of this request depends on a
user-provided value
.
xhr.open('GET', url);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
let html = xhr.responseText
// html = html.replace('href="', '')
html = html.replace(/href="/gm, 'href="' + url) // append the url to all href
html = html.replace(/src="/gm, 'src="' + url) // append the url to all href
// html = html.replace(/alt="[DIR]"/gm, '')
console.log(html);
callback(html)

Check notice on line 32 in www/js/lib/library.js

View check run for this annotation

codefactor.io / CodeFactor

www/js/lib/library.js#L32

Expected an assignment or function call and instead saw an expression. (no-unused-expressions)

const links = document.getElementsByTagName('a')
for (let index = 0; index < links.length; index++) {
const element = links[index];
element.addEventListener('click', function (e) {
e.preventDefault();

Check warning on line 38 in www/js/lib/library.js

View check run for this annotation

codefactor.io / CodeFactor

www/js/lib/library.js#L38

Unexpected constant condition. (no-constant-condition)

console.log('clicked', e.target.href);
if (String(e.target.href).slice(-4) === '.zim') {
window.open(e.target.href, '_blank')
return
}
getHTMLContent(e.target.href, function (html) {
document.getElementsByTagName('body')[0].innerHTML = html
})
})
}
console.log(links);
} else {
throw new Error('Failed to get content from');
}
}
};
}

function setIframeUrl () {
Expand All @@ -18,7 +62,7 @@ function setIframeUrl () {
const fallbackLibraryURl = LIB_URL.altLibraryUrl
const iframe = document.getElementById('libraryIframe')

if (!libraryURl && !fallbackLibraryURl && iframe.getElementsByTagName('body')[0]) return
if (!libraryURl || !fallbackLibraryURl || !iframe) return

let isOptionalChainSupported = true // if not supported, that means the browser is too old
let isParentWindowSupported = true // if not supported, that means it's chrome extension
Expand All @@ -34,25 +78,17 @@ function setIframeUrl () {
isParentWindowSupported = false
}

console.log(iframe);
if (isOptionalChainSupported && isParentWindowSupported && false) {
// console.log(iframe);
if (isOptionalChainSupported && isParentWindowSupported && false) {

Check warning on line 82 in www/js/lib/library.js

View check run for this annotation

codefactor.io / CodeFactor

www/js/lib/library.js#L82

Multiple spaces found before '&&'. (no-multi-spaces)
iframe.setAttribute('src', libraryURl)
console.log('library loaded');
} else {
const xhr = new XMLHttpRequest();
console.log(xhr, fallbackLibraryURl);
xhr.open('GET', fallbackLibraryURl);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
iframe.setAttribute('srcdoc', xhr.responseText)
} else {
throw new Error('Failed to get content from');
}
}
};
console.log('download.kiwix loaded');
// iframe.setAttribute('src', fallbackLibraryURl)
// console.log(xhr, fallbackLibraryURl);
getHTMLContent(fallbackLibraryURl, function (html) {
document.getElementsByTagName('body')[0].innerHTML = html

Check warning

Code scanning / CodeQL

Client-side cross-site scripting Medium

Cross-site scripting vulnerability due to
user-provided value
.
})
// console.log('download.kiwix loaded');
}
}

Expand Down

0 comments on commit de8b510

Please sign in to comment.