本館粉絲專頁

使用者:Digipunk/mw-common.js:修訂版本之間的差異

分享此網頁到Facebook
分享此網頁到Plurk
分享此網頁到百度搜藏
分享此網頁到Twitter
分享此網頁到Del.icio.us
台灣棒球維基館
跳轉到: 導覽搜尋
第1行: 第1行:
 
// importScript() 為舊版寫法,新版需改用 mw.loader.load() 或直接在 LocalSettings.php 裡建立 importScript()
 
// importScript() 為舊版寫法,新版需改用 mw.loader.load() 或直接在 LocalSettings.php 裡建立 importScript()
importScript('User:Digipunk/FC/func.js');
+
importScript('User:Digipunk/FC/func.js'); // floating calendar
importScript('User:Digipunk/SC/func.js');
+
importScript('User:Digipunk/SC/func.js'); // timezone conversion, JS-RNK
  
 
// 處理新版 MW 在 </DIV><BR> 後面強行加入 <P><BR></P>
 
// 處理新版 MW 在 </DIV><BR> 後面強行加入 <P><BR></P>
第186行: 第186行:
 
     return encodeURI(str.substr(0, pos)) + '#' + encodeURIComponent(str.substr(pos + 1)).replace(/%/g, '.');
 
     return encodeURI(str.substr(0, pos)) + '#' + encodeURIComponent(str.substr(pos + 1)).replace(/%/g, '.');
 
   }
 
   }
 +
}}}
 +
 +
function dgpk_arrange_raw_object(obj) {{{
 +
  for (var j in obj) {
 +
    if (typeof obj[j] == 'object') {
 +
      obj[j] = dgpk_arrange_raw_object(obj[j]);
 +
    } else {
 +
      if (!isNaN(obj[j])) {
 +
        obj[j] = parseFloat(obj[j]);
 +
      } else if (typeof obj[j] == 'string') {
 +
        obj[j] = decodeURIComponent(obj[j].replace(/\+/g, ' ')).trim();
 +
      }
 +
    }
 +
  }
 +
  return obj;
 
}}}
 
}}}

2018年9月26日 (三) 22:10的修訂版本

// importScript() 為舊版寫法,新版需改用 mw.loader.load() 或直接在 LocalSettings.php 裡建立 importScript()
importScript('User:Digipunk/FC/func.js');  // floating calendar
importScript('User:Digipunk/SC/func.js');  // timezone conversion, JS-RNK
 
// 處理新版 MW 在 </DIV><BR> 後面強行加入 <P><BR></P>
if (mw.config.get('wgVersion').localeCompare('1.3') >= 0) {
  document.body.innerHTML = document.body.innerHTML.replace(/<\/div>([\r\n]*)<p><br[ \/]*>\s*<\/p>/ig, '</div>$1<br />');
}
 
window.addEventListener("load", function(event) {
  DGPK_ONLOAD_ACTIONS();
  DGPK_CHECK_PREVIEW();
}, false);
 
function DGPK_ONLOAD_ACTIONS() {{{
  try { DGPKJS_CSS_JS();   } catch (e) { console.log(e); }
 
  try { DGPKJS_FC();       } catch (e) { console.log(e); }
  try { DGPKJS_SC();       } catch (e) { console.log(e); }
 
  try { DGPKJS_STATS();    } catch (e) { console.log(e); }
  try { DGPKJS_MOVER();    } catch (e) { console.log(e); }  // TR.mover highlight
  try { DGPKJS_SORTALBE(); } catch (e) { console.log(e); }  // TABLE.sortable TD.fixed-ord sno
}}}
 
 
//--------------------------------------------------
// deal with the built-in AJAX preview
//--------------------------------------------------
function DGPK_CHECK_PREVIEW() {{{
  var WE = document.getElementsByClassName('wikiEditor-ui-tabs');
  var PV = document.getElementsByClassName('wikiEditor-ui-view-preview');
 
  if (WE.length > 0 && PV.length > 0) {
    var A = WE[0].getElementsByTagName('A');
 
    for (var i = 0, ii = A.length; i < ii; i++) {
      if (A[i].innerHTML == '預覽') {
        var PVC = PV[0].getElementsByClassName('wikiEditor-preview-contents');  // preview content
        if (PVC.length > 0) {
          A[i].addEventListener("mousedown", function(event) {
            setTimeout(function() {
              __dgpk_preview(PVC[0], 0);
            }, 200);
          }, false);
        }
        break;
      }
    }
  }
}}}
 
function __dgpk_preview(pvc, counter) {{{
  if (pvc.innerHTML == '' && counter < 50) {
    setTimeout(function() {
      __dgpk_preview(pvc, ++counter);
    }, 100);
  } else {
    DGPK_ONLOAD_ACTIONS();
  }
}}}
 
//--------------------------------------------------
// insert CSS rules to individual page
// and eval script snippet
//--------------------------------------------------
function DGPKJS_CSS_JS() {{{
  var CSS = document.getElementsByClassName('DGPK-css');
  var n = CSS.length;
  if (n > 0) {
    var style = (function() {
      var el = document.createElement('style');
      el.appendChild(document.createTextNode('')); // WebKit hack
      el.type  = 'text/css';
      el.rel   = 'stylesheet';
      el.media = 'screen';
      // el.id    = id;
      document.head.appendChild(el);
      return el.sheet;
    })();
 
    for (var i = n - 1; i >= 0; i--) {
      //var rules = CSS[i].innerHTML.replace(/<\/?[^>]+>/g, '').split("\n");
      var rules = CSS[i].innerText.split("}");
      for (var j = 0, jj = rules.length; j < jj; j++) {
        if (rules[j].trim() == '') continue;
        style.insertRule(rules[j] + '}');
      }
      CSS[i].parentNode.removeChild(CSS[i]);
    }
  }
 
  var JS = document.getElementsByClassName('DGPK-js');
  for (var zzi = JS.length - 1; zzi >= 0; zzi--) {
    (function () { eval(JS[zzi].innerText); }());  // 丟到函式裡執行,避免 eval 裡的變數影響這裡
    JS[zzi].parentNode.removeChild(JS[zzi]);
  }
}}}
 
 
function DGPKJS_STATS() {{{
  var CLS = ['TBL-DATA-B', 'TBL-DATA-P'];
  for (var i = 0; i < 2; i++) {
    var TBL = document.getElementsByClassName(CLS[i]);
    for (var j = 0, jj = TBL.length; j < jj; j++) {
      for (var k = 0, kk = TBL[j].tBodies.length; k < kk; k++) {
        for (var x = 0, xx = TBL[j].tBodies[k].children.length; x < xx; x++) {
          if (TBL[j].tBodies[k].children[x].tagName == 'TR' && ['mover-x', 'title', 'column', 'sortbottom'].indexOf(TBL[j].tBodies[k].children[x].className) < 0) {
            TBL[j].tBodies[k].children[x].className += ' mover';
            TBL[j].tBodies[k].children[x].innerHTML = TBL[j].tBodies[k].children[x].innerHTML.replace(/>(\s*)(\d+)\//g, function(m, a, b) {
              return '>' + a + '<S>' + ('00' + b).substr(-3) + '</S>' + b + '/';
            });
            if (TBL[j].tBodies[k].children[x].children.length > 0) {
              //TBL[j].tBodies[k].children[x].children[0].className += ' fixed-ord';
              // 乾脆這裡一次解決,fixed-ord 留給外部彈性使用
              TBL[j].tBodies[k].children[x].children[0].innerHTML = '<S>' + ('00' + x).substr(-3) + '</S>' + TBL[j].tBodies[k].children[x].children[0].innerHTML;
            }
          }
        }
      }
    }
  }
}}}
 
// 在 TABLE.sortable 裡,自動為 TD.fixed-ord 加上隱藏序號
function DGPKJS_SORTALBE() {{{
  var SORTABLE = document.getElementsByClassName('sortable');
  for (var i = 0, ii = SORTABLE.length; i < ii; i++) {
    var elmts = SORTABLE[i].getElementsByClassName('fixed-ord');
    for (var j = 0, jj = elmts.length; j < jj; j++) {
      elmts[j].innerHTML = '<SPAN STYLE="display:none">' + ('00' + j).substr(-3) + '</SPAN>' + elmts[j].innerHTML;
    }
  }
}}}
 
//------------------------------------
// mouseover highlight
// 針對 [TABLE|TR].[mover|mover-f]
// 加在 TABLE 就套用於所有 TR,除了 TR.mover-x
//------------------------------------
function DGPKJS_MOVER() {{{
  var CLS = ['mover', 'mover-f'];
  for (var i = 0; i < 2; i++) {
    var OBJS = document.getElementsByClassName(CLS[i]);
    for (var j = 0, jj = OBJS.length; j < jj; j++) {
      if (OBJS[j].tagName == 'TABLE') {
        for (var k = 0, kk = OBJS[j].tBodies.length; k < kk; k++) {
          for (var x = 0, xx = OBJS[j].tBodies[k].children.length; x < xx; x++) {
            var obj = OBJS[j].tBodies[k].children[x];
            if (obj.tagName == 'TR' && obj.className.indexOf('mover-x') /*mover|mover-f|mover-x*/ < 0) {
              __dgpk_mover(obj, i);
            }
          }
        }
      } else if (OBJS[j].tagName == 'TR') {
        __dgpk_mover(OBJS[j], i);
      }
    }
  }
}}}
 
function __dgpk_mover(tr, force) {{{
  tr.addEventListener("mouseover", function(event) {
    this.className += ' mev-over' + (force ? '-f' : '');
  }, false);
 
  tr.addEventListener("mouseout", function(event) {
    if (force) {
      this.className = this.className.replace(/ mev-over-f/g, '');
    } else {
      this.className = this.className.replace(/ mev-over/g, '');
    }
  }, false);
}}}
 
 
//--------------------------------------------------
// Utilities
//--------------------------------------------------
// normalize uri to conform to wiki format
function dgpk_wikilink(str) {{{
  var pos = str.indexOf('#');
  if (pos < 0) {
    return encodeURI(str);
  } else {
    return encodeURI(str.substr(0, pos)) + '#' + encodeURIComponent(str.substr(pos + 1)).replace(/%/g, '.');
  }
}}}
 
function dgpk_arrange_raw_object(obj) {{{
  for (var j in obj) {
    if (typeof obj[j] == 'object') {
      obj[j] = dgpk_arrange_raw_object(obj[j]);
    } else {
      if (!isNaN(obj[j])) {
        obj[j] = parseFloat(obj[j]);
      } else if (typeof obj[j] == 'string') {
        obj[j] = decodeURIComponent(obj[j].replace(/\+/g, ' ')).trim();
      }
    }
  }
  return obj;
}}}