User:Bosco/Unsigned helper.js: Difference between revisions

From Test Wiki
Content deleted Content added
reportSearcherResultToUser: set minWidth of the dialog to 20% of the screen width to fit the info from Special:Diff/1304091700
mNo edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 4: Line 4:
(function () {
(function () {
const DEBUG = false;
const DEBUG = false;
const LOG_PREFIX = `[Unsigned Helper]:`;
const LOG_PREFIX = `[簽名工具]:`;


function error(...toLog) {
function error(...toLog) {
Line 22: Line 22:
}
}


const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];


const CONFIG = {
const CONFIG = {
undated: 'Undated', // [[Template:Undated]]
undated: 'Undated', // [[Template:Undated]]
unsignedLoggedIn: 'Unsigned', // [[Template:Unsigned]]
unsignedLoggedIn: '未签名', // [[Template:Unsigned]]
unsignedIp: 'Unsigned IP', // [[Template:Unsigned IP]]
unsignedIp: '未签名', // [[Template:Unsigned]]
};
};


if (mw.config.get('wgAction') !== 'edit' && mw.config.get('wgAction') !== 'submit' && document.getElementById("editform") == null) {
if (mw.config.get('wgAction') !== 'edit' && mw.config.get('wgAction') !== 'submit' && document.getElementById("editform") == null) {
info('Not editing a page. Aborting.');
info('未編輯頁面。暫停中。');
return;
return;
}
}


info('Loading...');
info('載入中...');


function formatErrorSpan(errorMessage) {
function formatErrorSpan(errorMessage) {
return `<span style="color:maroon;"><b>Error:</b> ${errorMessage}</span>`;
return `<span style="color:maroon;"><b>錯誤:</b> ${errorMessage}</span>`;
}
}


Line 303: Line 303:
async function exponentialSearch(lower, upper, candidateIndex, testFunc) {
async function exponentialSearch(lower, upper, candidateIndex, testFunc) {
if (upper === null && lower === candidateIndex) {
if (upper === null && lower === candidateIndex) {
throw new Error(`Wrong arguments for exponentialSearch (${lower}, ${upper}, ${candidateIndex}).`);
throw new Error(`exponentialSearch 的錯誤參數(${lower}, ${upper}, ${candidateIndex}).`);
}
}
if (lower === upper && lower === candidateIndex) {
if (lower === upper && lower === candidateIndex) {
throw new Error("Cannot find it");
throw new Error("不能找到");
}
}
const progressMessage = `Examining [${lower}, ${upper ? upper : '...'}]. Current candidate: ${candidateIndex}`;
const progressMessage = `Examining [${lower}, ${upper ? upper : '...'}]. Current candidate: ${candidateIndex}`;
Line 357: Line 357:
if (startRevision == undefined) {
if (startRevision == undefined) {
if (startIndex === 0) {
if (startIndex === 0) {
reject("Cannot find the latest revision. Does this page exist?");
reject("不能找到最新版本。此頁面存在嗎?");
} else {
} else {
reject(`Cannot find the start revision (index=${startIndex}).`);
reject(`不能尋找開始版本 (版本=${startIndex}).`);
}
}
return;
return;
Line 366: Line 366:
const latestFullRevision = await this.#contentLoader.loadContent(startIndex);
const latestFullRevision = await this.#contentLoader.loadContent(startIndex);
if (!latestFullRevision.slots.main.content.includes(text)) {
if (!latestFullRevision.slots.main.content.includes(text)) {
reject("Cannot find text in the latest revision. Did you edit it?");
reject("不能尋找最新版本的內容。你編輯了嗎?");
return;
return;
}
}
Line 383: Line 383:
* ?
* ?
*/
*/
warn('testFunc: Cannot load the content for candidateIndex = ' + candidateIndex);
warn('測試功能:不能載入candidateIndex的內容 = ' + candidateIndex);
return undefined;
return undefined;
}
}
Line 389: Line 389:
return candidateFullRevision.slots.main.content.includes(text);
return candidateFullRevision.slots.main.content.includes(text);
} catch (e) {
} catch (e) {
reject('testFunc: ' + e);
reject('測試內容: ' + e);
}
}
});
});
if (foundIndex === undefined) {
if (foundIndex === undefined) {
reject("Cannot find this text.");
reject("不能尋找這個內容");
return;
return;
}
}
Line 539: Line 539:


// TODO maybe migrate to https://www.mediawiki.org/wiki/OOUI/Windows/Message_Dialogs
// TODO maybe migrate to https://www.mediawiki.org/wiki/OOUI/Windows/Message_Dialogs
const mainDialog = $('<div>Examining...</div>').dialog({
const mainDialog = $('<div>檢查中...</div>').dialog({
buttons: {
buttons: {
Cancel: function () {
Cancel: function () {
Line 607: Line 607:
"The ",
"The ",
$('<a>').prop({
$('<a>').prop({
href: '/w/index.php?diff=prev&oldid=' + revid,
href: '/index.php?diff=prev&oldid=' + revid,
target: '_blank'
target: '_blank'
}).text(`found revision (index=${searcherResult.index})`),
}).text(`已尋找版本 (版本=${searcherResult.index})`),
" may be a revert: ",
" may be a revert: ",
comment
comment
Line 630: Line 630:
function formatSizeChange(afterSize, beforeSize) {
function formatSizeChange(afterSize, beforeSize) {
const change = afterSize - beforeSize;
const change = afterSize - beforeSize;
const title = `${afterSize} bytes after change of this size`;
const title = `編輯後共${afterSize}位元組`;
const titleAttribute = `title="${title}"`;
const titleAttribute = `title="${title}"`;
let style = '';
let style = '';
Line 662: Line 662:
"Found a revision: ",
"Found a revision: ",
$('<a>').prop({
$('<a>').prop({
href: '/w/index.php?diff=prev&oldid=' + revid,
href: '/index.php?diff=prev&oldid=' + revid,
target: '_blank'
target: '_blank'
}).text(`[[Special:Diff/${revid}]] (index=${searcherResult.index})`),
}).text(`[[Special:Diff/${revid}]] (index=${searcherResult.index})`),
Line 678: Line 678:
function searchFromIndex(index) {
function searchFromIndex(index) {
if (selection == undefined || selection == '') {
if (selection == undefined || selection == '') {
mainDialog.html(formatErrorSpan("Please select an unsigned message.") +
mainDialog.html(formatErrorSpan("請選擇一個未簽名的訊息") +
" Selected: <code>" + originalSelection + "</code>");
" Selected: <code>" + originalSelection + "</code>");
return;
return;
Line 704: Line 704:
reportNormalSearcherResultToUser(searcherResult, useCallback, keepLookingCallback, cancelCallback);
reportNormalSearcherResultToUser(searcherResult, useCallback, keepLookingCallback, cancelCallback);
}, rejection => {
}, rejection => {
error(`Searcher cannot find requested index=${index}. Got error:`, rejection);
error(`搜尋器不能尋找已要求的索引=${index}. Got error:`, rejection);
if (!mainDialog.dialog('isOpen')) {
if (!mainDialog.dialog('isOpen')) {
// user clicked [cancel]
// user clicked [cancel]