Skip to content

Commit

Permalink
Fix: Return value of mw.config.get
Browse files Browse the repository at this point in the history
  • Loading branch information
AnYiEE committed Oct 24, 2023
1 parent 4190dc7 commit 37e165f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
6 changes: 3 additions & 3 deletions static/wikimirror.js

Large diffs are not rendered by default.

63 changes: 35 additions & 28 deletions static/wikimirror.tsc.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,52 +473,59 @@
const hookedMwConfigGet = (...args) => {
let isMediaWikiConfigMap = false;
let originReturnValue;
switch (args.length) {
case 0:
if (args.length) {
if (Array.isArray(args[0])) {
isMediaWikiConfigMap = true;
originReturnValue = originMwConfigGet.bind(mw.config)();
break;
case 1:
originReturnValue = originMwConfigGet.call(mw.config, args[0]);
break;
default:
originReturnValue = originMwConfigGet.call(mw.config, args[0], args[1]);
break;
}
originReturnValue = originMwConfigGet.apply(mw.config, args);
} else {
isMediaWikiConfigMap = true;
originReturnValue = originMwConfigGet.bind(mw.config)();
}
const correctReturnValue = (selection, valueOfMediaWikiConfigMap) => {
const target = valueOfMediaWikiConfigMap ?? originReturnValue;
const correctReturnValue = (selection, target) => {
const isObject = Object.prototype.toString.call(target) === '[object Object]';
const isString = typeof target === 'string';
switch (selection) {
case 'wgGraphAllowedDomains':
return target['https']?.push(this.DOMAIN);
if (isObject) {
target['https']?.push(this.DOMAIN);
}
break;
case 'wgKartographerMapServer':
return `https://maps.${this.DOMAIN}`;
if (isString) {
target = `https://maps.${this.DOMAIN}`;
}
break;
case 'wgMultimediaViewer':
for (const [key, value] of Object.entries(target)) {
if (!key.includes('Link')) {
continue;
if (isObject) {
for (const [key, value] of Object.entries(target)) {
if (!key.includes('Link')) {
continue;
}
const urlObject = new URL(value);
urlObject.host = `www.mediawiki.${this.DOMAIN}`;
target[key] = urlObject.toString();
}
const urlObject = new URL(value);
urlObject.host = `www.mediawiki.${this.DOMAIN}`;
target[key] = urlObject.toString();
}
return target;
break;
case 'wgUrlShortenerAllowedDomains':
return target.replace('^', `^(.*\\.)?${this.DOMAIN_REGEX}$|^`);
if (isString) {
target = target.replace('^', `^(.*\\.)?${this.DOMAIN_REGEX}$|^`);
}
}
return target;
};
if (isMediaWikiConfigMap) {
const mediaWikiConfigMap = originReturnValue;
for (const key in mediaWikiConfigMap) {
if (!WikiMirrorPrivateMethod.isValidKey(mediaWikiConfigMap, key)) {
for (const [key, value] of Object.entries(mediaWikiConfigMap)) {
if (!value) {
continue;
}
const valueOfMediaWikiConfigMap = mediaWikiConfigMap[key];
mediaWikiConfigMap[key] = correctReturnValue(key, valueOfMediaWikiConfigMap);
mediaWikiConfigMap[key] = correctReturnValue(key, value);
}
originReturnValue = mediaWikiConfigMap;
} else {
originReturnValue = correctReturnValue(args[0]);
} else if (originReturnValue) {
originReturnValue = correctReturnValue(args[0], originReturnValue);
}
return originReturnValue;
};
Expand Down

0 comments on commit 37e165f

Please sign in to comment.