Skip to content

Commit

Permalink
feat(Util): add findVariants (#1634)
Browse files Browse the repository at this point in the history
* feat(Util): add findVariants
  • Loading branch information
WaitSpringQW authored Jan 4, 2025
1 parent 135f342 commit 05073ba
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 22 deletions.
77 changes: 57 additions & 20 deletions dist/Util/Util.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {changeOpacityWhenMouseEnterOrLeave} from './modules/changeOpacityWhenMou
export {checkA11yConfirmKey} from './modules/checkA11yConfirmKey';
export {checkDependencies} from './modules/checkDependencies.ts';
export {delay} from './modules/delay';
export {findVariants} from './modules/findVariants';
export {generateArray} from './modules/generateArray';
export {generateSvgDataUrl} from './modules/generateSvgDataUrl';
export {getBody} from './modules/getBody';
Expand Down
41 changes: 41 additions & 0 deletions src/Util/modules/findVariants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {initMwApi} from './initMwApi';
import {uniqueArray} from './uniqueArray';

type FindVariants = typeof findVariants;

// eslint-disable-next-line func-style
async function findVariants(text: string) {
const api: mw.Api = initMwApi('Util-FindVariants');

const VARIANTS = ['zh-hans', 'zh-hant', 'zh-cn', 'zh-hk', 'zh-mo', 'zh-sg', 'zh-my', 'zh-tw'];

const allVariants: string[] = [];

const params: ApiParseParams = {
action: 'parse',
contentmodel: 'wikitext',
format: 'json',
formatversion: '2',
prop: ['displaytitle'],
title: 'temp',
text,
};

for (const variant of VARIANTS) {
params.uselang = variant;
params.variant = variant;
const response = await api.post(params);

const displaytitle = response?.['query']?.displaytitle as string;
const variantElement = document.createElement('variant');
variantElement.innerHTML = displaytitle;

if (variantElement.textContent) {
allVariants[allVariants.length] = variantElement.textContent;
}
}

return uniqueArray(allVariants);
}

export {type FindVariants, findVariants};
2 changes: 2 additions & 0 deletions src/Util/modules/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type ChangeOpacityWhenMouseEnterOrLeave =
type CheckA11yConfirmKey = import('./checkA11yConfirmKey').CheckA11yConfirmKey;
type CheckDependencies = import('./checkDependencies').CheckDependencies;
type Delay = import('./delay').Delay;
type FindVariants = import('./findVariants').FindVariants;
type GenerateArray = import('./generateArray').GenerateArray;
type GenerateSvgDataUrl = import('./generateSvgDataUrl').GenerateSvgDataUrl;
type GetBody = import('./getBody').GetBody;
Expand All @@ -26,6 +27,7 @@ declare module 'ext.gadget.Util' {
export const checkA11yConfirmKey: CheckA11yConfirmKey;
export const checkDependencies: CheckDependencies;
export const delay: Delay;
export const findVariants: FindVariants;
export const generateArray: GenerateArray;
export const generateSvgDataUrl: GenerateSvgDataUrl;
export const getBody: GetBody;
Expand Down
5 changes: 3 additions & 2 deletions src/Util/modules/queryUserGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {initMwApi} from './initMwApi';

type QueryUserGroups = typeof queryUserGroups;

const queryUserGroups = async (users: string[]) => {
// eslint-disable-next-line func-style
async function queryUserGroups(users: string[]) {
const api: mw.Api = initMwApi('Util-QueryUserGroups');

const CACHE_KEY_PREFIX = 'ext.gadget.Util_queryUserGroups-';
Expand Down Expand Up @@ -61,6 +62,6 @@ const queryUserGroups = async (users: string[]) => {
}

return {query: {users: queryUsers}};
};
}

export {type QueryUserGroups, queryUserGroups};

0 comments on commit 05073ba

Please sign in to comment.