참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다. 구글 크롬, 파이어폭스, 마이크로소프트 엣지, 사파리: ⇧ Shift 키를 누른 채 "새로 고침" 버튼을 클릭하십시오. 더 자세한 정보를 보려면 위키백과:캐시 무시하기 항목을 참고하십시오.

importScript("User:Klutzy/Api.js");

function google(v){ return "http://www.google.com/search?q="+v; }

// 미디어위키: 네임스페이스 문서에 베타위키로 가는 링크 추가하기
$(function() {
  if(wgNamespaceNumber==8) {
    var url = "http://translatewiki.net/wiki/MediaWiki:"+wgTitle+"/ko";
    var y = mw.util.addPortletLink('p-views', url, 'TW', 'ca-translatewiki', '번역위키 메시지 편집.', '0', null);
  }
});

$(function(){
  if(wgNamespaceNumber==-1 && (wgCanonicalSpecialPageName == "Contributions" || wgCanonicalSpecialPageName == "DeletedContributions")) {
    var u = document.getElementsByName("target")[0].value;
    mw.util.addPortletLink('p-views', google(u), 'GG', 'ca-google', 'Google it', '0', null);
  }
});

/*
if(wgNamespaceNumber==10 && wgAction=="delete" && wgArticleId!=0) {
  addOnloadHook(function(){
    var queryUrl = "http://ko.wikipedia.org/w/api.php?action=query&list=backlinks&bltitle="+wgPageName+"&bllimit=1&format=json";
    var x = sajax_init_object(); // XMLHttpRequest
    //var query = eval( 
  });
}
*/

/*
$(function(){
  var notice = document.getElementById("vector_post_release_message");
  var td = notice.getElementsById("table-cell-2")
  td.innerHTML = "
});
*/

// watchlist에서 /XXX가 붙은 문서를 읽어들여, watch되지 않은 문서를 자동으로 주시하는 기능
// ..이어야 하는데 지금은 그냥 현재 시간에 해당하는 문서를 무조건 watch해버리는 것만 구현되어있음
var updateWatchlist = function(){
  // http://ko.wikipedia.org/w/api.php?action=query&list=watchlistraw&wrlimit=500
  var Api = JsMwApi(); // importScript() seems like an asynchronous feature; do not declare Api outside of addOnloadHook
  Api({action: "query", list: "watchlistraw", wrlimit: "500"}, function(res) {
    if(res == null) return;
    // 위키백과:사랑방/Y년 제W주/XXX
    var items = res.watchlistraw;
    var templates = new Array();
    var MAGIC = "/XXX"; // XXX
    items.forEach(function(item, index, array) {
      var title = item.title;
      if(title.indexOf(MAGIC) == -1) return;
      title = title.replace(MAGIC, "");
      templates.push(title);
    });

    var newitems = new Array();
    templates.forEach(function(template, index, array) {
      var title = template;
      //var reg = RegExp(template.replace(/[YMW]/g, "\\d+"));
      //var captured = items.filter(function(item) { return reg.test(item.title); });
      var nums = new Array();
      nums.push(-1, 0, 1, 2, 3);
      var newTitles = nums.map(function(num) { 
        template =  template.replace(/(Y년 M월)/g, "{{#timel:Y년 M월|"+num+" months}}");
        template =  template.replace(/(Y년 제W주)/g, "{{#timel:Y년 제W주|"+num+" weeks}}");
        return template;
      });
      newTitles.forEach(function(newTitle, index_, array_) {
        Api({action: "parse",  text: newTitle}, function(res) {
          if(res == null) return;
          res = res.parse.text["*"];
          res = res.split(">",2)[1].split("<",2)[0];
          if(items.indexOf(res)<0) {
            newitems.push(res);
            Api({action:"watch", title: res}, function(res){});
          }
        }); // Api
      }); // newTitles.forEach
    }); // templates.forEach
  }); // Api. I HATE javascript
};

// watchlist auto-update. highly experimental. do not try this at home.
if(wgNamespaceNumber==-1 && wgCanonicalSpecialPageName == "Watchlist") {
  addOnloadHook(updateWatchlist);
}