MediaWiki:Common.js
提供:Socialakiba Wiki
注意: 保存後、変更を確認するにはブラウザーのキャッシュを消去する必要がある場合があります。
- Firefox / Safari: Shift を押しながら 再読み込み をクリックするか、Ctrl-F5 または Ctrl-R を押してください (Mac では ⌘-R)
- Google Chrome: Ctrl-Shift-R を押してください (Mac では ⌘-Shift-R)
- Internet Explorer / Microsoft Edge: Ctrl を押しながら 最新の情報に更新 をクリックするか、Ctrl-F5 を押してください
- Opera: Ctrl-F5を押してください
/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */
/**
* AdSenseコードを動的に追加する 指定要素の直下に挿入する
* @param {string} adClient - パブリッシャーID (例: ca-pub-1234567890123456)
* @param {string} adSlot - 広告ユニットID (例: 1234567890)
* @param {string} targetId - 広告を挿入する要素のID
*/
function injectAdSense(adClient, adSlot, targetId) {
let adContainer = document.createElement('ins');
adContainer.className = 'adsbygoogle';
adContainer.style.display = 'block';
adContainer.style.textAlign = 'center';
adContainer.setAttribute('data-ad-client', adClient);
adContainer.setAttribute('data-ad-slot', adSlot);
adContainer.setAttribute('data-ad-format', 'auto');
let targetElement = document.getElementById(targetId);
if (targetElement) {
if (targetElement.firstElementChild) {
targetElement.insertBefore(adContainer, targetElement.firstElementChild);
} else {
targetElement.appendChild(adContainer);
}
if (window.adsbygoogle) {
(adsbygoogle = window.adsbygoogle || []).push({});
}
} else {
console.error('Target element not found: ' + targetId);
}
}
// AdSenseスクリプトをロードして広告を挿入
mw.loader.getScript('//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js')
.done(function() {
// AdSenseスクリプトのロード完了
// ここで挿入したい広告の情報を指定
const pageName = mw.config.get('wgPageName');
const namespaceNumber = mw.config.get('wgNamespaceNumber');
if (namespaceNumber === 0) {
if (!['メインページ'].includes(pageName)) {
injectAdSense('ca-pub-3395902935101030', '9961322902', 'bodyContent');
}
}
})
.fail(function() {
// AdSenseスクリプトのロード失敗
console.error('Failed to load AdSense script');
});
