MediaWiki:Common.js
跳至導覽
跳至搜尋
注意:在您儲存之後您必須清除瀏覽器快取才可看到最新的變更。
- Firefox / Safari:按住 Shift 時點選 重新整理,或按 Ctrl-F5 或 Ctrl-R (Mac 則為 ⌘-R)
- Google Chrome:按 Ctrl-Shift-R (Mac 則為 ⌘-Shift-R)
- Internet Explorer:按住 Ctrl 時點選 重新整理,或按 Ctrl-F5
- Opera:前往 選單 → 設定 (在 Mac 為 Opera → 偏好設定) 然後再到 隱私 & 安全性 → 清除瀏覽資料 → 已快取的圖片與檔案。
/* 所有用戶在加載任何頁面時,這裡的JavaScript都會加載 == 辅助处理 == */ //功能設定 if(!window.JSConfig){var JSConfig={};} JSConfig.isEdit0=true; //設置是否顯示編輯首段按鈕 JSConfig.editSectionLink='right';//設置編輯按鈕是否在右側 JSConfig.collapseText=wgULS('隱藏▲','隱藏▲');//指示折疊收縮的默認文字 JSConfig.expandText=wgULS('顯示▼','顯示▼');//指示折疊展開的默認文字 JSConfig.autoCollapse=2; //文章少於 autoCollapse 個折疊塊時,不自動折疊 // 移動元素 function elementMoveto(node, refNode, pos){//默认位置为refNode前 if(node && refNode){ var parent=refNode.parentNode; if (pos && pos=='after') {refNode=refNode.nextSibling;} try { if(refNode){ parent.insertBefore(node, refNode); }else{ parent.appendChild(node); } } catch (DOMException) {}; } } //创建元素 function createElement(tag,children,props){ var element = document.createElement(tag); if(!(children instanceof Array)){children=[children];} for(var i=0;i<children.length;i++){ var child=children[i]; if(typeof child=='string'){child=document.createTextNode(child);} if(child){element.appendChild(child);} } if(typeof props=='object'){ for(var k in props){ switch(k){ case 'styles': var styles=props.styles; for(var s in styles){element.style[s]=styles[s];} break; case 'events': var events=props.events; for(var e in events){ addHandler(element,e,events[e]); } break; case 'class': element.className=props[k];break; default: element.setAttribute(k,props[k]); } } } return element; } //JS、CSS导入模块 importedURLs = {}; //用以保證腳本和樣式只载入一次 function importScript( page ) { var src= /^https?:\/\//.test(page) ? page : getWikiPath(page,{'ctype':'text/javascript'}); if( importedURLs[src] ) { return; } importedURLs[src] = true; var scriptElem = createElement( 'script',null,{'src':src,'type':'text/javascript'} ); document.getElementsByTagName( 'head' )[0].appendChild( scriptElem ); } function importStylesheet( page ) { var sheet= /^https?:\/\//.test(page) ? page : getWikiPath(page,{'ctype':'text/css'}); if( importedURLs[sheet] ) { return; } importedURLs[sheet] = true; var styleElem = createElement( 'style', ['@import "'+sheet+'";'], {'type':'text/css'} ); document.getElementsByTagName( 'head' )[0].appendChild( styleElem ); } //引入[[Special:Gadgets]]要求的腳本和樣式 if(window.requireScripts instanceof Array){ for(var i=0;i<requireScripts.length;i++){ importScript(requireScripts[i]); } } if(window.requireStylesheets instanceof Array){ for(var i=0;i<requireStylesheets.length;i++){ importStylesheet(requireStylesheets[i]); } } /* 测试元素中是否含有指定的样式 ************************************** * Description: 使用正则式与缓存来提高性能 * Maintainers: User:fdcn @zh.wikipedia * [[en:User:Mike Dillon]], [[en:User:R. Koot]], [[en:User:SG]] @en.wikipedia */ var hasClass = (function () { var reCache = {}; return function (element, className) { return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className); }; })(); /* == IE兼容性修正 == */ if (navigator.appName == "Microsoft Internet Explorer" && document.compatMode == "CSS1Compat"){ (function(){ var oldWidth; var docEl = document.documentElement; function fixIEScroll(){ if (!oldWidth || docEl.clientWidth > oldWidth){ doFixIEScroll(); }else{ setTimeout(doFixIEScroll, 1); } oldWidth = docEl.clientWidth; } function doFixIEScroll() { docEl.style.overflowX = (docEl.scrollWidth - docEl.clientWidth < 4) ? "hidden" : ""; } document.attachEvent("onreadystatechange", fixIEScroll); attachEvent("onresize", fixIEScroll); })(); } /* == 段落編輯連結 == */ //JSConfig.isEdit0 設置是否出現「編輯首段」按鈕 //對不需要編輯首段的頁面,還可在頁面中加 Template:NoEdit 模板來禁用。 //JSConfig.editSectionLink 設置「編輯」按鈕的位置 if (wgIsArticle && wgAction == "view") { //增加編輯首段按鈕 addOnloadHook(function(){ if(!JSConfig.isEdit0||(document.getElementById&&document.getElementById('no-edit-0'))){return;} var caEdit=document.getElementById&&document.getElementById('ca-edit'); if (!caEdit){return;} var linkAttributes={ 'href':caEdit.firstChild.href + '§ion=0', 'title':wgULS('編輯正文所有標題之前的文字(通常稱為首段或導言)','編輯正文所有標題之前的文字(通常稱首段或導言)'), 'accesskey':'0' } //增加0按鈕標籤 var caEdit0 = createElement( 'li', [createElement('A',['0'],linkAttributes)], {id:'ca-edit-0'} ); caEdit.className = 'istalk'; elementMoveto(caEdit0,caEdit,"after"); //增加條目標題下的「編輯首段」按鈕 var editsection0= createElement( 'span', ['[',createElement('A',[wgULS('編輯首段','編輯首段')],linkAttributes),']'], {'class':'editsection'} ); var siteSub=document.getElementById&&document.getElementById('siteSub'); elementMoveto( editsection0 , siteSub.firstChild ); }); //設置編輯按鈕位置是否浮動 addOnloadHook(function(){ if (JSConfig.editSectionLink=='right') { return; } var editLinks=getElementsByClassName(document.getElementById('bodyContent'),"span","editsection"); for(var i = 0; i < editLinks.length; i++) { editLinks[i].style.cssFloat = editLinks[i].style.float = 'none'; editLinks[i].style.textAlign = "left" ; editLinks[i].parentNode.appendChild(editLinks[i]); } }); } /* == 增加折疊功能 == */ /** 折疊 div table ***************************** * Description: 實現div.NavFrame和table.collapsible的可折疊性。 * JSConfig的collapseText、expandText、autoCollapse屬性定義默認文字和默認最少自動折疊塊 * Maintainers: User:fdcn */ addOnloadHook(function(){ function toggleState(item){ var oldState=item.state; item.state=1-oldState; if(item.text[0]){ item.text[oldState].style.display = 'inline'; item.text[item.state].style.display='none'; } item.action(item); } function cancelBubble(e){ e=e||window.event; if(e.stopPropagation){e.stopPropagation();}else{e.cancelBubble=true;} } function createToggleButton(head,frame,toggle){ var textS,textH; var button=getElementsByClassName(head,"span","NavToggle")[0]; if(button){ textS=getElementsByClassName(button,"span","NavToggleShow")[0] textH=getElementsByClassName(button,"span","NavToggleHide")[0]; }else { textS=createElement("span",[JSConfig.expandText]); textH=createElement('span',[JSConfig.collapseText]); button=createElement("span",[textS,textH],{'class':"NavToggle",styles:{'width':"3.8em"}}); } if(textS){textS.style.display='none';} button.style.display='inline'; var item={'state':0, 'text':[textS,textH],'frame':frame,'action':toggle} var links=head.getElementsByTagName("A"); for(var i=0,l;l=links[i];i++){ addClickHandler(l,cancelBubble); } head.insertBefore( button, head.childNodes[0] ); head.style.cursor = "pointer"; addClickHandler( head, function(){toggleState(item);} ); return item; } // 折疊div function toggleNavigationBar(item) { var cls=item.state?'none':'block'; for ( var NavChild = item.frame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ){ if (NavChild.className == 'NavPic' || NavChild.className == 'NavContent') { NavChild.style.display = cls; } } } // 折疊表格 function collapseTable( item ) { var rows = item.frame.getElementsByTagName( "tr" ); var display = item.state? 'none':rows[0].style.display; for (var i=1,row; row=rows[i]; i++) { row.style.display = display; } } //init var item,items=[]; var NavFrames=getElementsByClassName(document,"div","NavFrame"); for(var i=0,NavFrame;NavFrame = NavFrames[i];i++) { var heads=getElementsByClassName(NavFrame,"div","NavHead"); for(var ih=0,head; head = heads[ih]; ih++ ) { if (head.parentNode != NavFrame) {continue;} items.push(createToggleButton(head,NavFrame,toggleNavigationBar)); break; } } var tables = getElementsByClassName(document,"table","collapsible"); for ( var i = 0,table; table= tables[i]; i++ ) { var head = table.getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0]; items.push(createToggleButton(head,table,collapseTable)); } var count=items.length; for ( var i = 0; i<count; i++ ) { item=items[i]; if ( hasClass( item.frame, "collapsed" ) || ( count >= JSConfig.autoCollapse && hasClass( item.frame, "autocollapse" ) ) ) { toggleState(item); } } }); //修正折疊後定位變化 hookEvent("load",function(){if(location.hash){location.href=location.hash;}}); /* == 取消修訂編輯摘要修正 == */ /** fix edit summary prompt for undo this code fixes the fact that the undo function combined with the "no edit summary prompter" causes problems if leaving the edit summary unchanged this was added by [[:en:User:Deskana]], code by [[:en:User:Tra]] */ addOnloadHook(function () { var autoSummary=document.getElementsByName('wpAutoSummary')[0]; if (document.location.search.indexOf("undo=") != -1 && autoSummary) { autoSummary.value=''; } }) /*