Skip to content

Commit

Permalink
refactor: deal with no-jquery/no-global-selector issue (#423)
Browse files Browse the repository at this point in the history
Signed-off-by: WaitSpring <[email protected]>
  • Loading branch information
WaitSpringQW authored Nov 20, 2023
1 parent d5736b3 commit ac39576
Show file tree
Hide file tree
Showing 89 changed files with 921 additions and 829 deletions.
3 changes: 1 addition & 2 deletions src/AccessKeyCheatsheet/modules/core.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {$body} from '../../util';
import {generateAccessKeysTableWithOpener} from './util/generateAccessKeysTableWithOpener';
import {generateMessageDialogProperty} from './util/generateMessageDialogProperty';

/**
* Adds alt+shift+? as an access key to show a list of all default access keys
*/
const accessKeyCheatsheet = (): void => {
const $body: JQuery<HTMLBodyElement> = $('body');

const {$accessKeysTable, $accessKeyCheatsheetOpener} = generateAccessKeysTableWithOpener($body);
const messageDialogProperty = generateMessageDialogProperty($accessKeysTable);

Expand Down
3 changes: 1 addition & 2 deletions src/AddSectionPlus/AddSectionPlus.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {$body} from '../util';
import {WG_SKIN} from './modules/constant';
import {processElement} from './modules/processElement';

Expand All @@ -6,8 +7,6 @@ $(function addSectionPlus(): void {
return; // style is already added by [[MediaWiki:Gadget-AddSectionPlus-pagestyles.css]]
}

const $body: JQuery<HTMLBodyElement> = $('body');

const $wrapper: JQuery = $body.find('#ca-addsection');
if (!$wrapper.length) {
return;
Expand Down
3 changes: 1 addition & 2 deletions src/AdvancedSiteNotices/AdvancedSiteNotices.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {$body} from '../util';
import type {RemoteNotices} from './modules/util/queryApi';
import {WG_ACTION} from './modules/constant';
import {loadRemoteNotices} from './modules/loadRemoteNotices';
import {showNotices} from './modules/showNotice';

$(async function advancedSiteNotices(): Promise<void> {
const $body: JQuery<HTMLBodyElement> = $('body');

const $siteNotice: JQuery = $body.find('#siteNotice');
if (['edit', 'submit'].includes(WG_ACTION) || !$siteNotice.length) {
return;
Expand Down
4 changes: 1 addition & 3 deletions src/AjaxLogin/AjaxLogin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {$body, checkA11yConfirmKey} from '../util';
import {LOGIN_ELEMENT_SELECTOR, WG_USER_NAME} from './modules/constant';
import {ajaxLogin} from './modules/core';
import {checkA11yConfirmKey} from '../util';

$(function initAutoLogin(): void {
const $body: JQuery<HTMLBodyElement> = $('body');

const $element: JQuery<HTMLAnchorElement> = $body.find<HTMLAnchorElement>(LOGIN_ELEMENT_SELECTOR);
if (WG_USER_NAME || !$element.length) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/AjaxLogin/modules/util/removeWindowResizeHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {$window} from '../../../util';

const removeWindowResizeHandler = (windowManager: OO.ui.WindowManager): void => {
$(window).off({
$window.off({
'orientationchange resize': (windowManager as unknown as {onWindowResizeHandler: () => void})
.onWindowResizeHandler,
});
Expand Down
4 changes: 1 addition & 3 deletions src/AutoLink/modules/util/getTargetElements.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IS_DIFF_ACTION, IS_TARGET_SPECIAL_PAGE, IS_WG_EDIT_OR_SUBMIT_ACTION, IS_WG_HISTORY_ACTION} from '../constant';

import {$body} from '../../../util';
interface TargetElements {
color: string;
$targetElementArray: JQuery[];
Expand All @@ -9,8 +9,6 @@ const getTargetElements = (): TargetElements => {
const $targetElementArray: JQuery[] = [];
let color = ''; // links color (coloured links)

const $body: JQuery<HTMLBodyElement> = $('body');

if (IS_DIFF_ACTION) {
// in diff pages
color = 'inherit'; // not coloured links
Expand Down
5 changes: 3 additions & 2 deletions src/Cat-a-lot/modules/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable mediawiki/class-doc */
import {$body, $window} from '../../util';
import {
API_ENTRY_POINT,
API_TAG,
Expand Down Expand Up @@ -99,7 +100,7 @@ const catALot = (): void => {

CAL.initSettings();

this.$body = $('body');
this.$body = $body;
this.$container = $('<div>').addClass(CLASS_NAME_CONTAINER).appendTo(this.$body);
this.$dataContainer = $('<div>').addClass(CLASS_NAME_CONTAINER_DATA).appendTo(this.$container);
this.$markCounter = $('<div>')
Expand Down Expand Up @@ -659,7 +660,7 @@ const catALot = (): void => {
// Reset width
this.$container.width('');
this.$container.height('');
this.$container.width(Math.min((this.$container.width() ?? 0) * 1.1 + 15, ($(window).width() ?? 0) - 10));
this.$container.width(Math.min((this.$container.width() ?? 0) * 1.1 + 15, ($window.width() ?? 0) - 10));
this.$resultList.css({
'max-height': `${CAL.dialogHeight}px`,
height: '',
Expand Down
3 changes: 2 additions & 1 deletion src/Cat-a-lot/modules/extendJQueryPrototype.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable mediawiki/class-doc */
import {CLASS_NAME_LABEL, CLASS_NAME_LABEL_LAST_SELECTED, CLASS_NAME_LABEL_SELECTED} from './constant';
import {$document} from '../../util';

const extendJQueryPrototype = (): void => {
/*! jQuery checkboxShiftClick | GPL-2.0 <qwbk.cc/H:GPL> */
Expand All @@ -16,7 +17,7 @@ const extendJQueryPrototype = (): void => {
}

// Highlight last selected
$(document).find(`.${CLASS_NAME_LABEL_LAST_SELECTED}`).removeClass(CLASS_NAME_LABEL_LAST_SELECTED);
$document.find(`.${CLASS_NAME_LABEL_LAST_SELECTED}`).removeClass(CLASS_NAME_LABEL_LAST_SELECTED);

let $thisControl: JQuery = $(event.target);
if (!$thisControl.hasClass(CLASS_NAME_LABEL)) {
Expand Down
4 changes: 2 additions & 2 deletions src/ConfirmLogout/modules/addListener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {$body, oouiConfirmWithStyle} from '../../util';
import {WG_SKIN} from './constant';
import {getMessage} from './i18n';
import {logout} from './logout';
import {oouiConfirmWithStyle} from '../../util';
import {refreshEventListener} from './util/refreshEventListener';

const addListener = ($element: JQuery<HTMLAnchorElement>): void => {
Expand Down Expand Up @@ -30,7 +30,7 @@ const addListener = ($element: JQuery<HTMLAnchorElement>): void => {
}

const observerCallback = (_mutations: MutationRecord[], observer: MutationObserver): void => {
if (!$(document.body).hasClass('vector-sticky-header-visible')) {
if (!$body.hasClass('vector-sticky-header-visible')) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion src/CookieWarning/modules/insertElement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {CLASS_CONSENT_NOTICE, STORAGE_KEY} from './constant';
import {$body} from '../../util';

import {getMessage} from './i18n';

const insertElement = (): void => {
Expand All @@ -16,7 +18,7 @@ const insertElement = (): void => {
mw.storage.set(STORAGE_KEY, '1', 60 * 60 * 1000 * 24 * 30);
$consentNotice.fadeOut().remove();
});
$consentNotice.appendTo($('body'));
$consentNotice.appendTo($body);
};

export {insertElement};
9 changes: 5 additions & 4 deletions src/DefaultSummaries/modules/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {articleSummaries, commonSummaries, talkPageSummaries} from './messages';
import {$body} from '../../util';

let $wpSummary: JQuery = $('#wpSummary');
let $wpSummary: JQuery = $body.find('#wpSummary');

const addOptionsToDropdown = (dropdown: OO.ui.DropdownWidget, optionTexts: string[]): void => {
dropdown.getMenu().addItems(
Expand Down Expand Up @@ -48,10 +49,10 @@ const getSummaryDropdowns = (): JQuery => {
// VisualEditor
const loadDefaultSummariesInVE = (): void => {
// .ve-init-mw-viewPageTarget-saveDialog-checkboxes
if ($('body').data('wppresent')) {
if ($body.data('wppresent')) {
return;
}
$('body').data('wppresent', 'true');
$body.data('wppresent', 'true');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const {target} = ve.init;
Expand All @@ -66,7 +67,7 @@ const loadDefaultSummariesInVE = (): void => {

// WikiEditor
const loadDefaultSummariesInWikiEditor = (): void => {
const $editCheckboxes: JQuery = $('.editCheckboxes');
const $editCheckboxes: JQuery = $body.find('.editCheckboxes');
// If we failed to find the editCheckboxes class
if (!$editCheckboxes.length) {
return;
Expand Down
3 changes: 2 additions & 1 deletion src/DiffLink/modules/addHook.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {$body} from '../../util';
import {checkRevisionPage} from './util/checkRevisionPage';
import {processId} from './processId';

Expand All @@ -7,7 +8,7 @@ const addHook = (): void => {
return;
}

if (!(checkRevisionPage() || $('table').hasClass('diff'))) {
if (!(checkRevisionPage() || $body.find('table').hasClass('diff'))) {
return;
}

Expand Down
5 changes: 4 additions & 1 deletion src/DiffLink/modules/util/checkRevisionPage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {$body} from '../../../util';

const checkRevisionPage = (): boolean => {
return (
($('#contentSub').find('#mw-revision-nav').length || $('main#content>.pre-content #mw-revision-nav').length) > 0
($body.find('#contentSub').find('#mw-revision-nav').length ||
$body.find('main#content>.pre-content #mw-revision-nav').length) > 0
);
};

Expand Down
22 changes: 8 additions & 14 deletions src/DisamAssist/modules/core.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import {$body, $window, initMwApi} from '../../util.ts';
import {cfg} from './config';
import {txt} from './messages';

const api = new mw.Api({
ajax: {
headers: {
'Api-User-Agent': `Qiuwen/1.1 (DisamAssist/1.1; ${mw.config.get('wgWikiID')})`,
},
},
});
const api = initMwApi(`Qiuwen/1.1 (DisamAssist/1.1; ${mw.config.get('wgWikiID')})`);
let startLink;
let ui;
let links;
Expand Down Expand Up @@ -137,13 +132,13 @@ const createUI = () => {
updateEditCounter();
toggleActionButtons(false);
// Insert the UI in the page
$('#mw-content-text').before(ui.display);
$body.find('#mw-content-text').before(ui.display);
ui.display.hide().fadeIn();
};

/* If there are pending changes, show a confirm dialog before closing */
const addUnloadConfirm = () => {
$(window).on('beforeunload', () => {
$window.on('beforeunload', () => {
if (running && checkActualChanges()) {
return txt.pending;
} else if (editCount !== 0) {
Expand Down Expand Up @@ -424,7 +419,7 @@ const togglePendingEditBox = (show) => {
if (editLimit) {
pendingEditBox.append($('<div>').text(txt.pendingEditBoxLimited).addClass('disamassist-subtitle'));
}
$('#mw-content-text').before(pendingEditBox);
$body.find('#mw-content-text').before(pendingEditBox);
updateEditCounter();
}
if (show) {
Expand All @@ -437,7 +432,6 @@ const togglePendingEditBox = (show) => {
const notifyCompletion = () => {
const oldTitle = document.title;
document.title = txt.notifyCharacter + document.title;
const $body = $('body');
$body.one('mousemove', () => {
document.title = oldTitle;
});
Expand Down Expand Up @@ -580,7 +574,7 @@ const countActuallyChangedFullyCheckedPages = () => {

/* Find the links to disambiguation options in a disambiguation page */
const getDisamOptions = () => {
return $('#mw-content-text a').filter((_index, element) => {
return $body.find('#mw-content-text a').filter((_index, element) => {
return !!extractPageName($(element));
});
};
Expand All @@ -597,7 +591,7 @@ const end = () => {
choosing = false;
running = false;
startLink.removeClass('selected');
$('.disamassist-optionmarker').remove();
$body.find('.disamassist-optionmarker').remove();
currentToolUI.fadeOut({
complete() {
currentToolUI.remove();
Expand All @@ -618,7 +612,7 @@ const error = (errorDescription) => {
}).addClass('disamassist-errorbutton')
);
const uiIsInPlace = ui && $.contains(document.documentElement, ui.display[0]);
const nextElement = uiIsInPlace ? ui.display : $('#mw-content-text');
const nextElement = uiIsInPlace ? ui.display : $body.find('#mw-content-text');
nextElement.before(errorBox);
errorBox.hide().fadeIn();
};
Expand Down
17 changes: 9 additions & 8 deletions src/EasyArchive/EasyArchive.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {$body} from '../util.ts';
import {easy_archive_lang} from './modules/i18n';

(function easyArchive() {
Expand Down Expand Up @@ -549,22 +550,22 @@ import {easy_archive_lang} from './modules/i18n';
);
}
if (mw.config.get('skin') === 'citizen') {
$('#easy_archive_enable_notice, #easy_archive_supports_notice, #easy_archive_stop_notice').replaceWith(
function () {
$body
.find('#easy_archive_enable_notice, #easy_archive_supports_notice, #easy_archive_stop_notice')
.replaceWith(function () {
const $this = $(this);
return $('<section>').addClass('page-info__item').attr('id', $this.attr('id')).html($this.html());
}
);
});
}
if (
['vector', 'vector-2022', 'gongbi', 'write'].includes(mw.config.get('skin')) ||
document.querySelector('ul#footer-info')
) {
$('#easy_archive_enable_notice, #easy_archive_supports_notice, #easy_archive_stop_notice').replaceWith(
function () {
$body
.find('#easy_archive_enable_notice, #easy_archive_supports_notice, #easy_archive_stop_notice')
.replaceWith(function () {
const $this = $(this);
return $('<li>').attr('id', $this.attr('id')).html($this.html());
}
);
});
}
})();
10 changes: 7 additions & 3 deletions src/Edit0/modules/core.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import {$body} from '../../util';
import {getMessage} from './i18n';

export const edit0 = (): void => {
const localTitle: string = getMessage('Edit0');
const $ourContent: JQuery = $('#content, #mw_content').first();
const $ourContent: JQuery = $body.find('#content, #mw_content').first();

const $span1: JQuery = $ourContent.find('span.mw-editsection:not(.plainlinks)').first();
if (!$span1.length) {
return;
}
const $span0: JQuery = $span1.clone();
$('body:not(.skin-citizen) #content h1#firstHeading').append($span0);
$('body.skin-citizen .mw-indicators').prepend($span0);

$body.find('body:not(.skin-citizen) #content h1#firstHeading').append($span0);
$body.find('body.skin-citizen .mw-indicators').prepend($span0);

$span0.find('a').each((_index: number, element: HTMLAnchorElement): void => {
const $a: JQuery<HTMLAnchorElement> = $(element);
const href: string = $a.attr('href') ?? '';
Expand Down
9 changes: 5 additions & 4 deletions src/EditForm_JS/modules/aiAssisted.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {$body} from '../../util';
import {getMessage} from './i18n';

export const AiAssisted = (): void => {
Expand All @@ -8,8 +9,8 @@ export const AiAssisted = (): void => {
}
// @ts-ignore
const $layout: JQuery = ve.init
? $('.ve-ui-mwSaveDialog-checkboxes')
: $('#editform').find('.editCheckboxes .oo-ui-horizontalLayout');
? $body.find('.ve-ui-mwSaveDialog-checkboxes')
: $body.find('#editform').find('.editCheckboxes .oo-ui-horizontalLayout');
if (!$layout.length) {
return;
}
Expand Down Expand Up @@ -39,9 +40,9 @@ export const AiAssisted = (): void => {
type: 'hidden',
value: '',
});
const $wpChangeTags: JQuery = $('#wpChangeTags');
const $wpChangeTags: JQuery = $body.find('#wpChangeTags');
if (!$wpChangeTags.length) {
$('#editform').prepend($tagInput);
$body.find('#editform').prepend($tagInput);
}
changeTags = String($wpChangeTags.val());
changeTags = checkbox.isSelected() ? `${changeTags},${tagName}` : changeTags.replace(`,${tagName}`, '');
Expand Down
7 changes: 4 additions & 3 deletions src/EditForm_JS/modules/antiConv.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {$body} from '../../util';
import {getMessage} from './i18n';

export const antiConv = (): void => {
// 检测客户端繁简转换
$('#antispam-container').append(
$body.find('#antispam-container').append(
$('<input>').attr({
id: 'wpAntiConv',
type: 'text',
value: '\u6C49\u6F22',
})
);
const checkAntiConv = (): void => {
const $wpAntiConv = $('#wpAntiConv');
const $wpAntiConv = $body.find('#wpAntiConv');
if ($wpAntiConv.length > 0 && $wpAntiConv.val() !== '\u6C49\u6F22') {
$('#editform :input').attr({disabled: 'disabled', readonly: 'readonly'});
$body.find('#editform :input').attr({disabled: 'disabled', readonly: 'readonly'});
OO.ui.alert(getMessage('ConversionSoftware'), {size: 'medium'});
} else {
setTimeout(checkAntiConv, 1000);
Expand Down
Loading

0 comments on commit ac39576

Please sign in to comment.