「MediaWiki:Common.js」の版間の差分

提供:WikiNebula
ナビゲーションに移動 検索に移動
編集の要約なし
編集の要約なし
100行目: 100行目:
           nebula_write_error(
           nebula_write_error(
             "次のreplace タグが構文エラーです。<br>"
             "次のreplace タグが構文エラーです。<br>"
             + "<pre>" + tag.outerHTML + "</pre>"
             + "<code>" + code_escape(tag.outerHTML) + "</code>"
           );
           );
         }  
         }  
106行目: 106行目:
     });
     });
   }
   }
    function code_escape(code){
      var textarea = document.createElement("textarea");
      textarea.innerText = code;
      return textarea.innerHTML;
    }


my_switch();
my_switch();

2024年5月2日 (木) 11:17時点における版

/* ここにあるすべての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
  }

  function nebula_run(doc){
    Array.from(doc.children[0].children).forEach(function(tag){
      if (tag.tagName.toLowerCase() == "replace"){
        if (!(
             tag.childNodes[0].nodeName == "#text"
          && tag.childNodes[1].nodeName.toLowerCase() == "to"
          && tag.childNodes[2].nodeName == "#text"
        )){
          nebula_write_error(
            "次のreplace タグが構文エラーです。<br>"
            + "<code>" + code_escape(tag.outerHTML) + "</code>"
          );
        } 
      }
    });
  }
    function code_escape(code){
      var textarea = document.createElement("textarea");
      textarea.innerText = code;
      return textarea.innerHTML;
    }

my_switch();