「MediaWiki:Common.js」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
Deploy requested random talent icon cards from wiki PR 5; preserve existing code |
||
| 47行目: | 47行目: | ||
}); | }); | ||
}); | }); | ||
/* StartLeno top-page talent cards. | |||
* 既存の MediaWiki:Common.js の末尾に追加してください。 | |||
* トップページに id="sl-random-talent-cards" がある場合だけ動作します。 | |||
* 出典は Category:所属タレント と、各記事の | 画像 = 欄です。 | |||
*/ | |||
(function () { | |||
'use strict'; | |||
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () { | |||
$(function () { | |||
var root = document.getElementById('sl-random-talent-cards'); | |||
if (!root) { | |||
return; | |||
} | |||
var api = new mw.Api(); | |||
var limit = 6; | |||
function iconName(source) { | |||
var match = source.match(/^[ \t]*\|[ \t]*画像[ \t]*=[ \t]*([^\r\n|}]*)/m); | |||
if (!match) { | |||
return null; | |||
} | |||
var name = match[1].replace(/<!--.*$/, '').trim() | |||
.replace(/^(?:File|Image|ファイル|画像)\s*:\s*/i, ''); | |||
return name && !/[<>{}\[\]|]/.test(name) ? name : null; | |||
} | |||
async function members() { | |||
var list = []; | |||
var cursor = null; | |||
do { | |||
var params = { | |||
action: 'query', | |||
list: 'categorymembers', | |||
cmtitle: 'Category:所属タレント', | |||
cmtype: 'page', | |||
cmnamespace: 0, | |||
cmlimit: 'max', | |||
formatversion: 2 | |||
}; | |||
if (cursor) { | |||
params.cmcontinue = cursor; | |||
} | |||
var result = await api.get(params); | |||
var items = result.query && result.query.categorymembers || []; | |||
list.push.apply(list, items); | |||
var next = result.continue && result.continue.cmcontinue || null; | |||
if (cursor === next) { | |||
break; | |||
} | |||
cursor = next; | |||
} while (cursor); | |||
return list; | |||
} | |||
function canLoad(url) { | |||
return new Promise(function (resolve) { | |||
var image = new Image(); | |||
image.onload = function () { resolve(true); }; | |||
image.onerror = function () { resolve(false); }; | |||
image.src = url; | |||
}); | |||
} | |||
async function validCard(member) { | |||
// カテゴリのページIDから本文を再確認し、存在しないページを除外します。 | |||
var pageResult = await api.get({ | |||
action: 'query', | |||
prop: 'revisions', | |||
pageids: member.pageid, | |||
rvprop: 'content', | |||
rvslots: 'main', | |||
rvlimit: 1, | |||
formatversion: 2 | |||
}); | |||
var page = pageResult.query && pageResult.query.pages && | |||
pageResult.query.pages[0]; | |||
var revision = page && page.revisions && page.revisions[0]; | |||
var source = revision && revision.slots && revision.slots.main && | |||
revision.slots.main.content; | |||
if (!page || page.missing || typeof source !== 'string') { | |||
return null; | |||
} | |||
var filename = iconName(source); | |||
if (!filename) { | |||
return null; | |||
} | |||
var fileResult = await api.get({ | |||
action: 'query', | |||
prop: 'imageinfo', | |||
titles: 'File:' + filename, | |||
iiprop: 'url', | |||
iiurlwidth: 160, | |||
formatversion: 2 | |||
}); | |||
var filePage = fileResult.query && fileResult.query.pages && | |||
fileResult.query.pages[0]; | |||
var info = filePage && filePage.imageinfo && filePage.imageinfo[0]; | |||
var url = info && (info.thumburl || info.url); | |||
if (!url || !(await canLoad(url))) { | |||
return null; | |||
} | |||
return { title: page.title, url: url }; | |||
} | |||
function draw(items) { | |||
var cards = document.createDocumentFragment(); | |||
items.forEach(function (item) { | |||
var link = document.createElement('a'); | |||
link.href = mw.util.getUrl(item.title); | |||
link.style.cssText = 'width:140px;max-width:42vw;display:flex;flex-direction:column;align-items:center;gap:8px;text-align:center;text-decoration:none;'; | |||
var icon = document.createElement('img'); | |||
icon.src = item.url; | |||
icon.alt = ''; | |||
icon.width = 108; | |||
icon.height = 108; | |||
icon.style.cssText = 'width:108px;height:108px;max-width:100%;border-radius:50%;object-fit:cover;'; | |||
icon.onerror = function () { link.remove(); }; | |||
var name = document.createElement('span'); | |||
name.textContent = item.title; | |||
name.style.fontWeight = 'bold'; | |||
link.appendChild(icon); | |||
link.appendChild(name); | |||
cards.appendChild(link); | |||
}); | |||
root.replaceChildren(cards); | |||
} | |||
async function start() { | |||
var people = await members(); | |||
// シャッフルしてから有効な画像だけ最大6件選ぶ。 | |||
for (var i = people.length - 1; i > 0; i--) { | |||
var j = Math.floor(Math.random() * (i + 1)); | |||
var temp = people[i]; | |||
people[i] = people[j]; | |||
people[j] = temp; | |||
} | |||
var valid = []; | |||
for (var n = 0; n < people.length && valid.length < limit; n++) { | |||
try { | |||
var item = await validCard(people[n]); | |||
if (item) { | |||
valid.push(item); | |||
} | |||
} catch (e) { | |||
// 個別の取得エラーは他の候補に影響させない。 | |||
} | |||
} | |||
draw(valid); | |||
} | |||
start().catch(function () { | |||
// APIエラー時もリンク切れや「読み込み中」を残さない。 | |||
root.replaceChildren(); | |||
}); | |||
}); | |||
}); | |||
}()); | |||
2026年11月16日 (月) 06:36時点における版
mw.loader.using(["mediawiki.api", "mediawiki.util", "jquery"]).then(function () {
jQuery(function ($) {
var box = document.getElementById("random-talent-box");
if (!box) {
return;
}
var api = new mw.Api();
api.get({
action: "query",
list: "categorymembers",
cmtitle: "Category:所属タレント",
cmlimit: "max",
cmnamespace: 0,
format: "json"
}).then(function (data) {
var members = (data.query && data.query.categorymembers) || [];
if (!members.length) {
box.innerHTML = "所属タレントが見つかりませんでした。";
return;
}
var picked = members[Math.floor(Math.random() * members.length)];
var title = picked.title;
var pageUrl = mw.util.getUrl(title);
var categoryUrl = mw.util.getUrl("Category:所属タレント");
box.innerHTML =
'<div style="height:100%;display:flex;flex-direction:column;justify-content:space-between;border:1px solid #eee;border-radius:12px;background:#fff;padding:18px;">' +
'<div>' +
'<div style="font-size:90%;color:#666;margin-bottom:10px;letter-spacing:.03em;">PICKUP TALENT</div>' +
'<div style="font-size:180%;font-weight:800;line-height:1.3;margin-bottom:12px;">' +
'<a href="' + pageUrl + '" style="text-decoration:none;">' + title + '</a>' +
'</div>' +
'<div style="opacity:.8;line-height:1.8;">StartLeno所属タレントから、ランダムで1名を表示しています。</div>' +
'</div>' +
'<div style="margin-top:18px;display:flex;gap:10px;flex-wrap:wrap;">' +
'<a href="' + pageUrl + '" style="display:inline-block;padding:8px 12px;border:1px solid #ccc;border-radius:999px;text-decoration:none;">プロフィールを見る</a>' +
'<a href="' + categoryUrl + '" style="display:inline-block;padding:8px 12px;border:1px solid #ccc;border-radius:999px;text-decoration:none;">一覧を見る</a>' +
'</div>' +
'</div>';
}).catch(function (e) {
box.innerHTML = "所属タレントの読み込みに失敗しました: " + String(e);
});
});
});
/* StartLeno top-page talent cards.
* 既存の MediaWiki:Common.js の末尾に追加してください。
* トップページに id="sl-random-talent-cards" がある場合だけ動作します。
* 出典は Category:所属タレント と、各記事の | 画像 = 欄です。
*/
(function () {
'use strict';
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () {
$(function () {
var root = document.getElementById('sl-random-talent-cards');
if (!root) {
return;
}
var api = new mw.Api();
var limit = 6;
function iconName(source) {
var match = source.match(/^[ \t]*\|[ \t]*画像[ \t]*=[ \t]*([^\r\n|}]*)/m);
if (!match) {
return null;
}
var name = match[1].replace(/<!--.*$/, '').trim()
.replace(/^(?:File|Image|ファイル|画像)\s*:\s*/i, '');
return name && !/[<>{}\[\]|]/.test(name) ? name : null;
}
async function members() {
var list = [];
var cursor = null;
do {
var params = {
action: 'query',
list: 'categorymembers',
cmtitle: 'Category:所属タレント',
cmtype: 'page',
cmnamespace: 0,
cmlimit: 'max',
formatversion: 2
};
if (cursor) {
params.cmcontinue = cursor;
}
var result = await api.get(params);
var items = result.query && result.query.categorymembers || [];
list.push.apply(list, items);
var next = result.continue && result.continue.cmcontinue || null;
if (cursor === next) {
break;
}
cursor = next;
} while (cursor);
return list;
}
function canLoad(url) {
return new Promise(function (resolve) {
var image = new Image();
image.onload = function () { resolve(true); };
image.onerror = function () { resolve(false); };
image.src = url;
});
}
async function validCard(member) {
// カテゴリのページIDから本文を再確認し、存在しないページを除外します。
var pageResult = await api.get({
action: 'query',
prop: 'revisions',
pageids: member.pageid,
rvprop: 'content',
rvslots: 'main',
rvlimit: 1,
formatversion: 2
});
var page = pageResult.query && pageResult.query.pages &&
pageResult.query.pages[0];
var revision = page && page.revisions && page.revisions[0];
var source = revision && revision.slots && revision.slots.main &&
revision.slots.main.content;
if (!page || page.missing || typeof source !== 'string') {
return null;
}
var filename = iconName(source);
if (!filename) {
return null;
}
var fileResult = await api.get({
action: 'query',
prop: 'imageinfo',
titles: 'File:' + filename,
iiprop: 'url',
iiurlwidth: 160,
formatversion: 2
});
var filePage = fileResult.query && fileResult.query.pages &&
fileResult.query.pages[0];
var info = filePage && filePage.imageinfo && filePage.imageinfo[0];
var url = info && (info.thumburl || info.url);
if (!url || !(await canLoad(url))) {
return null;
}
return { title: page.title, url: url };
}
function draw(items) {
var cards = document.createDocumentFragment();
items.forEach(function (item) {
var link = document.createElement('a');
link.href = mw.util.getUrl(item.title);
link.style.cssText = 'width:140px;max-width:42vw;display:flex;flex-direction:column;align-items:center;gap:8px;text-align:center;text-decoration:none;';
var icon = document.createElement('img');
icon.src = item.url;
icon.alt = '';
icon.width = 108;
icon.height = 108;
icon.style.cssText = 'width:108px;height:108px;max-width:100%;border-radius:50%;object-fit:cover;';
icon.onerror = function () { link.remove(); };
var name = document.createElement('span');
name.textContent = item.title;
name.style.fontWeight = 'bold';
link.appendChild(icon);
link.appendChild(name);
cards.appendChild(link);
});
root.replaceChildren(cards);
}
async function start() {
var people = await members();
// シャッフルしてから有効な画像だけ最大6件選ぶ。
for (var i = people.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = people[i];
people[i] = people[j];
people[j] = temp;
}
var valid = [];
for (var n = 0; n < people.length && valid.length < limit; n++) {
try {
var item = await validCard(people[n]);
if (item) {
valid.push(item);
}
} catch (e) {
// 個別の取得エラーは他の候補に影響させない。
}
}
draw(valid);
}
start().catch(function () {
// APIエラー時もリンク切れや「読み込み中」を残さない。
root.replaceChildren();
});
});
});
}());