|
本館粉絲專頁 |
|
|
使用者:Digipunk/mw-common.js
台灣棒球維基館
注意:保存設置後,要清掉瀏覽器的緩存才能生效:Mozilla:點刷新(或用快鍵Ctrl-R),IE / Opera: Ctrl-F5, Safari: Cmd-R, Konqueror Ctrl-R。
// 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 importScript('User:Digipunk/TeamX/func.js'); // TeamX stuff // 處理新版 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 />'); } var dgpkmv = dgpk_get_cookie('dgpkmv'); var dgpk_num_async_blocks = 0; // async generated blocks that affect the viewport position var dgpk_async_subpages = false;//true; // async subpages // 雖然 sync 會被瀏覽器所討厭,但在 subpages 這個應用裡 sync 比較恰當且單純,載入時不會明顯上下跳動 var dgpk_dd = new Date(); window.addEventListener("load", function(event) { if (dgpk_async_subpages == true) { DGPKJS_ASYNC_SUBPAGES(0); } else { DGPK_DO_ACTIONS(); } }, false); function DGPK_DO_ACTIONS() { DGPK_ONLOAD_ACTIONS(); DGPK_CHECK_PREVIEW(); if (dgpk_num_async_blocks > 0) { window.addEventListener("beforeunload", function(event) { var mv = window.pageYOffset || window.document.documentElement.scrollTop || window.document.body.scrollTop || 0; dgpk_set_cookie('dgpkmv', mv, '', 3); }, false); } } function DGPK_ONLOAD_ACTIONS() {{{ if (dgpk_async_subpages == false) { try { DGPKJS_SYNC_SUBPAGES(); } catch (e) { console.log(e); } } try { DGPKJS_SC(); } catch (e) { console.log(e); } try { DGPKJS_CSS_JS(); } catch (e) { console.log(e); } // 以上可能需要後續處理,這行寫在這裡 //try { DGPKJS_TDU(); } catch (e) { console.log(e); } try { DGPKJS_FC(); } catch (e) { console.log(e); } try { DGPKJS_STATS(); } catch (e) { console.log(e); } try { DGPKJS_SORTALBE(); } catch (e) { console.log(e); } // TABLE.sortable TD.fixed-ord sno try { DGPKJS_NEWS(); } catch (e) { console.log(e); } //try { DGPKJS_GETTY_IMAGES(); } catch (e) { console.log(e); } // enable Getty Images embedding syntax //try { DGPKJS_EDIT_RESTRICTION(); } catch (e) { console.log(e); } try { DGPKJS_UNICODE(); } catch (e) { console.log(e); } try { DGPKJS_XXT(); } catch (e) { console.log(e); } }}} //-------------------------------------------------- // 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].remove(); } } var JS = document.getElementsByClassName('DGPK-js'); for (var zzi = JS.length - 1; zzi >= 0; zzi--) { (function () { eval(JS[zzi].innerText); }()); // 丟到函式裡執行,避免 eval 裡的變數影響這裡 JS[zzi].remove(); } }}} 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; } } }}} //-------------------------------------------------- // Utilities //-------------------------------------------------- // normalize uri to conform to wiki format function dgpk_wikilink(str) {{{ str = str.replace(/ /g, '_'); var pos = str.indexOf('#'); if (pos < 0) { return encodeURI(str); } else { return encodeURI(str.substr(0, pos)) + '#' + dgpk_wikienc(str.substr(pos + 1)); } }}} function dgpk_wikienc(str) {{{ return encodeURIComponent(str.replace(/ /g, '_')).replace(/%/g, '.'); }}} // rebuild wiki objects from text function dgpk_wikiobj(txt) {{{ return ('' + txt).replace(/\[\[(File|Image|檔案|圖片):([^\]\|]+)([^\]]*)\]\]/g, function(x, a, b, c) { return '<IMG SRC="' + dgpk_fileurl(b) + '"' + c.replace(/^.*\|\s*(\d+)\s*px.*$/, function(z, n) { // 需考量不同寫法與來源,無法一行解決,對得不拆成兩行 return (n ? ' STYLE="width:' + n + 'px;height:auto"' : ''); }) + '/>'; }).replace(/\[\[([^\]\|]+)\|?([^\]\|]*)\]\]/g, function(x, a, b) { if (b) { return '<A HREF="' + mw.config.get('wgScript') + '/' + a + '">' + decodeURI(b) + '</A>'; } else { return '<A HREF="' + mw.config.get('wgScript') + '/' + a + '">' + decodeURI(a) + '</A>'; } }).replace(/\[(http:|https:|ftp:)?\/\/([^\]\s]+)\s+([^\]]+)\]/g, function(x, a, b, c) { return '<A HREF="' + (a || '') + '//' + b + '" CLASS="external text">' + c + '</A>'; }); }}} // url of file function dgpk_fileurl(wiki_filename) {{{ return mw.config.get('wgScript') + '/Special:Filepath/' + wiki_filename; }}} // normalize raw object 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 (obj[j] !== '' && !isNaN(obj[j])) { obj[j] = parseFloat(obj[j]); } else if (typeof obj[j] == 'string') { obj[j] = decodeURIComponent(obj[j].replace(/\+/g, ' ')).trim(); } } } return obj; }}} function dgpk_to_array(str, tok, no_empty) {{{ if (str === null || typeof str == 'undefined') return []; str = '' + str; if (!str) return []; if (tok == null) tok = '/'; if (no_empty == null) no_empty = 0; var arr, arr2 = []; arr = str.split(tok); for (var i = 0; i < arr.length; i++) { var val = arr[i].replace(/(^( )+|( )+$)/, ' ').trim(); if (val !== '' && !isNaN(val)) { val = parseFloat(val); } if (no_empty == 1) { if (val !== '') { arr2.push(val); } } else { arr2[i] = val; } } return arr2; }}} // [cookie] function dgpk_set_cookie(name, value) {{{ // arg[2] == null 現行目錄 var date = new Date(); var path = (arguments.length > 2 ? ';path=' + arguments[2] : ''); date.setTime(date.getTime() + (arguments.length > 3 ? arguments[3] * 1000 : 365*10*86400*1000)); var expires = '; expires=' + date.toGMTString(); document.cookie = name + '=' + value + expires + path; }}} function dgpk_get_cookie(cname) {{{ var name = cname + '='; var ca = document.cookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i].trim(); if (c.indexOf(name) == 0) return c.substring(name.length, c.length); } return ''; }}} // [ajax] function __dgpk_create_http_request() {{{ var http_request = null; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari http_request = new XMLHttpRequest(); } else {// code for IE6, IE5 http_request = new ActiveXObject('Microsoft.XMLHTTP'); } return http_request; }}} function __dgpk_ajax_connection(m, url, data, async, cb_completed, cb_args) {{{ var http_req = null; var method = m.toUpperCase(); try { if ((http_req = __dgpk_create_http_request()) == null) return null; http_req.open(method, url, async); if (async) { //http_req.onload = cb_completed; http_req.onload = function() { cb_completed && cb_completed.call(this, this.responseText, cb_args) }; http_req.send(data); return true; } else { http_req.send(data); return http_req.responseText; } } catch (err) { console.log('連線發生問題:' + err.message); } return null; }}} function dgpk_ajax_async_get(url, data, cb_completed, arg1, arg2) {{{ return __dgpk_ajax_connection('GET', url, data, true, cb_completed, arg1, arg2); }}} function dgpk_ajax_sync_get(url, data) {{{ return __dgpk_ajax_connection('GET', url, data, false); }}} function dgpk_strip_tags(val) {{{ if (typeof(val) == 'string') { return val.replace(/<\/?[^>]+>/g, ''); } else { return val; } }}} function dgpk_round(v, n, no_leading_zero) {{{ if (n == null || n <= 0) return Math.round(v); var zerostr = '0000000000'.substr(0, n); var ret; v = '' + (Math.round(parseFloat(v) * Math.pow(10, n)) / Math.pow(10, n)); if (v.indexOf('.') < 0) { ret = v + '.' + zerostr; } else { var tmp = v.split('.'); ret = (tmp[0] + '.' + ('' + tmp[1] + zerostr).substr(0, n)); } if (no_leading_zero == 1) { return ret.replace(/^0\./, '.'); } else { return ret; } }}} function dgpk_usleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } //-------------------------------------------------- // [2019-05-13] // enable Getty Images embedding syntax // Format: <a ...>Embed from Getty Images</a><script>...</script><script src=...></script> // 簡單限定在 gettyimages.com,此限制不嚴謹,先暫時如此處理 //-------------------------------------------------- /* 因版權問題,取消此功能 function DGPKJS_GETTY_IMAGES() {{{ var GETTY_IMAGES = document.getElementsByClassName('DGPK-Getty-Images'); for (var zzi = GETTY_IMAGES.length - 1; zzi >= 0; zzi--) { // restore http/src links rewritten by wiki var str = GETTY_IMAGES[zzi].innerHTML.replace(/<a [^>]*href="([^"]+)">[^<>]*<\/a>/g, '$1').replace(/>/g, '>').replace(/</g, '<').trim(); var mat = null; if (str.indexOf('gettyimages.com') < 0) continue; if (str.indexOf('<iframe ') >= 0) { //console.log('Getty Images - Lagacy Code'); GETTY_IMAGES[zzi].outerHTML = str; // replace element } else if (str.indexOf('<script') >= 0) { //console.log('Getty Images - Standard'); // remote JS - do once if ((mat = str.match(/<script [^>]*src=['"]([^'"]+)['"][^>]*><\/script>/)) != null) { getty_images_js = true; var imported = document.createElement('script'); imported.src = mat[1]; document.head.appendChild(imported); str = str.replace(mat[0], ''); } // local JS if ((mat = str.match(/<script>(.*)<\/script>/)) != null) { eval(mat[1]); str = str.replace(mat[0], ''); } //GETTY_IMAGES[zzi].remove(); GETTY_IMAGES[zzi].outerHTML = str; // replace element } } }}} */ //-------------------------------------------------- // [2019-08-24] // edit restriction via JS //-------------------------------------------------- function DGPKJS_EDIT_RESTRICTION() {{{ var dd_mon = dgpk_dd.getMonth() + 1; var dd_day = dgpk_dd.getDate(); if (dd_mon < 10) dd_mon = '0' + dd_mon; if (dd_day < 10) dd_day = '0' + dd_day; var today = dgpk_dd.getFullYear() + '-' + dd_mon + '-' + dd_day; if (mw.config.get('wgTitle').indexOf('二十九屆亞洲棒球錦標賽') > 0 && mw.config.get('wgUserGroups').indexOf('sysop') < 0 && //mw.config.get('wgUserGroups').indexOf('bureaucrat') < 0 && ['Digipunk', 'Good boy'].indexOf(mw.config.get('wgUserName')) < 0 && mw.config.get('wgAction') == 'edit' && //document.getElementById('editform') != null && document.getElementById('wpTextbox1') != null && ( document.getElementById('wpTextbox1').innerHTML.indexOf('{{SBDW') >= 0 || document.getElementById('wpTextbox1').innerHTML.indexOf('{{賽程表') >= 0 || document.getElementById('wpTextbox1').innerHTML.indexOf('MODE=ROSTER-BEGIN') >= 0 || //document.getElementById('wpTextbox1').innerHTML.indexOf('DGPK-awards') >= 0 || //document.getElementById('wpTextbox1').innerHTML.indexOf('=={{TeamX|') >= 0 || 0 ) && today >= '2019-10-03' && today <= '2019-10-20') { document.getElementById('editform').onsubmit = function() { window.location.href = mw.config.get('wgServer') + mw.config.get('wgScript') + '/' + mw.config.get('wgPageName'); return false; }; } }}} //-------------------------------------------------- // [2019-09-08] // format the content of external links in DGPK-news //-------------------------------------------------- function DGPKJS_NEWS() {{{ var NEWS = document.getElementsByClassName('DGPK-news'); for (var i = 0, ii = NEWS.length; i < ii; i++) { var A = NEWS[i].getElementsByTagName('A'); for (var j = 0, jj = A.length; j < jj; j++) { if (A[j].className.indexOf('external') >= 0) { var mat = A[j].innerHTML.match(/^(\d\d\d\d-\d\d-\d\d)\s+(.*)$/); if (mat) { var descr = ''; if (mat[2]) { descr = mat[2]; if (descr.split(/\s+/).length = 2 && descr.indexOf(' ') < 0) { descr = descr.replace(' ', ' '); } } //A[j].innerHTML = '<S>' + mat[1] + '</S> ' + descr; A[j].innerHTML = '<tt>[' + mat[1] + ']</tt> ' + descr; } } } } }}} //-------------------------------------------------- // [2019-11-18] // insert subpages via ajax // main page: <DIV CLASS="DGPK-subpages" DATA-SUBPAGE="{$subpage_name}"></DIV> (may be multiple occurrences) // sub pages: <DIV CLASS="DGPK-subpage-start"></DIV> ... <DIV CLASS="DGPK-subpage-end"></DIV> (may be multiple occurrences) //-------------------------------------------------- //-------- // async //-------- var dgpk_subpages = 0; function DGPKJS_ASYNC_SUBPAGES(round) {{{ var tok1 = '<div class="DGPK-subpage-start"></div>'; var tok2 = '<div class="DGPK-subpage-end"></div>'; var len1 = tok1.length; var SUBP = document.getElementsByClassName('DGPK-subpages'); var PAGE = []; for (var i = 0, ii = SUBP.length; i < ii; i++) { if (SUBP[i].getAttribute('data-subpage')) { SUBP[i].id = 'DGPK-SUBP-' + PAGE.length; PAGE.push(SUBP[i].getAttribute('data-subpage')); } } var num = PAGE.length; dgpk_subpages += num; for (var i = 0; i < num; i++) { var url = window.location.href.replace('?title=', '/').split('#')[0].split('?')[0].split('&')[0] + '/' + PAGE[i]; dgpk_ajax_async_get(url, null, function(resp, args) { if (resp) { var pos1 = pos2 = 0; var html = ''; while ((pos1 = resp.indexOf(tok1, pos2)) > 0 && (pos2 = resp.indexOf(tok2, pos1)) > 0) { html += resp.substr(pos1 + len1, pos2 - pos1 - len1); } var subp = document.getElementById('DGPK-SUBP-' + args.idx); subp.outerHTML = '<DIV STYLE="visibility:none;width:0;height:0;overflow:hidden">' + subp.innerHTML + '</DIV>' + html; } if (--dgpk_subpages == 0) { // all done if (DGPKJS_ASYNC_SUBPAGES(i) == 0) { // and no deeper-level subpages DGPK_DO_ACTIONS(); if (dgpkmv) window.scrollTo(0, dgpkmv); } } }, {idx:i} ); } if (round == 0) { if (num > 0) { // 若有 subpages,記錄 reload 前的捲動距離 dgpk_num_async_blocks += num; } else { DGPK_DO_ACTIONS(); } } return num; }}} //---------- // sync //---------- function DGPKJS_SYNC_SUBPAGES() {{{ var tok1 = '<div class="DGPK-subpage-start"></div>'; var tok2 = '<div class="DGPK-subpage-end"></div>'; var len1 = tok1.length; var PAGE = document.getElementsByClassName('DGPK-subpages'); var ok = 0; var url = window.location.href.replace('?title=', '/').split('#')[0].split('?')[0].split('&')[0] + '/'; for (var i = PAGE.length - 1; i >= 0; i--) { var resp = dgpk_ajax_sync_get(url + PAGE[i].getAttribute('data-subpage')); if (resp) { var pos1 = pos2 = 0; var html = ''; while ((pos1 = resp.indexOf(tok1, pos2)) > 0 && (pos2 = resp.indexOf(tok2, pos1)) > 0) { html += resp.substr(pos1 + len1, pos2 - pos1 - len1); ok++; } PAGE[i].outerHTML = '<DIV STYLE="visibility:none;width:0;height:0;overflow:hidden">' + PAGE[i].innerHTML + '</DIV>' + html; } } if (ok > 0) DGPKJS_SYNC_SUBPAGES(); // deal with multi-level subpages }}} /* function DGPKJS_TDU() {{{ var TDU = document.getElementsByClassName('DGPK-TDU'); var n = TDU.length; if (n > 0) { for (var i = n - 1; i >= 0; i--) { var dt = new Date(); var NOW = { y: dt.getFullYear(), m: dt.getMonth() + 1, d: dt.getDate() }; var update = TDU[i].getAttribute('data-str'); var UPDATE = { y: parseInt(substr(update, 0, 4)), m: parseInt(substr(update, 4, 2)), d: parseInt(substr(update, 6, 2)) }; var DIFF = { y:0, m:0, d:0 }; var str = ''; if (NOW.y == UPDATE.y) { if (NOW.m == UPDATE.m) { DIFF.d = NOW.d - UPDATE.d; } else { if (NOW.d > UPDATE.d) { DIFF.m = NOW.m - UPDATE.m; DIFF.d = NOW.d - UPDATE.d; } else { } } } else { } TDU[i].outerHTML = str; } } }}} */ //-------------------------------------------------- // [2020-09-09] // display unicode characters //-------------------------------------------------- function DGPKJS_UNICODE() {{{ /* 此方法無法處理鏈結 var UC = document.getElementsByClassName('DGPK-unicode'); var num_uc = UC.length; for (var i = num_uc - 1; i >= 0; i--) { var str = ''; var mat = UC[i].innerText.trim().match(/^U\+([0-9A-F]+)$/i); if (mat) { //eval('str = "\\u{' + mat[1] + '}";'); // 這樣寫也行,只是不帥 str = String.fromCodePoint(parseInt('0x' + mat[1])); } UC[i].outerHTML = str; } */ /* 此方法導致其它 JS 無法運作,因內容全被置換 var page = document.documentElement; page.innerHTML = page.innerHTML.replace(/%(U\+)?([0-9A-F]+)%/ig, function(mat) { return String.fromCodePoint(parseInt('0x' + mat.replace(/[^0-9A-F]/ig, ''))); }); */ // 笨方法,沒效率,但滿足需求 var regex = new RegExp(/%U\+[0-9A-F]{4,}%/ig); __dgpk_unicode(document.body, regex); if (regex.test(document.title)) { document.title = __dgkpk_unicode_replace(document.title, regex); } }}} function __dgpk_unicode(o, regex) {{{ // recursive function if (regex.test(o.innerText)) { if (o.children.length > 0) { for (var i = 0; i < o.children.length; i++) { __dgpk_unicode(o.children[i], regex); } } if (regex.test(o.innerText)) { o.innerHTML = __dgkpk_unicode_replace(o.innerHTML, regex); } } }}} function __dgkpk_unicode_replace(str, regex) {{{ return str.replace(regex, function(mat) { return String.fromCodePoint(parseInt('0x' + mat.replace(/[^0-9A-F]/ig, ''))); }); }}} //-------------------------------------------------- // [2021-08-02] // XXT stuff //-------------------------------------------------- function DGPKJS_XXT() {{{ let py = mw.config.get('wgTitle').substr(0, 4); if (isNaN(py) || parseInt(py) != dgpk_dd.getFullYear()) return false; document.querySelectorAll('DIV.DGPK-XXT TD[class^="info"]').forEach(td => { let mat = td.innerText.match(/(\d+)[\/\-](\d+) \d+:\d+/); mat && (parseInt(mat[1]) == dgpk_dd.getMonth() + 1) && (parseInt(mat[2]) == dgpk_dd.getDate()) && td.classList.add('today'); }); }}}