利用者:Mr.sirokuma/commons.js
ナビゲーションに移動
検索に移動
注意: 保存後、変更を確認するにはブラウザーのキャッシュを消去する必要がある場合があります。
- Firefox / Safari: Shift を押しながら 再読み込み をクリックするか、Ctrl-F5 または Ctrl-R を押してください (Mac では ⌘-R)
- Google Chrome: Ctrl-Shift-R を押してください (Mac では ⌘-Shift-R)
- Microsoft Edge: Ctrl を押しながら 最新の情報に更新 をクリックするか、Ctrl-F5 を押してください。
/**
* 個人メニューバーにカスタムリンクを追加
*/
$(function() {
// 1. サブサブページへのリンク
mw.util.addPortletLink(
'p-personal',
mw.util.getUrl('利用者:Mr.sirokuma/サブページ'),
'サブサブページ',
'pt-subsubpage',
'サブページへ移動します',
null,
'#pt-preferences' // 個人設定の前に挿入
);
// 2. 作成したページへのリンク
mw.util.addPortletLink(
'p-personal',
mw.util.getUrl('利用者:Mr.sirokuma/作成したページ'),
'作成したページ',
'pt-myworks',
'作成したページ一覧へ移動します',
null,
'#pt-preferences' // 同じく個人設定の前に挿入
);
});
/**
* サイドバーに編集回数カウントバーを設置(動的ステップ版)
*/
(function() {
const userName = mw.config.get('wgUserName');
if (!userName) return;
$.getJSON(mw.util.wikiScript('api'), {
action: 'query',
list: 'users',
ususers: userName,
usprop: 'editcount',
format: 'json'
}).done(function(data) {
const editCount = data.query.users[0].editcount;
let nextMilestone, prevMilestone;
// 1000回までは100刻み、それ以降は1000刻みで計算
if (editCount < 1000) {
nextMilestone = (Math.floor(editCount / 100) + 1) * 100;
prevMilestone = Math.floor(editCount / 100) * 100;
} else {
nextMilestone = (Math.floor(editCount / 1000) + 1) * 1000;
prevMilestone = Math.floor(editCount / 1000) * 1000;
}
const remaining = nextMilestone - editCount;
const progress = ((editCount - prevMilestone) / (nextMilestone - prevMilestone)) * 100;
const html = `
<nav id="p-edit-progress" class="mw-portlet vector-menu vector-menu-portal portal" style="margin-top: 1em; padding: 0 0.5em;">
<h3 style="font-size: 0.75em; font-weight: bold; color: #54595d; margin-bottom: 8px;">次まであと ${remaining} 編集</h3>
<div class="vector-menu-content">
<div style="background: #eaecf0; border-radius: 10px; height: 8px; width: 100%; overflow: hidden; border: 1px solid #c8ccd1;">
<div style="background: #36c; height: 100%; width: ${progress}%; transition: width 0.8s ease-in-out;"></div>
</div>
<div style="font-size: 0.7em; margin-top: 5px; color: #72777d; display: flex; justify-content: space-between;">
<span>現在: ${editCount}</span>
<span>目標: ${nextMilestone}</span>
</div>
</div>
</nav>
`;
// Vectorのサイドバー(複数の可能性を考慮)に挿入
const $target = $('#mw-panel').find('.vector-menu').last();
if ($target.length) {
$target.after(html);
} else {
$('#mw-panel, #p-navigation').append(html);
}
});
})();
/**
* サイドバー
*/
(function() {
let deck = [], playerHand = [], dealerHand = [], gameOver = false;
// カードの山を作成
function createDeck() {
const suits = ['♠', '♥', '♦', '♣'];
const values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
deck = [];
for (let s of suits) for (let v of values) deck.push({s, v});
deck = deck.sort(() => Math.random() - 0.5);
}
// 点数計算
function getScore(hand) {
let score = 0, aces = 0;
hand.forEach(c => {
if (['J', 'Q', 'K'].includes(c.v)) score += 10;
else if (c.v === 'A') { score += 11; aces++; }
else score += parseInt(c.v);
});
while (score > 21 && aces > 0) { score -= 10; aces--; }
return score;
}
const gameHtml = `
<nav id="p-blackjack" class="mw-portlet vector-menu vector-menu-portal portal" style="margin-top: 1.5em; padding: 10px 5px; border-top: 1px solid #c8ccd1; background: #f8f9fa;">
<h3 style="font-size: 0.8em; font-weight: bold; color: #54595d; margin-bottom: 5px;">ブラックジャック</h3>
<div id="bj-area" style="font-size: 0.75em; line-height: 1.4;">
<div id="bj-dealer">親: ?</div>
<div id="bj-player" style="margin: 5px 0; font-weight: bold;">子: </div>
<div id="bj-msg" style="color: #d33; font-weight: bold; min-height: 1.2em; margin-bottom: 5px;"></div>
<div id="bj-controls">
<button id="bj-hit" style="padding: 2px 5px; cursor:pointer;">ヒット</button>
<button id="bj-stand" style="padding: 2px 5px; cursor:pointer;">スタンド</button>
<button id="bj-start" style="padding: 2px 10px; cursor:pointer; display:none; background:#36c; color:#fff; border:none; border-radius:2px;">再戦</button>
</div>
</div>
</nav>
`;
// 挿入
const $target = $('#p-edit-progress, #mw-panel .vector-menu').last();
$target.length ? $target.after(gameHtml) : $('#mw-panel').append(gameHtml);
function updateDisplay(hideDealer = true) {
const dText = hideDealer ? `${dealerHand[0].s}${dealerHand[0].v} (+?)` :
dealerHand.map(c => c.s + c.v).join(' ') + ` (${getScore(dealerHand)})`;
$('#bj-dealer').text('親: ' + dText);
$('#bj-player').text('子: ' + playerHand.map(c => c.s + c.v).join(' ') + ` (${getScore(playerHand)})`);
}
function startGame() {
createDeck();
playerHand = [deck.pop(), deck.pop()];
dealerHand = [deck.pop(), deck.pop()];
gameOver = false;
$('#bj-msg').text('');
$('#bj-start').hide();
$('#bj-hit, #bj-stand').show();
updateDisplay(true);
if (getScore(playerHand) === 21) finish('BJ! 勝利!');
}
function finish(msg) {
gameOver = true;
updateDisplay(false);
$('#bj-msg').text(msg);
$('#bj-hit, #bj-stand').hide();
$('#bj-start').show().css('display', 'block');
}
$(document).on('click', '#bj-hit', function() {
playerHand.push(deck.pop());
updateDisplay(true);
if (getScore(playerHand) > 21) finish('バースト!負け');
});
$(document).on('click', '#bj-stand', function() {
while (getScore(dealerHand) < 17) dealerHand.push(deck.pop());
const p = getScore(playerHand), d = getScore(dealerHand);
if (d > 21) finish('親バースト!勝利');
else if (p > d) finish('勝利!');
else if (p < d) finish('敗北...');
else finish('引き分け');
});
$(document).on('click', '#bj-start', startGame);
// 初回起動
startGame();
})();
/**
* モバイルビューでもサイドバーを横に表示させるためのCSS注入
*/
(function() {
// モバイルビュー(Minervaスキン等)かどうか判定
if (mw.config.get('skin') === 'minerva' || window.innerWidth < 768) {
const customStyle = `
<style>
/* メインコンテナをフレックスボックス化 */
#mw-mf-viewport {
display: flex !important;
flex-direction: row !important;
overflow-x: auto !important;
}
/* コンテンツエリアを狭める */
#mw-mf-page-center {
flex: 0 0 70% !important;
width: 70% !important;
margin: 0 !important;
transform: none !important;
}
/* サイドバー(メニュー)を右側に強制表示 */
#mw-mf-page-left {
position: relative !important;
flex: 0 0 30% !important;
width: 30% !important;
left: 0 !important;
display: block !important;
background: #f8f9fa !important;
border-left: 1px solid #ddd !important;
visibility: visible !important;
}
/* ゲームやカウントバーのフォントサイズをさらに小さく */
#p-edit-progress, #p-blackjack {
font-size: 0.6em !important;
padding: 5px 2px !important;
}
#bj-controls button {
padding: 1px 2px !important;
font-size: 0.8em !important;
}
</style>
`;
$('head').append(customStyle);
}
})();
/**
* 編集画面用・検索小窓(軽量・高速版)
*/
(function(mw, $) {
const action = mw.config.get('wgAction');
if (!['edit', 'submit'].includes(action)) return;
const isMobile = window.innerWidth < 768;
// スタイルを一括注入(JSでの.css()呼び出しを減らして描画を速くする)
$('<style>').text(`
#ehp-panel {
position: fixed; top: 60px; right: ${isMobile ? '2.5%' : '10px'};
width: ${isMobile ? '95%' : '400px'}; height: 600px; max-height: 85vh;
background: #fff; border: 1px solid #a2a9b1; border-radius: 8px;
box-shadow: 0 4px 15px rgba(0,0,0,0.3); z-index: 10001;
display: none; flex-direction: column; overflow: hidden; touch-action: none;
}
#ehp-panel.active { display: flex !important; }
#ehp-header { background: #f8f9fa; padding: 10px 12px; border-bottom: 1px solid #a2a9b1; cursor: move; display: flex; justify-content: space-between; align-items: center; user-select: none; }
#ehp-frame { width: 100%; flex-grow: 1; border: none; background: #fff; min-height: 300px; }
#ehp-launch {
position: fixed; bottom: 80px; right: 20px; width: 50px; height: 50px;
background: #3366cc; color: #fff; border-radius: 50%; text-align: center;
line-height: 50px; cursor: pointer; box-shadow: 0 4px 10px rgba(0,0,0,0.4);
z-index: 10000; font-size: 20px; transition: transform 0.2s;
}
#ehp-launch:active { transform: scale(0.9); }
.ehp-search-box { padding: 10px; border-bottom: 1px solid #eee; display: flex; gap: 5px; }
.ehp-input { flex-grow: 1; font-size: 16px; padding: 6px; border: 1px solid #ccc; border-radius: 4px; }
`).appendTo('head');
// パネルの構築
const $panel = $(`
<div id="ehp-panel">
<div id="ehp-header"><strong>🔍 検索小窓</strong><button id="ehp-close" style="background:none;border:none;font-size:20px;cursor:pointer;">×</button></div>
<div class="ehp-search-box">
<input type="text" id="ehp-query" class="ehp-input" placeholder="Enpedia内を検索...">
<button id="ehp-go" style="padding:6px 12px; background:#3366cc; color:#fff; border:none; border-radius:4px; cursor:pointer;">Go</button>
</div>
<iframe id="ehp-frame" src="${mw.util.getUrl('メインページ')}"></iframe>
</div>
`);
const $launchBtn = $('<div id="ehp-launch">🔍</div>');
const $frame = $panel.find('#ehp-frame');
const $input = $panel.find('#ehp-query');
$('body').append($panel, $launchBtn);
// 検索処理
const doSearch = () => {
const q = $input.val().trim();
if (q) {
$frame.attr('src', `${mw.config.get('wgScript')}?title=Special:Search&search=${encodeURIComponent(q)}`);
$input.blur();
}
};
// イベント一括設定
$launchBtn.on('click', () => $panel.toggleClass('active'));
$panel.on('click', '#ehp-close', () => $panel.removeClass('active'));
$panel.on('click', '#ehp-go', doSearch);
$input.on('keypress', (e) => e.which === 13 && doSearch());
// ドラッグ移動(必要最小限の計算)
let drag = false, offset = { x: 0, y: 0 };
$panel.on('mousedown touchstart', '#ehp-header', function(e) {
if (e.target.id === 'ehp-close') return;
drag = true;
const ev = e.type === 'touchstart' ? e.originalEvent.touches[0] : e;
offset.x = ev.clientX - $panel.offset().left;
offset.y = ev.clientY - $panel.offset().top;
e.preventDefault();
});
$(document).on('mousemove touchmove', (e) => {
if (!drag) return;
const ev = e.type === 'touchmove' ? e.originalEvent.touches[0] : e;
$panel.css({
left: ev.clientX - offset.x,
top: ev.clientY - offset.y,
right: 'auto', bottom: 'auto'
});
}).on('mouseup touchend', () => drag = false);
})(mediaWiki, jQuery);
/**
* 編集画面用:独立Google検索窓(別タブ表示)
*/
(function() {
const action = mw.config.get('wgAction');
if (action !== 'edit' && action !== 'submit') return;
// 1. 検索バーのHTML(初期は非表示)
const $gooBar = $(`
<div id="google-search-bar" style="position: fixed; bottom: 140px; right: 80px; background: #fff; border: 2px solid #ea4335; border-radius: 25px; padding: 5px 15px; box-shadow: 0 4px 10px rgba(0,0,0,0.2); z-index: 10001; display: none; align-items: center; gap: 8px;">
<input type="text" id="goo-input" placeholder="Googleで検索..." style="border: none; outline: none; font-size: 16px; width: 180px; padding: 5px;">
<button id="goo-exec" style="background: #ea4335; color: #fff; border: none; border-radius: 15px; padding: 5px 12px; cursor: pointer; font-size: 14px; font-weight: bold;">Go</button>
</div>
`);
// 2. 起動用ボタン(赤いGボタン)
const $gooBtn = $(`
<div id="goo-launcher" style="position: fixed; bottom: 140px; right: 20px; width: 50px; height: 50px; background: #ea4335; color: #fff; border-radius: 50%; text-align: center; line-height: 50px; cursor: pointer; box-shadow: 0 2px 8px rgba(0,0,0,0.3); z-index: 10000; font-size: 22px; font-weight: bold; user-select: none;" title="Google検索を開く">
G
</div>
`);
$('body').append($gooBar).append($gooBtn);
// 検索実行関数
function doGoogleSearch() {
const query = $('#goo-input').val();
if (query) {
// 別タブでGoogle検索を開く
window.open(`https://www.google.com/search?q=${encodeURIComponent(query)}`, '_blank');
// 入力後、バーを閉じる(お好みで消してください)
$gooBar.fadeOut(200);
}
}
// クリックでバーを表示/非表示
$gooBtn.on('click', function() {
$gooBar.fadeToggle(200, function() {
if ($gooBar.is(':visible')) $('#goo-input').focus();
});
});
// Goボタンクリック
$(document).on('click', '#goo-exec', doGoogleSearch);
// エンターキー対応
$(document).on('keypress', '#goo-input', function(e) {
if (e.which === 13) {
e.preventDefault();
doGoogleSearch();
}
});
// 画面の他の場所をクリックしたら閉じる(親切設計)
$(document).on('mousedown', function(e) {
if (!$(e.target).closest('#google-search-bar, #goo-launcher').length) {
$gooBar.fadeOut(200);
}
});
})();