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

if <script> with 'mw.config.set' is not found then grab <script> with config variables #1766

Merged
merged 2 commits into from
Feb 18, 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
6 changes: 4 additions & 2 deletions src/util/saveArticles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export async function saveArticles(zimCreator: ZimCreator, downloader: Downloade
}
}

async function getModuleDependencies(articleId: string, mw: MediaWiki, downloader: Downloader) {
export async function getModuleDependencies(articleId: string, mw: MediaWiki, downloader: Downloader) {
/* These vars will store the list of js and css dependencies for
the article we are downloading. */
let jsConfigVars = ''
Expand Down Expand Up @@ -411,10 +411,12 @@ async function getModuleDependencies(articleId: string, mw: MediaWiki, downloade
for (let i = 0; i < scriptTags.length; i += 1) {
if (scriptTags[i].text.includes('mw.config.set')) {
jsConfigVars = regex.exec(scriptTags[i].text)[0] || ''
jsConfigVars = `(window.RLQ=window.RLQ||[]).push(function() {${jsConfigVars}});`
} else if (scriptTags[i].text.includes('RLCONF') || scriptTags[i].text.includes('RLSTATE') || scriptTags[i].text.includes('RLPAGEMODULES')) {
jsConfigVars = scriptTags[i].text
}
}

jsConfigVars = `(window.RLQ=window.RLQ||[]).push(function() {${jsConfigVars}});`
jsConfigVars = jsConfigVars.replace('nosuchaction', 'view') // to replace the wgAction config that is set to 'nosuchaction' from api but should be 'view'

return { jsConfigVars, jsDependenciesList, styleDependenciesList }
Expand Down
27 changes: 26 additions & 1 deletion test/unit/saveArticles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { startRedis, stopRedis, redisStore } from './bootstrap.js'
import domino from 'domino'

import { setupScrapeClasses, convertWikicodeToHtml, testHtmlRewritingE2e } from '../util.js'
import { saveArticles, treatMedias, applyOtherTreatments, treatSubtitle, treatVideo } from '../../src/util/saveArticles.js'
import { saveArticles, getModuleDependencies, treatMedias, applyOtherTreatments, treatSubtitle, treatVideo } from '../../src/util/saveArticles.js'
import { ZimArticle } from '@openzim/libzim'
import { Dump } from '../../src/Dump'
import { mwRetToArticleDetail, renderDesktopArticle, DELETED_ARTICLE_ERROR } from '../../src/util/index.js'
Expand Down Expand Up @@ -349,4 +349,29 @@ describe('saveArticles', () => {
// Throwing error if article is deleted
expect(() => renderDesktopArticle(articleJsonObject, 'deletedArticle', { title: 'deletedArticle' })).toThrow(new Error(DELETED_ARTICLE_ERROR))
})

test('Load inline js from HTML', async () => {
const { downloader, mw } = await setupScrapeClasses() // en wikipedia

const _moduleDependencies = await getModuleDependencies('Potato', mw, downloader)
// next variables declared to avoid "variable is not defined" errors
let RLCONF: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let RLSTATE: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let RLPAGEMODULES: any
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const document: any = { documentElement: { className: '' }, cookie: '' }

// eslint-disable-next-line no-eval
eval(_moduleDependencies.jsConfigVars)
expect(RLCONF).toMatchObject({
wgPageName: 'Potato',
wgTitle: 'Potato',
wgPageContentLanguage: 'en',
wgPageContentModel: 'wikitext',
wgRelevantPageName: 'Potato',
wgRelevantArticleId: 23501,
})
})
})