Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove part of code which redirects the page #1811

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/util/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export const DEFAULT_WIKI_PATH = 'wiki/'
export const ALL_READY_FUNCTION = /function allReady\( modules \) {/
export const DO_PROPAGATION = /mw\.requestIdleCallback\( doPropagation, \{ timeout: 1 \} \);/
export const LOAD_PHP = /script.src = ".*load\.php.*";/
export const RULE_TO_REDIRECT = /window\.top !== window\.self/
export const WEBP_HANDLER_URL = 'https://gist.githubusercontent.com/rgaudin/60bb9cc6f187add506584258028b8ee1/raw/9d575b8e25d67eed2a9c9a91d3e053a0062d2fc7/web-handler.js'
export const MAX_FILE_DOWNLOAD_RETRIES = 5
38 changes: 26 additions & 12 deletions src/util/dump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import MediaWiki from '../MediaWiki.js'
import { ZimCreator, ZimArticle } from '@openzim/libzim'
import { Dump } from '../Dump.js'
import fs from 'fs'
import { DO_PROPAGATION, ALL_READY_FUNCTION, WEBP_HANDLER_URL, LOAD_PHP } from './const.js'
import { DO_PROPAGATION, ALL_READY_FUNCTION, WEBP_HANDLER_URL, LOAD_PHP, RULE_TO_REDIRECT } from './const.js'
import * as path from 'path'
import { fileURLToPath } from 'url'

Expand Down Expand Up @@ -89,13 +89,13 @@ export async function getAndProcessStylesheets(downloader: Downloader, redisStor
}

export async function downloadAndSaveModule(zimCreator: ZimCreator, mw: MediaWiki, downloader: Downloader, dump: Dump, module: string, type: 'js' | 'css') {
// param :
// module : string : the name of the module
// moduleUri : string : the path where the module will be saved into the zim
// type : string : either 'js' or 'css'
// this function save a key into Redis db in the form of module.type -> moduleUri
// return :
// a promise resolving 1 if data has been succesfully saved or resolving 0 if data was already in Redis
const replaceCodeByRegex = (sourceText, replaceMap: Map<RegExp, string>) => {
let text: string
replaceMap.forEach((textToReplace, regEx) => {
text = sourceText.replace(regEx, textToReplace)
})
return text
}

// the function hackStartupModule changes startup script by returning true for all modules so that load.php is not called.
// it also removes requestIdleCallback as in our case window is idle after all script tags are called but those script tags
Expand All @@ -104,8 +104,14 @@ export async function downloadAndSaveModule(zimCreator: ZimCreator, mw: MediaWik
if ((!ALL_READY_FUNCTION.test(jsCode) || !DO_PROPAGATION.test(jsCode)) && !LOAD_PHP.test(jsCode)) {
throw new Error('unable to hack startup module')
}

return jsCode.replace(DO_PROPAGATION, 'doPropagation();').replace(ALL_READY_FUNCTION, 'function allReady( modules ) { return true;').replace(LOAD_PHP, 'script.src ="";')
return replaceCodeByRegex(
jsCode,
new Map([
[DO_PROPAGATION, 'doPropagation();'],
[ALL_READY_FUNCTION, 'function allReady( modules ) { return true;'],
[LOAD_PHP, 'script.src ="";'],
]),
)
}

let apiParameterOnly
Expand All @@ -120,8 +126,16 @@ export async function downloadAndSaveModule(zimCreator: ZimCreator, mw: MediaWik

const { content } = await downloader.downloadContent(moduleApiUrl)
let text = content.toString()
if (module === 'startup' && type === 'js') {
text = hackStartUpModule(text)

if (type === 'js') {
switch (module) {
case 'startap':
text = hackStartUpModule(text)
break
case 'mediawiki.page.ready':
text = replaceCodeByRegex(text, new Map([[RULE_TO_REDIRECT, 'false']]))
break
}
}

try {
Expand Down