MediaWiki:Common.js
注意: 保存後、変更を確認するにはブラウザーのキャッシュを消去する必要がある場合があります。
- 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は、すべてのページ読み込みですべての利用者に対して読み込まれます */ function my_switch(){ var uri = new URL(window.location.href); var is_switched = uri.searchParams.get("nebula_switch") != null; if (is_switched){ console.log("nebula_switch: 入れ替え後のページです"); if (is_view_mode(uri)){ console.log("nebula_switch: 閲覧モードです"); var parser = new Switch_parser(); if (parser.parse(document.getElementById("nebula_switch"))){ if (parser.tag_exists){ console.log( "nebula_switch: 入れ替えタグの解析に成功しました" ); nebula_run(parser.doc); }else{ console.log("nebula_switch: 入れ替えタグがありません"); } }else{ console.log("nebula_switch: 入れ替えタグが異常です"); nebula_write_error(parser.failed_message); } }else console.log( "nebula_switch: 閲覧モードでないため、入れ替えは行いません" ); } } function is_view_mode(uri){ var view_tab = document.getElementById("ca-view"); if (view_tab) var is_view_mode_selected = view_tab.classList.contains("selected"); else var is_view_mode_selected = false; var is_not_ve_edit_mode = uri.searchParams.get("veaction") == null; return is_view_mode_selected && is_not_ve_edit_mode; } // ES5 なので class 構文は使えない・・・ function Switch_parser(){ if (this === null) throw new TypeError(); } Switch_parser.prototype = { constructor: Switch_parser, parse: function(switch_tag){ // パース成功の真偽値を返却する if (switch_tag == null){ this.tag_exists = false; return true; } this.tag_exists = true; if (!window.DOMParser){ this.failed_message = "DOMParser が有効ではありません。" + "お使いのブラウザで DOMParser を有効にしてください。"; return false; } var dom_parser = new DOMParser(); var doc = dom_parser.parseFromString( "<root>" + switch_tag.innerText + "</root>", "text/xml" ); var parser_error = doc.querySelector("parsererror"); if (parser_error){ this.failed_message = "入れ替えタグに構文エラーがありました\n" + parser_error.innerHTML; return false; } this.doc = doc; return true; } }; function nebula_write_error(message){ var content_node = document.getElementsByClassName("mw-parser-output")[0]; content_node.innerHTML = "<h1>エラー: 男女入れ替えスイッチでエラー</h1>" + message throw new Error("nebula_switch: " + message); } function nebula_run(doc){ var content_node = document.getElementsByClassName("mw-parser-output")[0]; Array.from(doc.children[0].children).forEach(function(tag){ if (tag.tagName.toLowerCase() == "replace"){ var is_syntax_error = !( tag.childNodes[0].nodeName == "#text" && tag.childNodes[1].nodeName.toLowerCase() == "to" && tag.childNodes[2].nodeName == "#text" && tag.childNodes.length == 3 ); if (is_syntax_error){ nebula_write_error( "次のreplace タグが構文エラーです。<br>" + "<code>" + code_escape(tag.outerHTML) + "</code>" ); } var before = code_unescape(tag.childNodes[0].nodeValue); var after = code_unescape(tag.childNodes[2].nodeValue); content_node.innerHTML = content_node.innerHTML.replaceAll(before, after); } if (tag.tagName.toLowerCase() == "swap"){ var is_syntax_error = !( tag.childNodes[0].nodeName == "#text" && tag.childNodes[1].nodeName.toLowerCase() == "for" && tag.childNodes[2].nodeName == "#text" && tag.childNodes.length == 3 ); if (is_syntax_error){ nebula_write_error( "次のreplace タグが構文エラーです。<br>" + "<code>" + code_escape(tag.outerHTML) + "</code>" ); } var A = code_unescape(tag.childNodes[0].nodeValue); var B = code_unescape(tag.childNodes[2].nodeValue); content_node.innerHTML = swap_string(content_node.innerHTML, A, B); } }); document.getElementById("firstHeading").innerText += "(男女入れ替えて表示)"; content_node.innerHTML += `<table class="plainlinks ombox ombox-notice" role="presentation"><tbody><tr><td class="mbox-image"><span typeof="mw:File"><a href="/wiki/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Crystal_Clear_action_run.svg" class="mw-file-description"><img src="//upload.wikimedia.org/wikipedia/commons/thumb/3/35/Crystal_Clear_action_run.svg/50px-Crystal_Clear_action_run.svg.png" decoding="async" width="50" height="45" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/35/Crystal_Clear_action_run.svg/75px-Crystal_Clear_action_run.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/35/Crystal_Clear_action_run.svg/100px-Crystal_Clear_action_run.svg.png 2x" data-file-width="140" data-file-height="127"></a></span></td><td class="mbox-text"><b>このアカウントは [[:ja:User:{{{1}}}|{{{1}}}]] ([[:ja:User talk:{{{1}}}|会話]]) によって管理されている <a href="/wiki/Wikipedia:Bot" title="Wikipedia:Bot">Bot</a> です。</b> <p>手作業で処理するのが煩わしい大量の編集を自動化するために導入されています。<a href="/wiki/Wikipedia:%E5%A4%9A%E9%87%8D%E3%82%A2%E3%82%AB%E3%82%A6%E3%83%B3%E3%83%88" title="Wikipedia:多重アカウント">ソックパペット</a>ではありませんので注意してください。<br> </p> <small><i>管理者へ: この Bot が誤作動を起こしていたら、この Bot を<a class="external text" href="https://ja.wikipedia.org/w/index.php?title=%E7%89%B9%E5%88%A5:Blockip&&wpTarget=Bot&wpExpiry=infinite&wpAnonOnly=0&wpAutoBlock=0&wpCreateAccount=0&wpReason=other&wpReason-other=%5B%5BWikipedia%3ABot%7CBot%5D%5D+%E3%81%AE%E8%AA%A4%E4%BD%9C%E5%8B%95">ブロックして</a>ください。</i></small></td></tr></tbody></table> `; } function code_escape(code){ var textarea = document.createElement("textarea"); textarea.innerText = code; return textarea.innerHTML; } function code_unescape(code){ var textarea = document.createElement("textarea"); textarea.innerHTML = code; return textarea.innerText; } function swap_string(input_string, x, y) { // xとyを正規表現の特殊文字からエスケープする x = x.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); y = y.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // xとyを入れ替える var regex = new RegExp('(' + x + '|' + y + ')', 'g'); var result = input_string.replace(regex, function(match) { return match === x ? y : x; }); return result; } my_switch();