ผู้ใช้:Passawuth/monobook.js

จาก วิกิพจนานุกรม พจนานุกรมเสรี

หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง

  • ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
  • กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
  • อินเทอร์เน็ตเอกซ์พลอเรอร์ และ Edge: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
  • โอเปร่า: กด Ctrl-F5
// ========================================================
// HotCats
// Main Load Function
//
// ใช้สำหรับ เติม/ลบ/แก้ไข หมวดหมู่อย่างรวดเร็ว
// รุ่นดั้งเดิมสำหรับคอมมอนส์ที่ http://commons.wikimedia.org/wiki/MediaWiki:HotCat.js โดย Magnus Manske
// รุ่นดัดแปลงสำหรับวิกิพีเดียที่ http://fr.wikipedia.org/wiki/MediaWiki:JSScripts/HotCats โดย Zelda
// ขอขอบคุณ [[ผู้ใช้:Passawuth]] ที่ได้ช่วยแก้ไขสคริปต์ส่วนนี้
// Originally ported and modified by User:Passawuth
//
// User:Jutiphan Created: 06-10-2007
// Partial Update Sync with MediaWiki:Gadget-HotCat.js 29-02-2008
// Extensive modified and partial update sync with User:TheDJ/Gadget-HotCat.js 20-03-2008
// Replaced: none
// ========================================================
/* <pre><nowiki> */

$(hotcat);

var hotcat_running = 0 ;
var hotcat_last_v = "" ;
var hotcat_exists_yes = "http://upload.wikimedia.org/wikipedia/commons/thumb/b/be/P_yes.svg/20px-P_yes.svg.png" ;
var hotcat_exists_no = "http://upload.wikimedia.org/wikipedia/commons/thumb/4/42/P_no.svg/20px-P_no.svg.png" ;
var hotcat_upload = 0 ;
var hotcat_no_autocommit = 0;
var hotcat_old_onsubmit = null;
var hotcat_nosuggestions = false;
var hotcat_uncat_regex = /\{\{\s*ต้องการหมวดหมู่\s*\}\}/g;

function hotcat() {
  //Check if performing actions
  if (hotcat_check_action()) return;
  // Note that wgIsArticle is also set to true for category, talk, user, etc. pages: anything that
  // can be edited. It is false for diff pages, special pages, and ...
  if (!wgIsArticle) return;       // Diff pages...

  var visible_catlinks = document.getElementById ('mw-normal-catlinks') || getElementsByClassName ( document , "p" , "catlinks" ) [0];
  var hidden_catlinks = document.getElementById ('mw-hidden-catlinks');

  if ( visible_catlinks == null || typeof( visible_catlinks ) == 'undefined' ) {
    d3 = document.createElement ( "div" );
    d3.id = "mw-normal-catlinks";
    d3.innerHTML = '<a href="/wiki/Special:Categories" title="พิเศษ:Categories">หมวดหมู่</a>: ';
    visible_catlinks = d3;

    if ( hidden_catlinks ) {
      // There are hidden categories.
      hidden_catlinks.parentNode.insertBefore( d3, hidden_catlinks );
      hidden_catlinks.parentNode.className = "catlinks";
    } else {
      // This page has no categories at all, lets create a section where we can add them.
      var footer = getElementsByClassName ( document , "div" , "printfooter" ) [0];
      if( !footer ) return; // We have no idea where we should add this.

      d1 = document.createElement ( "div" );
      d1.id = "catlinks";
      d1.className = "catlinks";
      d1.appendChild ( d3 );
      footer.parentNode.insertBefore( d1, footer.nextSibling );
    }
  }

  hotcat_modify_existing(visible_catlinks);
  hotcat_append_add_span(visible_catlinks);
}

function hotcat_append_add_span(catline) {
  var span_add = document.createElement("span") ;
  var span_sep = document.createTextNode(" | ") ;
  if (catline.firstChild) catline.appendChild(span_sep);
  catline.appendChild(span_add);
  hotcat_create_span(span_add);
}

String.prototype.ucFirst = function () {
  return this.substr(0, 1).toUpperCase() + this.substr(1, this.length);
};

function hotcat_modify_span(span, i) {
  //var cat_title = span.firstChild.getAttribute ( "title" ) ;
  // This fails with MW 1.13alpha if the category is a redlink, because MW 1.13alpha appends
  // [[MediaWiki:Red-link-title]] to the category name... we another way to get that category
  // name. Lupo, 2008-02-27
  var cat_title = span.firstChild.innerHTML; // without "Category:"
  var sep1 = document.createTextNode(" ") ;
  var a1 = document.createTextNode("(-)") ;
  var remove_link = document.createElement("a") ;
  // Set the href to a dummy value to make sure we don't move if somehow the onclick handler
  // is bypassed.
  remove_link.href = "#catlinks";
  remove_link.title = cat_title;
  remove_link.onclick = hotcat_remove;
  remove_link.appendChild(a1);
  span.appendChild(sep1);
  span.appendChild(remove_link);

  var mod_id = "hotcat_modify_" + i ;
  var sep2 = document.createTextNode(" ") ;
  var a2 = document.createTextNode("(±)") ;
  var modify_link = document.createElement("a") ;
  modify_link.id = mod_id;
  modify_link.href = "javascript:hotcat_modify(\"" + mod_id + "\");";
  modify_link.appendChild(a2);
  span.appendChild(sep2);
  span.appendChild(modify_link);
}

function hotcat_modify_existing(catline) {
  var spans = catline.getElementsByTagName("span") ;
  for (var i = 0; i < spans.length; i++) {
    hotcat_modify_span(spans[i], i);
  }
}

function hotcat_getEvt(evt) {
  return evt || window.event || window.Event; // Gecko, IE, Netscape
}

function hotcat_evt2node(evt) {
  var node = null;
  try {
    var e = hotcat_getEvt(evt);
    node = e.target;
    if (!node) node = e.srcElement;
  } catch (ex) {
    node = null;
  }
  return node;
}

function hotcat_evtkeys(evt) {
  var code = 0;
  try {
    var e = hotcat_getEvt(evt);
    if (typeof(e.ctrlKey) != 'undefined') { // All modern browsers
      if (e.ctrlKey)  code |= 1;
      if (e.shiftKey) code |= 2;
    } else if (typeof (e.modifiers) != 'undefined') { // Netscape...
      if (e.modifiers & Event.CONTROL_MASK) code |= 1;
      if (e.modifiers & Event.SHIFT_MASK)   code |= 2;
    }
  } catch (ex) {
  }
  return code;
}

function hotcat_killEvt(evt)
{
  try {
    var e = hotcat_getEvt(evt);
    if (typeof (e.preventDefault) != 'undefined') e.preventDefault();
    if (typeof (e.stopPropagation) != 'undefined') e.stopPropagation();
  } catch (ex) {
  }
}

function hotcat_remove(evt) {
  var node = hotcat_evt2node(evt);
  if (!node) return false;
  // Get the category name from the original link to the category, which is at
  // node.parentNode.firstChild (the DOM structure here is
  // <span><a...>Category</a> <a...>(-)</a>...</span>).
  var cat_title = node.parentNode.firstChild.innerHTML; //It will be encoded later on, see below
  var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
  if (hotcat_evtkeys(evt) & 1) // CTRL pressed?
    editlk = editlk + '&hotcat_nocommit=1';
  hotcat_killEvt(evt);

  if (window.confirm("คุณแน่ใจว่าต้องการลบ หมวดหมู่:" + cat_title + " ออกจากหน้านี้?")) {
    document.location = editlk + '&hotcat_removecat=' + encodeURIComponent(cat_title);
  }
}

function hotcatGetParamValue(paramName, h) {
  if (typeof h == 'undefined') {
    h = document.location.href;
  }
  var cmdRe = RegExp('[&?]' + paramName + '=([^&]*)');
  var m = cmdRe.exec(h);
  if (m) {
    try {
      return decodeURIComponent(m[1]);
    } catch (someError) {
    }
  }
  return null;
}

// New. Code by Lupo & Superm401, added by Lupo, 2008-02-27
function hotcat_find_category(wikitext, category)
{
  var cat_name = category.replace(/([\\\^\$\.\?\*\+\(\)])/g, "\\$1");
  var initial = cat_name.substr(0, 1);
  var cat_regex = new RegExp("\\[\\[\\s*หมวดหมู่\\s*:\\s*"
      + (initial == "\\"
      ? initial
      : "[" + initial.toUpperCase() + initial.toLowerCase() + "]")
      + cat_name.substring(1).replace(/[ _]/g, "[ _]")
      + "\\s*(\\|.*?)?\\]\\]", "g"
      );
  var result = new Array();
  var curr_match = null;
  while ((curr_match = cat_regex.exec(wikitext)) != null) {
    result [result.length] = {match : curr_match};
  }
  return result; // An array containing all matches, with positions, in result[i].match
}

// New. Code by TheDJ, 2008-03-12
function hotcat_find_ins(wikitext)
{
  var re = /\[\[(?:หมวดหมู่):[^\]]+\]\]/ig;
  var index = -1;
  while (re.exec(wikitext) != null) index = re.lastIndex;

  if (index > -1) return index;
  //we should try to find interwiki links here, but that's for later.

  return -1;
}

function hotcat_check_action() {
  if (wgAction != "edit") return ret; // Not an edit page, so not our business...

  var ret = 0 ;
  var summary = new Array() ;
  var t = document.editform.wpTextbox1.value ;
  var prevent_autocommit = 0;
  if ((typeof hotcat_no_autocommit != "undefined" && hotcat_no_autocommit)
      || hotcatGetParamValue('hotcat_nocommit') == '1') {
    prevent_autocommit = 1;
  }
  
  var cat_rm = hotcatGetParamValue('hotcat_removecat');
  var cat_add = hotcatGetParamValue('hotcat_newcat');
  var cat_key = null;
  if (cat_rm != null && cat_rm.length > 0) {
    var matches = hotcat_find_category(t, cat_rm);
    if (!matches || matches.length == 0) {
      alert('ไม่พบเจอหมวดหมู่ "' + cat_rm + '" ในหน้านี้ ซึ่งเป็นไปได้ที่อาจจะอยู่ในแม่แบบที่ใช้ในหน้านี้');
      prevent_autocommit = 1;
    } else if (matches.length > 1) {
      alert('พบเจอหมวดหมู่ "' + cat_rm
          + "\" หลายครั้งในหน้านี้ ซึ่งไม่แน่ใจว่าควรจะลบอันไหน หยุดการดำเนินงาน");
      prevent_autocommit = 1;
    } else {
      //Jutiphan 20-03-2008: Branched code. Not sync to provide better modify func.
      if (cat_add != null && cat_add.length > 0) {
        var hcre = new RegExp("(\\s*)\\[\\[ *หมวดหมู่ *: *" + cat_rm.replace(/([\\\^\$\*\+\?\.\|\{\}\[\]\(\)])/g, "\\$1") + " *(\\|[^\\]]*)?\\]\\]", "gi");
        t = t.replace(hcre, "$1[[หมวดหมู่:" + cat_add + "$2]]");
        summary.push("เปลี่ยน หมวดหมู่:" + cat_rm + " → [[หมวดหมู่:" + cat_add + "]]");

      } else {
        if (cat_add != null && cat_add.length > 0 && matches[0].match.length > 1)
          cat_key = matches[0].match[1]; // Remember the category key, if any.
        var t1 = t.substring(0, matches[0].match.index);
        var t2 = t.substring(matches[0].match.index + matches[0].match[0].length);
        // Remove whitespace (properly): strip whitespace, but only up to the next line feed.
        // If we then have two linefeeds in a row, remove one. Otherwise, if we have two non-
        // whitespace characters, insert a blank.
        var i = t1.length - 1;
        while (i >= 0 && t1.charAt(i) != '\n' && t1.substr(i, 1).search(/\s/) >= 0) i--;
        var j = 0;
        while (j < t2.length && t2.charAt(j) != '\n' && t1.substr(j, 1).search(/\s/) >= 0) j++;
        if (i >= 0 && t1.charAt(i) == '\n' && j < t2.length && t2.charAt(j) == '\n')
          i--;
        if (i >= 0) t1 = t1.substring(0, i + 1); else t1 = "";
        if (j < t2.length) t2 = t2.substring(j); else t2 = "";
        if (t1.length > 0 && t1.substring(t1.length - 1).search(/\S/) >= 0
            && t2.length > 0 && t2.substr(0, 1).search(/\S/) >= 0)
          t1 = t1 + ' ';
        t = t1 + t2;
        summary.push("ลบ หมวดหมู่:" + cat_rm + " ออก");
      }
      ret = 1;
    }
  }

  //Jutiphan 20-03-2008: Branched code. Added ret check to provide better modify func. handling
  if (ret != 1 && cat_add != null && cat_add.length > 0) {
    var matches = hotcat_find_category(t, cat_add);
    if (matches && matches.length > 0) {
      alert('หมวดหมู่:' + cat_add + ' นั้นมีอยู่แล้ว หยุดการดำเนินงาน');
      prevent_autocommit = 1;
    } else {
      var insertionpoint = hotcat_find_ins(t);
      var newcatstring = '\n\[\[หมวดหมู่:' + cat_add + (cat_key != null ? cat_key : "") + '\]\]';
      if (insertionpoint > -1) {
        t = t.substring(0, insertionpoint) + newcatstring + t.substring(insertionpoint);
      } else {
        t = t + newcatstring;
      }
      summary.push("เพิ่ม[[หมวดหมู่:" + cat_add + "]]");
      var t2 = t.replace(hotcat_uncat_regex, ""); // Remove "uncat" templates
      if (t2.length != t.length) {
        t = t2;
        summary.push("ลบป้าย {{ต้องการหมวดหมู่}} ออก");
      }
      ret = 1;
    }
  }
  if (ret) {
    document.editform.wpTextbox1.value = t;
    document.editform.wpSummary.value = summary.join("; ") + " ด้วย[[WP:iScript|สคริปต์จัดให้]])";
    document.editform.wpMinoredit.checked = true;
    if (!prevent_autocommit) {
      // Hide the entire edit section so as not to tempt the user into editing...
      var bodyContentId = document.getElementById("bodyContent") || document.getElementById("mw_contentholder");
      bodyContentId.style.display = "none";
      document.editform.submit();
    }
  }
  return ret;
}

function hotcat_clear_span(span_add) {
  while (span_add.firstChild) span_add.removeChild(span_add.firstChild);
}

function hotcat_create_span(span_add) {
  hotcat_clear_span(span_add);
  var a_add = document.createElement("a") ;
  var a_text = document.createTextNode("(+)") ;
  span_add.id = "hotcat_add";
  a_add.href = "javascript:hotcat_add_new()";
  a_add.appendChild(a_text);
  span_add.appendChild(a_add);
}

function hotcat_modify(link_id) {
  var link = document.getElementById(link_id) ;
  var span = link.parentNode ;
  var catname = span.firstChild.innerHTML; // was .firstChild.data. Just for consistency

  while (span.firstChild.nextSibling) span.removeChild(span.firstChild.nextSibling);
  span.firstChild.style.display = "none";
  hotcat_create_new_span(span, catname);
  hotcat_last_v = "";
  hotcat_text_changed(); // Update icon
}

function hotcat_add_new() {
  var span_add = document.getElementById("hotcat_add") ;
  hotcat_clear_span(span_add);
  hotcat_last_v = "";
  hotcat_create_new_span(span_add, "");
}

function hotcat_create_new_span(thespan, init_text) {
  var form = document.createElement("form") ;
  form.method = "post";
  form.onsubmit = function () {
    hotcat_ok();
    return false;
  };
  form.id = "hotcat_form";
  form.style.display = "inline";

  var list = document.createElement("select") ;
  list.id = "hotcat_list";
  list.onclick = function () {
    document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value;
    hotcat_text_changed();
  };
  list.ondblclick = function () {
    document.getElementById("hotcat_text").value = document.getElementById("hotcat_list").value;
    hotcat_text_changed();
    hotcat_ok();
  };
  list.style.display = "none";

  var text = document.createElement("input") ;
  text.size = 40;
  text.id = "hotcat_text";
  text.type = "text";
  text.value = init_text;
  text.onkeyup = function () {
    window.setTimeout("hotcat_text_changed();", 100);
  };

  var exists = document.createElement("img") ;
  exists.id = "hotcat_exists";
  exists.src = hotcat_exists_no;

  var OK = document.createElement("input") ;
  OK.type = "button";
  OK.value = "ตกลง";
  OK.onclick = function (evt) {
    hotcat_ok(hotcat_evtkeys(evt) & 1);
  };

  var cancel = document.createElement("input") ;
  cancel.type = "button";
  cancel.value = "ยกเลิก";
  cancel.onclick = hotcat_cancel;

  form.appendChild(list);
  form.appendChild(text);
  form.appendChild(exists);
  form.appendChild(OK);
  form.appendChild(cancel);
  thespan.appendChild(form);
  text.focus();
}

function hotcat_ok(nocommit) {
  var text = document.getElementById("hotcat_text") ;
  var v = text.value ;

  // Empty category ?
  if (v == "") {
    hotcat_cancel();
    return;
  }

  var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
  var url = editlk + '&hotcat_newcat=' + encodeURIComponent(v) ;

  // Editing existing?
  var span = text.parentNode.parentNode ; // span.form.text
  if (span.id != "hotcat_add") { // Not plain "addition"
    url += '&hotcat_removecat=' + span.firstChild.innerHTML;
  }
  if (nocommit) url = url + '&hotcat_nocommit=1';
  document.location = url;
}

function hotcat_cancel() {
  var span = document.getElementById("hotcat_form").parentNode ;
  if (span.id == "hotcat_add") {
    hotcat_create_span(span);
  } else {
    while (span.firstChild.nextSibling) span.removeChild(span.firstChild.nextSibling);
    span.firstChild.style.display = "";
    for (var i = 0; i < span.parentNode.childNodes.length; i++) {
      if (span.parentNode.childNodes[i] != span) continue;
      hotcat_modify_span(span, i);
      break;
    }
  }
}

function hotcat_text_changed() {
  if (hotcat_running) return;
  var text = document.getElementById("hotcat_text") ;
  var v = text.value.ucFirst() ;
  if (hotcat_last_v == v) return;

  hotcat_running = 1;
  hotcat_last_v = v;

  if (v != "") {
    var url = wgServer + "/" + wgScriptPath + "/api.php?format=xml&action=query&list=allpages&apnamespace=14&apfrom=" + encodeURIComponent(v) ;
    if (typeof ( hotcat_xmlhttp ) != "undefined") hotcat_xmlhttp.abort(); // Just to make sure...
    hotcat_xmlhttp = new sajax_init_object();
    hotcat_xmlhttp.open('GET', url, true);
    hotcat_xmlhttp.onreadystatechange = function () {
      if (typeof hotcat_xmlhttp == "undefined") return;
      if (hotcat_xmlhttp.readyState == 4) {
        var xml = hotcat_xmlhttp.responseXML ;
        if (xml == null) return;
        var pages = xml.getElementsByTagName("p") ;
        var titles = new Array() ;
        for (var i = 0; i < pages.length; i++) {
          var s = pages[i].getAttribute("title");
          s = s.substr(s.indexOf(":") + 1);
          if (s.substr(0, hotcat_last_v.length) != hotcat_last_v) break;
          titles.push(s);
        }
        hotcat_show_suggestions(titles);
      }
    };
    hotcat_xmlhttp.send(null);
  } else {
    var titles = new Array() ;
    hotcat_show_suggestions(titles);
  }
  hotcat_running = 0;
}

function hotcat_show_suggestions(titles) {
  var text = document.getElementById("hotcat_text") ;
  var list = document.getElementById("hotcat_list") ;
  var icon = document.getElementById("hotcat_exists") ;
  if (titles.length == 0) {
    list.style.display = "none";
    icon.src = hotcat_exists_no;
    return;
  }

  var listh = titles.length * 20 ;
  var nl = parseInt(text.parentNode.offsetLeft) - 1 ;
  var nt = parseInt(text.offsetTop) - listh ;
  if (typeof hotcat_list_down != "undefined" && hotcat_list_down) {
    nt = text.offsetTop + text.offsetHeight;
  }
  list.size = 5;
  list.style.align = "left";
  list.style.zIndex = 5;
  list.style.position = "absolute";
  list.style.top = nt + "px";
  list.style.width = text.offsetWidth + "px";
  list.style.height = listh + "px";
  list.style.left = nl + "px";
  while (list.firstChild) list.removeChild(list.firstChild);
  for (var i = 0; i < titles.length; i++) {
    var opt = document.createElement("option") ;
    var ot = document.createTextNode(titles[i]) ;
    opt.appendChild(ot);
    opt.setAttribute("value", titles[i]);
    list.appendChild(opt);
  }

  list.style.display = "block";

  icon.src = hotcat_exists_yes;

  var first_title = titles.shift() ;
  if (first_title == hotcat_last_v) return;

  var nosel = hotcat_last_v.length ;

  text.value = first_title;
  if (is_khtml)
    text.setSelectionRange(nosel, first_title.length);
  else
  {
    text.selectionStart = nosel;
    text.selectionEnd = first_title.length;
  }
}

/* </nowiki></pre> */