Skip to content

Commit

Permalink
[REVERT,ADD] open urls in new tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Oct 9, 2023
1 parent de8b510 commit 758bf38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 120 deletions.
17 changes: 13 additions & 4 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ function resizeIFrame () {
const libraryContent = document.getElementById('libraryContent');
const frames = [articleContent, libraryContent]
const region = document.getElementById('search-article');
libraryContent.contentWindow.postMessage({ event: 'resize', data: window.innerHeight }, '*');
const nestedFrame = libraryContent.contentWindow.document.getElementById('libraryIframe')
// window.postMessage(window.innerHeight, '*');

for (let i = 0; i < frames.length; i++) {
const iframe = frames[i];
if (iframe.style.display === 'none') {
// We are in About or Configuration, so we only set the region height
region.style.height = window.innerHeight + 'px';
// nestedFrame.style.height = window.innerHeight - 110 + 'px';
nestedFrame.style.height = window.innerHeight - 110 + 'px';
} else {
// IE cannot retrieve computed headerStyles till the next paint, so we wait a few ticks
setTimeout(function () {
Expand All @@ -158,7 +158,7 @@ function resizeIFrame () {
iframe.style.height = window.innerHeight - headerHeight + 'px';
// We have to allow a minimum safety margin of 10px for 'iframe' and 'header' to fit within 'region'
region.style.height = window.innerHeight + 10 + 'px';
// nestedFrame.style.height = window.innerHeight - 110 + 'px';
nestedFrame.style.height = window.innerHeight - 110 + 'px';
}, 100);
}
}
Expand Down Expand Up @@ -1310,7 +1310,16 @@ 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')

const libraryContent = document.getElementById('libraryContent');
const iframe = libraryContent.contentWindow.document.getElementById('libraryIframe');
try {
// eslint-disable-next-line no-new-func
Function('try{}catch{}')();
iframe.setAttribute('src', params.libraryUrl);
} catch (error) {
window.open(params.altLibraryUrl, '_blank');
}
});

// Add event listener to link which allows user to show file selectors
Expand Down
3 changes: 0 additions & 3 deletions www/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ document.getElementById('languageSelector').value = params.overrideBrowserLangua
document.getElementById('bypassAppCacheCheck').checked = !params.appCache;
document.getElementById('appVersion').textContent = 'Kiwix ' + params.appVersion;

// Send the required config to the iframe
document.getElementById('libraryContent').contentWindow.postMessage({ event: 'setIframeUrl', data: { libraryUrl: params.libraryUrl, altLibraryUrl: params.altLibraryUrl } }, '*');

// This is a simplified version of code in settingsStore, because that module is not available in init.js
function getSetting (name) {
var result;
Expand Down
113 changes: 0 additions & 113 deletions www/js/lib/library.js
Original file line number Diff line number Diff line change
@@ -1,113 +0,0 @@
// This page will be loaded in an iframe in library.html
// If this page is directly loaded it will just throw a lot of errors

let LIB_URL = {
libraryUrl: '',
altLibraryUrl: ''
}

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

const script = ''
function getHTMLContent (url, callback) {
const xhr = new XMLHttpRequest();
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)

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

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 () {
// const libraryURl = urls.libraryUrl
// const fallbackLibraryURl = urls.altLibraryUrl
const libraryURl = LIB_URL.libraryUrl
const fallbackLibraryURl = LIB_URL.altLibraryUrl
const iframe = document.getElementById('libraryIframe')

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
try {
// eslint-disable-next-line no-eval
eval('try{}catch{}')
} catch (error) {
isOptionalChainSupported = false
}
try {
window.parent.params
} catch (error) {
isParentWindowSupported = false
}

// console.log(iframe);
if (isOptionalChainSupported && isParentWindowSupported && false) {
iframe.setAttribute('src', libraryURl)
console.log('library loaded');
} else {
// iframe.setAttribute('src', fallbackLibraryURl)
// console.log(xhr, fallbackLibraryURl);
getHTMLContent(fallbackLibraryURl, function (html) {
document.getElementsByTagName('body')[0].innerHTML = html
})
// console.log('download.kiwix loaded');
}
}

window.addEventListener('DOMContentLoaded', function () {
setIframeUrl()
})

window.addEventListener('message', function (e) {
console.log(e.data);
if (e.data.event === 'resize') {
const height = e.data.data
resizeFrame(height)
}
if (e.data.event === 'setIframeUrl') {
const urls = e.data.data
LIB_URL = {
libraryUrl: urls.libraryUrl,
altLibraryUrl: urls.altLibraryUrl
}
setIframeUrl()
}
})

0 comments on commit 758bf38

Please sign in to comment.