วิกิพีเดีย:สคริปต์ผู้ใช้

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

สคริปต์ผู้ใช้ (Monobook.js) เป็นโค้ดภาษาจาวาสคริปต์ ที่ใช้ในการตกแต่ง เปลี่ยนแปลง หรือเพิ่มเติมความสามารถ รูปแบบวิกิพีเดียสำหรับผู้ใช้ดังกล่าว. ซึ่งความสามารถส่วนนี้สำหรับผู้ที่เป็นสมาชิกวิกิพีเดียเท่านั้น.

สารบัญ

[แก้] การใส่สคริปต์ผู้ใช้

โดยให้ใส่โค้ดจาวาสคริปต์ที่ต้องการที่หน้า:

ผู้ใช้:ชื่อผู้ใช้/monobook.js

โดยเปลี่ยนคำว่า "ชื่อผู้ใช้" เป็นชื่อผู้ใช้ของคุณเอง


[แก้] ตัวอย่างความสามารถเพิ่มเมนูด้านบน

ตัวอย่างความสามารถที่นิยมคือ การเพิ่มเมนูด้านบน เหมือนดังรูปแสดง (หรืออาจจะเพิ่มด้านซ้ายมือ) วิธีใช้โดยการนำฟังก์ชันของจาวาสคริปต์ ไปใส่ไว้ในหน้าผู้ใช้

ตัวอย่างการใช้งานสคริปต์ผู้ใช้
ตัวอย่างการใช้งานสคริปต์ผู้ใช้


[แก้] ตัวอย่างสคริปต์ จากผู้ใช้งานคนอื่น

[แก้] สคริปต์ ที่นำไปใช้ได้

สคริปต์จะแบ่งเป็น 2 แบบ คือ สคริปต์เครื่องมือ กับสคริปต์เฉพาะทาง โดยการใช้งานของสคริปต์เฉพาะทาง จำเป็น ต้องใช้ สคริปต์ เครื่องมือตัวใดตัวหนึ่ง ในการช่วยแสดงผล โดยวิธีการนำไปใช้ สามารถเลือกสคริปต์ที่ต้ัองการไปใส่ไว้ในหน้าสคริปต์ ของตัวเองตามความเหมาะสม สำหรับสคริปต์ เครื่องมือ สามารถใส่ครั้งเดียว และสามารถเรียกใช้ได้มากกว่าหนึ่งครั้ง

[แก้] สคริปต์เครื่องมือ

[แก้] เพิ่มแท็บด้านบน

/* */ function addTab(url, name, id, title, key){ var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0]; addlilink(tabs, url, name, id, title, key); } /* */


[แก้] เพิ่มเมนู เครื่องมือ ด้านซ้าย

/*  */
function addlilink(tabs, url, name, id, title, key){
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    na.accesskey = key;
    var pref = 'alt-';
    if(((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1)) || navigator.userAgent.toLowerCase().indexOf( 'mac' ) != -1 ) pref = 'control-';
    if(clientPC.indexOf('opera')!=-1) pref = 'shift-esc-';
    if(key && title) na.title = title + ' [' + pref + key + ']';
    else if(title) na.title = title;
    else if(key) na.title = '[' + pref + key + ']';
    return li;
}
/*  */

[แก้] อ่านชื่อของหน้านั้น

/*  */
function getPname() {
  return wgPageName.replace(/_/g, ' ');
}
/*  */

[แก้] สคริปต์เฉพาะทาง

[แก้] ใส่ข้อความต้องการความช่วยเหลือ

ใส่ข้อความต้องการความช่วยเหลือ ที่ส่วนบนของบทความ และทำการเซฟบทความทันที

/*  */
// ========== Quick Wikify ==========
function doQwikify() {
  document.editform.wpTextbox1.value = "{{ช่วยดูหน่อย}}\n\n" + document.editform.wpTextbox1.value;
  document.editform.wpSummary.value = "ช่วยดูหน่อย";
  document.editform.wpMinoredit.checked = true;
  document.editform.submit();
}
function addQwikify() {
  addTab("javascript:doQwikify()", "ช่วย", "ca-wikify", "แจ้งว่าช่วยดูหน่อย", "");
  akeytt();
}

if (document.title.indexOf("แก้ไข ") == 0) {
  if (window.addEventListener) window.addEventListener("load", addQwikify, false);
  else if (window.attachEvent) window.attachEvent("onload", addQwikify);
}
/*  */

[แก้] ใส่ข้อความแจ้งลบ

ใส่ข้อความแจ้งลบ ที่ส่วนบนของบทความ และทำการเซฟบทความทันที

/*  */
// ========== Quick Speedy delete ==========
function doQsd() {
  document.editform.wpTextbox1.value = "{{ลบ}}\n\n" + document.editform.wpTextbox1.value;
  document.editform.wpSummary.value = "Marked for speedy deletion.";
  document.editform.submit();
}
function addQsd() {
  addTab("javascript:doQsd()", "แจ้งลบ", "ca-sd", "คำสั่งแจ้งลบ", "");
  akeytt();
}

if (document.title.indexOf("แก้ไข ") == 0) {
  if (window.addEventListener) window.addEventListener("load", addQsd, false);
  else if (window.attachEvent) window.attachEvent("onload", addQsd);
}
/*  */

[แก้] แก้ย่อหน้าแรก (ย่อหน้าสรุป)

/*  */
// ========== Edit Section 0 ==========
function addEditSection0() {
  ta['ca-edit-0'] = ['', 'Edit the zeroth section of this page'];
  if (!document.getElementById) return;
  x = document.getElementById('ca-edit');
  if(!x) return;
  y = document.createElement('LI');
  y.id = 'ca-edit-0';
  if (x.className == 'selected') {
    if (/&action=edit&section=0$/.test(window.location.href)) {
      x.className = 'istalk';
      y.className = 'selected';
    } else {
      x.className = 'selected istalk';
    }
  } else if (x.className == 'selected istalk') {
    if (/&action=edit&section=0$/.test(window.location.href)) {
      x.className = 'istalk';
      y.className = 'selected istalk';
    } else {
      y.className = 'istalk';
    }
  } else {
    y.className = x.className;
    x.className = 'istalk';
  }
  z = document.createElement('A');
  if (x.children) {
    z.href = x.children[0].href + '&section=0';
    z.appendChild(document.createTextNode('0'));
    y.appendChild(z);
    document.getElementById('p-cactions').children[1].insertBefore(y,x.nextSibling);
  } else {
    z.href = x.childNodes[0].href + '&section=0';
    z.appendChild(document.createTextNode('0'));
    y.appendChild(z);
    document.getElementById('p-cactions').childNodes[3].insertBefore(y,x.nextSibling);
  }
}

if (document.title.indexOf("Editing ") == -1) {
  if (window.addEventListener) window.addEventListener("load", addEditSection0, false);
  else if (window.attachEvent) window.attachEvent("onload", addEditSection0);
}

/*  */

[แก้] ดูความแตกต่าง กับรุ่นที่บันทึกล่าสุด

/*  */
// ========== Add Last Diff ==========

function doLastDiff() {
  var nurl="http://th.wikipedia.org/w/index.php?title=" + getPname() + "&diff=cur&oldid=prev";
  document.location=nurl
}
function addLastDiff() {
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
    var l=addlilink(tabs, "javascript:doLastDiff()", 'ล่าสุด', '');
    l.lastChild.title="Show most recent diff";
}

if (window.addEventListener) window.addEventListener("load", addLastDiff, false);
else if (window.attachEvent) window.attachEvent("onload", addLastDiff);
/*  */

[แก้] สลับเปลี่ยน ตัวเลขอารบิกกับเลขไทย

/*  */
// ========== Replace string ==========
// Javascript from http://www.irt.org/script/242.htm
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

// ========== Thai to Arabic Numbers ==========
// This script converts all Thai numbers (๑ ๒ ๓) to Arabic numbers (1 2 3)
function doConvertNumber() {
  text = document.editform.wpTextbox1.value;
  text = replace(text,'๑','1');
  text = replace(text,'๒','2');
  text = replace(text,'๓','3');
  text = replace(text,'๔','4');
  text = replace(text,'๕','5');
  text = replace(text,'๖','6');
  text = replace(text,'๗','7');
  text = replace(text,'๘','8');
  text = replace(text,'๙','9');
  text = replace(text,'๐','0');
  document.editform.wpTextbox1.value = text;
  document.editform.wpSummary.value = "เปลี่ยนเลขไทยเป็นอารบิก";
}
function addConvertNumber() {
  addTab("javascript:doConvertNumber()", "๒ ->2", "ca-tanum", "เปลี่ยนเลขไทยเป็นอารบิก", "");
  akeytt();
}

if (document.title.indexOf("แก้ไข ") == 0) {
  if (window.addEventListener) window.addEventListener("load", addConvertNumber, false);
  else if (window.attachEvent) window.attachEvent("onload", addConvertNumber);
}

// ========== Arabic to Thai Numbers ==========
// This script converts all Arabic numbers (1 2 3) to Thai numbers (๑ ๒ ๓)
function doConvertToThaiNumber() {
  text = document.editform.wpTextbox1.value;
  text = replace(text,'1','๑');
  text = replace(text,'2','๒');
  text = replace(text,'3','๓');
  text = replace(text,'4','๔');
  text = replace(text,'5','๕');
  text = replace(text,'6','๖');
  text = replace(text,'7','๗');
  text = replace(text,'8','๘');
  text = replace(text,'9','๙');
  text = replace(text,'0','๐');
  document.editform.wpTextbox1.value = text;
  document.editform.wpSummary.value = "เปลี่ยนเลขอารบิกเป็นไทย";
}
function addConvertToThaiNumber() {
  addTab("javascript:doConvertToThaiNumber()", "2 ->๒", "ca-atnum", "เปลี่ยนเลขอารบิกเป็นไทย", "");
  akeytt();
}

if (document.title.indexOf("แก้ไข ") == 0) {
  if (window.addEventListener) window.addEventListener("load", addConvertToThaiNumber, false);
  else if (window.attachEvent) window.attachEvent("onload", addConvertToThaiNumber);
}
/*  */
ภาษาอื่น