Skip to content

Commit

Permalink
fix: unexpected exception in debug mode
Browse files Browse the repository at this point in the history
TypeError: mw.Api is not a constructor
  • Loading branch information
AnYiEE committed Jan 5, 2024
1 parent 6f69d06 commit 771526c
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 42 deletions.
9 changes: 2 additions & 7 deletions src/method/initQueryData.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
const mwApi = new mw.Api({
parameters: {
format: 'json',
formatversion: 2,
},
})
import { useMwApi } from '../utils/mw'

export async function initQueryData() {
// Init
Expand All @@ -13,7 +8,7 @@ export async function initQueryData() {

const {
query: { users, userinfo, specialpagealiases },
} = await mwApi.get({
} = await useMwApi().get({
action: 'query',
ususers: mw.config.get('wgUserName'),
meta: ['userinfo', 'siteinfo'],
Expand Down
2 changes: 1 addition & 1 deletion src/module/articleLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function articleLink(elements) {
class: 'in-page-edit-article-link',
text: _msg('quick-edit'),
}).on('click', function (event) {
event.preventDefault();
event.preventDefault()
var options = {}
options.page = title
if (revision !== null) {
Expand Down
4 changes: 2 additions & 2 deletions src/module/linksHere.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { _analytics } from './_analytics'
import { $progress, $link } from './_elements'
import { _msg } from './_msg'
import { mwApi, mwConfig } from '../utils/mw'
import { useMwApi, mwConfig } from '../utils/mw'
import { quickEdit } from './quickEdit'

/**
Expand Down Expand Up @@ -34,7 +34,7 @@ const getList = (title) => {
} else {
opt.lhlimit = 'max'
}
return mwApi.get(opt)
return useMwApi().get(opt)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/module/quickDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { _analysis } from './_analytics'
import { _msg } from './_msg'
import { hasRight } from '../utils/hasRight'
import { $br } from './_elements'
import { mwApi, mwConfig } from '../utils/mw'
import { useMwApi, mwConfig } from '../utils/mw'

/**
* @module quickDelete 删除页面模块
Expand Down Expand Up @@ -89,7 +89,7 @@ export function quickDelete(page, givenReason = '') {
function (result) {
if (result) {
reason = _msg('delete-title') + ' (' + reason + ')'
mwApi
useMwApi()
.postWithToken('csrf', {
action: 'delete',
title: page,
Expand Down
4 changes: 2 additions & 2 deletions src/module/quickDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { _analysis } from './_analytics'
import { _msg } from './_msg'
import { $br, $progress } from './_elements'
import { articleLink } from './articleLink'
import { mwApi } from '../utils/mw'
import { useMwApi } from '../utils/mw'

/**
* @module quickDiff 快速页面差异模块
Expand Down Expand Up @@ -55,7 +55,7 @@ export function quickDiff(param) {
} else if (param.fromtext) {
param.frompst = true
}
mwApi
useMwApi()
.post(param)
.done(function (data) {
const compareTableBody = data.compare.body
Expand Down
16 changes: 8 additions & 8 deletions src/module/quickEdit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mwApi, mwConfig } from '../utils/mw'
import { useMwApi, mwConfig } from '../utils/mw'

import { _analysis } from './_analytics'
import { _msg } from './_msg'
Expand Down Expand Up @@ -252,7 +252,7 @@ export function quickEdit(options) {
if (options.section === 'new') {
sectiontitle = $newSectionTitleInput.val()
const anchor = (
await mwApi.post({
await useMwApi().post({
action: 'parse',
text: `==${sectiontitle}==`,
contentmodel: 'wikitext',
Expand Down Expand Up @@ -415,7 +415,7 @@ export function quickEdit(options) {
}

// 解析页面内容
mwApi
useMwApi()
.get(options.jsonGet)
.done(function (data) {
console.timeEnd('[InPageEdit] 获取页面源代码')
Expand Down Expand Up @@ -516,7 +516,7 @@ export function quickEdit(options) {
} else {
queryJson.titles = options.page
}
mwApi
useMwApi()
.get(queryJson)
.done(function (data) {
console.info('[InPageEdit] 获取页面基础信息成功')
Expand Down Expand Up @@ -620,7 +620,7 @@ export function quickEdit(options) {
''
) // 去掉名字空间

mwApi
useMwApi()
.get({
action: 'query',
meta: 'allmessages',
Expand All @@ -634,7 +634,7 @@ export function quickEdit(options) {

if (!fullWikitext.trim()) return

mwApi
useMwApi()
.post({
action: 'parse',
text: fullWikitext,
Expand Down Expand Up @@ -845,7 +845,7 @@ export function quickEdit(options) {
},
],
onShow() {
mwApi
useMwApi()
.get({
action: 'query',
format: 'json',
Expand Down Expand Up @@ -898,7 +898,7 @@ export function quickEdit(options) {
options.jumpTo = '#' + sectiontitle
}

mwApi
useMwApi()
.postWithToken('csrf', options.jsonPost)
.done(saveSuccess)
.fail(saveError)
Expand Down
4 changes: 2 additions & 2 deletions src/module/quickPreview.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { _msg } from './_msg'
import { $progress } from './_elements'
import { mwApi } from '../utils/mw'
import { useMwApi } from '../utils/mw'

/**
* @module quickPreview 快速预览文章页
Expand Down Expand Up @@ -50,7 +50,7 @@ export function quickPreview(params, modalSize = 'large', center = false) {
onShow() {
$loading.css('margin-top', window.innerHeight / 2 - 100)
$('.previewbox .hideThisBtn').hide()
mwApi
useMwApi()
.post(options)
.then(function (data) {
console.timeEnd('[InPageEdit] Request preview')
Expand Down
9 changes: 6 additions & 3 deletions src/module/quickRedirect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { _analysis } from './_analytics'
import { _msg } from './_msg'
import { $br, $progress } from './_elements'
import { mwApi, mwConfig } from '../utils/mw'
import { useMwApi, mwConfig } from '../utils/mw'
import { _resolveExists } from './_resolveExists'
import { preference } from './preference'

Expand Down Expand Up @@ -119,7 +119,7 @@ export function quickRedirect(type = 'to') {

let promise = Promise.resolve()
if (preference.get('noRedirectIfConvertedTitleExists')) {
promise = mwApi
promise = useMwApi()
.get({ titles: json.title, converttitles: 1 })
.done((data) => {
const convertedTitle = data.query.pages[0]
Expand All @@ -142,7 +142,10 @@ export function quickRedirect(type = 'to') {
}
promise.then(
() => {
mwApi.postWithToken('csrf', json).done(successed).fail(failed)
useMwApi()
.postWithToken('csrf', json)
.done(successed)
.fail(failed)
},
() => {}
)
Expand Down
4 changes: 2 additions & 2 deletions src/module/quickRename.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { _msg } from './_msg'
import { hasRight } from '../utils/hasRight'
import { _resolveExists } from './_resolveExists'
import { $br } from './_elements'
import { mwApi, mwConfig } from '../utils/mw'
import { useMwApi, mwConfig } from '../utils/mw'
import { progress } from './progress'

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ export function quickRename(from, to) {
reason =
_msg('rename-summary') + ' → [[:' + to + ']] (' + reason + ')'
}
mwApi
useMwApi()
.postWithToken('csrf', {
action: 'move',
from,
Expand Down
6 changes: 3 additions & 3 deletions src/module/syncPreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 将 IPE 的参数设置保存在数据库内,实现多端同步
// ========================================= //

import { mwApi } from '../utils/mw'
import { useMwApi } from '../utils/mw'

/**
* @param {string} name
Expand All @@ -21,7 +21,7 @@ export async function setPreferences(name, value) {
...mw.user.options.value[prefKey(name)],
...value,
}
return mwApi.postWithToken('csrf', {
return useMwApi().postWithToken('csrf', {
format: 'json',
action: 'options',
change: new URLSearchParams({
Expand All @@ -35,7 +35,7 @@ export async function setPreferences(name, value) {
* @returns {Promise<Record<string, any>>}
*/
export async function getPreferences(name) {
const data = await mwApi.get({
const data = await useMwApi().get({
format: 'json',
action: 'query',
meta: 'userinfo',
Expand Down
21 changes: 11 additions & 10 deletions src/utils/mw.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import version from '../module/version'

export const mwConfig = mw.config.get()
export const mwApi = new mw.Api({
parameters: {
formatversion: 2,
format: 'json',
},
ajax: {
headers: {
'Api-User-Agent': `InPageEdit-v2/${version}`,
export const useMwApi = () =>
new mw.Api({
parameters: {
formatversion: 2,
format: 'json',
},
},
})
ajax: {
headers: {
'Api-User-Agent': `InPageEdit-v2/${version}`,
},
},
})

0 comments on commit 771526c

Please sign in to comment.