|
|
| 1行目: |
1行目: |
| /* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */ | | /* ここにあるすべての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');
| |
| });
| |