User:Ricky Lau/rac.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//<nowiki>
//racAddToList
function racAddToList () {
if (wgAction == 'edit' && wgPageName.endsWith ('racContent.js')){
if (Object.isUndefined (racGETAddToList) || racGETAddToList == null || racGETAddToList.blank()) { return; }
var code =$F('wpTextbox1');
var findCat = racGETAddToList.match(/cat:'([^\']+)'/);
var cat = Object.isArray (findCat)?findCat[1]:'';
var find = '//' + cat + 'newContent';
racGETAddToList = 'racContents.push (' + racGETAddToList + ');';
if (code.include (find)) {
$('wpTextbox1').value = code.replace (find, racGETAddToList + String.fromCharCode (13) + find);
}else{
var newCat = '//newCategory';
$('wpTextbox1').value = code.replace (newCat, '/*' + String.fromCharCode (13) + '==' + cat + '==' + String.fromCharCode (13) + '*/' + String.fromCharCode (13) + racGETAddToList + String.fromCharCode (13) + find + String.fromCharCode (13) + newCat);
}
$('wpSummary').value = '';
$('wpMinoredit').checked = true;
$('wpSave').click ();
}else{
window.open (mw.config.get('wgServer') + mw.config.get('wgScript') + '?' + Object.toQueryString ({
title: 'User:Ricky Lau/racContent.js',
action: 'edit',
racaddtolist: $F('racContent').strip()
}), '_blank');
}
}
//racAppendArg
function racAppendArg (arg, assign) {
if (!$F('racContent').endsWith ('|')) { $('racContent').value+= '|'; }
$('racContent').value+= arg + (assign?'=':'');
}
//racAppendSig
function racAppendSig (prefix) {
racGETContent += ((prefix)?prefix:'') + racSettings.sig;
}
//racApplyStubTp
function racApplyStubTp (tag) {
$('racContent').value = tag;
$('racPosition').value = '頁底';
$('racType').value = '掛模板';
$('racAddition').value = '';
$('racSummery').value = '速掛' + tag + '小作品模板';
}
//racCheckCnt
function racCheckCnt () {
racContents.pluck ('id').uniq().without ('').each (function (el) {
var cnt = racContents.findAll (function (cnt) { return cnt.id == el; });
if (cnt.length > 1) {
var duplicates = [];
cnt.each (function (item) { duplicates.push (item.txt + '@' + item.cat); });
alert ('工具列表出現重複ID:' + el + '\n' + duplicates.join ('\n') + '\n強烈建議修改列表以免運行錯誤。');
}
});
}
//racClick
function racClick (id) {
var racEl = racContents.find (function (c) { return (c.id == id); });
if (!racEl) { return; }
if (racEl.call) {
eval (racEl.call);
return;
}
if (racEl.href) {
window.open(mw.config.get('wgServer') + mw.config.get('wgScript') + '?' + Object.toQueryString ({
title: racEl.href,
action: 'view'
}),'_blank');
return;
}
$('racContent').value = ((racEl.tag)?racEl.tag:'');
$('racPosition').value = ((racEl.pos)?racEl.pos:'頁頂');
$('racType').value = ((racEl.type)?racEl.type:'');
if (racEl.needArg) { $('racContent').value+= '|'; }
if (racEl.append) { $('racContent').value+= racEl.append; }
$('racAddition').value = ((racEl.addition)?racEl.addition:'');
if (racEl.summery) {
$('racSummery').value = racEl.summery;
} else {
if (racEl.type == '掛模板') { $('racSummery').value = '速掛' + racGetTagName (racEl.tag) + '模板'; }
}
if (racEl.type == '掛模板') { racToggleLocation (true); }
racSuggest ();
}
//racCreateUI
function racCreateUI () {
//Make main frame
var racFrame = new Element ('div', { 'class': 'NavFrame', id: 'racFrame' });
$('contentSub').insert ({ after: racFrame });
//Make a title bar
var racTitle = new Element ('div', { 'class': 'NavHead', id: 'racTitle' });
racTitle.setStyle ({ cursor: 'pointer' });
racTitle.update ('Rapid Add Content');
racTitle.observe ('click', function () { racContainer.toggle (); });
racFrame.insert (racTitle);
//Make a container
var racContainer = new Element ('div', { 'class': 'NavContent', id: 'racContainer' });
racContainer.setStyle ({ fontSize: '10pt' });
racFrame.insert (racContainer);
if (racExcludeThisPage) { racContainer.hide () }
//Make racType
var racType = new Element ('input', { 'class': 'racInput', id: 'racType', type: 'text' });
racType.setStyle ({ fontFamily: 'courier', fontSize: '10pt', marginTop: '1px', verticalAlign: 'top', width: '100px' });
racContainer.insert (racType);
//Make racContent
var racContent = new Element ('textarea', { 'class': 'racInput', id: 'racContent', onkeypress: 'racSuggest();racStubSearch();', onchange: 'racSuggest();racSearchTp();', rows: '1', type: 'text' });
racContent.setStyle ({ fontFamily: 'courier', fontSize: '8pt', height: '16px', width: '580px' });
racContent.observe ('dblclick', racToggleContent);
racContainer.insert (racContent);
//Make racPosition
var racAtText = new Element ('span');
racAtText.update ('於');
racAtText.setStyle ({ paddingTop: '8px', verticalAlign: 'top' });
racContainer.insert (racAtText);
var racPosition = new Element ('input', { 'class': 'racInput', id: 'racPosition', type: 'text' });
racPosition.setStyle ({ fontFamily: 'courier', fontSize: '10pt', marginTop: '1px', verticalAlign: 'top', width: '100px' });
racContainer.insert (racPosition);
//Make racSummery
var racSummery = new Element ('input', { 'class': 'racInput', id: 'racSummery', type: 'text' });
racSummery.setStyle ({ fontSize: '10pt', width: '370px' });
racContainer.insert ('<br/>摘要:');
racContainer.insert (racSummery);
//Make racAddition
var racAddition = new Element ('input', { 'class': 'racInput', id: 'racAddition', type: 'hidden' });
racContainer.insert (racAddition);
//Make submit buttons
var racSumbitBtn = new Element ('input', { id: 'racSubmit', type: 'button', value: '給我立即掛上' });
racSumbitBtn.setStyle ({ fontSize: '10pt', fontWeight: 'bold', width: '130px' });
racSumbitBtn.observe ('mousedown', function () { racSubmit ('submit'); });
racContainer.insert (racSumbitBtn);
var racPreview = new Element ('input', { id: 'racPreview', type: 'button', value: '先來一次預覽' });
racPreview.setStyle ({ fontSize: '10pt', width: '130px' });
racPreview.observe ('mousedown', function () { racSubmit ('preview'); });
racContainer.insert (racPreview);
var racAddContent = new Element ('input', { id: 'racAddContent', type: 'button', value: '加入編輯欄位' });
racAddContent.setStyle ({ fontSize: '10pt', width: '130px' });
racAddContent.observe ('mousedown', function () { racSubmit ('addonly'); });
racContainer.insert (racAddContent);
//Make racSuggestion
var racSuggestion = new Element ('div', { id: 'racSuggestion' });
racSuggestion.setStyle ({ fontSize: '10pt' });
racContainer.insert (racSuggestion);
//Make racTemplateSearch
var racTemplateSearch = new Element ('div', { id: 'racTemplateSearch' });
racTemplateSearch.setStyle ({ fontSize: '8pt' });
racContainer.insert (racTemplateSearch);
//run racSuggest once
racSuggest ();
}
//racExclude
function racExclude () {
if (wgAction == 'edit' && wgPageName.endsWith ('racExclude.js')){
racGETExcludePageId = (racGETExcludePageId.empty())?wgArticleId:racGETExcludePageId;
if ($F('wpTextbox1').include('racExcludeList.push(\'' + racGETExcludePageId + '\');')){
$('wpTextbox1').value = $F('wpTextbox1').replace ('racExcludeList.push(\'' + racGETExcludePageId + '\');','');
}else{
$('wpTextbox1').value+= 'racExcludeList.push(\'' + racGETExcludePageId + '\');';
}
$('wpSummary').value = '';
$('wpMinoredit').checked = true;
$('wpSave').click ();
}else{
document.location = mw.config.get('wgServer') + mw.config.get('wgScript') + '?' + Object.toQueryString ({
title: 'User:Ricky Lau/racExclude.js',
action: 'edit',
racexclude: wgArticleId
});
}
}
//racGetTagName
function racGetTagName (txt) {
return txt.split ('|').first ().gsub(/(subst\:|\/auto)/, '');
}
//racMain
function racMain () {
//racGET values - in edit page only
if (wgAction == 'edit'){
racGETType = $GET ('ractype');
racGETContent = $GET ('raccontent');
racGETPosition = $GET ('racposition');
racGETSummery = $GET ('racsummery');
racGETAddition = $GET ('racaddition');
racGETAction = $GET ('racaction');
racGETExcludePageId = $GET ('racexclude');
racGETAddToList = $GET ('racaddtolist')
}
if (racGETAddToList != null && wgAction == 'edit') {
racAddToList ();
}else if (racGETExcludePageId != null && wgAction == 'edit') {
racExclude ();
}else if (!Object.isUndefined (racGETContent) && racGETContent != null && wgAction == 'edit') {
racSave ();
}else{
if (wgNamespaceNumber < 0 || (wgAction != 'view' && wgAction != 'purge' && wgAction != 'edit')) { return; }
racCheckCnt ();
racCreateUI ();
}
}
//racReset
function racReset () { racSearchStub = false; $$('.racInput').each (function (el) { el.clear(); }); racToggleLocation (false); racSuggest (); }
//racSave
function racSave () {
if (wgAction != 'edit') { alert ('Invalid call for racSave ()!'); return; }
switch (racGETType) {
case '掛模板':
racGETContent = '{{' + racGETContent + '}}';
break;
case '增加侵權項目':
racGETContent = '*[[' + racGETContent + ']]';
break;
}
if (racGETAddition != null) { eval (racGETAddition); }
if ($GET ('section') != null && racGETPosition == '頁首') { racGETPosition = '開首'; }
switch (racGETPosition) {
case '頁底':
case '結尾':
$('wpTextbox1').value += String.fromCharCode (13) + racGETContent;
break;
case '開首':
alert ($F('wpTextbox1').toSource ());
var heading = $F('wpTextbox1').match (/^(.+)/)[1];
$('wpTextbox1').value = $F('wpTextbox1').replace (heading, heading + String.fromCharCode (13) + racGETContent);
break;
case '整頁':
$('wpTextbox1').value = racGETContent;
break;
case '頁頂':
default:
$('wpTextbox1').value = racGETContent + String.fromCharCode (13) + $('wpTextbox1').value;
break;
}
$('wpSummary').value += ($F('wpSummary').empty?'':'、') + racGETSummery + ((wgTitle.include ('沙盒') && wgNamespaceNumber != 0)?'(RAC測試)':'');
switch (racGETAction) {
case 'submit':
$('wpSave').click ();
break;
case 'preview':
$('wpPreview').click ();
break;
}
}
//racSearchTp
function racSearchTp () {
if (racSearchStub) {
$('racTemplateSearch').update ('小作品模板搜索已開啟');
$('racTemplateSearch').setStyle ({ color: '#080' });
return;
}
$('racTemplateSearch').update ();
$('racTemplateSearch').setStyle ({ color: '#000' });
if ($F('racType').strip () == '掛模板') {
new Ajax.Request (mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + Object.toQueryString ({
action:'query',
titles:'Template:' + racGetTagName ($F('racContent')),
format:'xml'
}),{
onSuccess: function (response){
var result = response.responseXML.getElementsByTagName('page')[0].getAttribute('missing');
var isExist = Object.isUndefined (result) || result == null;
if (!isExist){
$('racTemplateSearch').update ('模板' + racGetTagName ($F('racContent')) + '不存在');
$('racTemplateSearch').setStyle ({ color: '#f00' });
}}});
}
}
//racStubSearch
var racSearchDelay;
function racStubSearch (delayed) {
if (!racSearchStub) { return; }
if (!delayed) {
clearTimeout (racSearchDelay);
racStubSearch.delay (2, true);
return;
}
new Ajax.Request (mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + Object.toQueryString ({
action:'query',
list:'search',
srnamespace:'10',
srwhat:'text',
srsearch: $F('racContent') + ' 小作品',
format:'xml'
}),{
onCreate: function () {
$('racTemplateSearch').update ('搜索' + $F('racContent') + '有關之小作品模板中…');
},
onComplete: function () {
$('racTemplateSearch').update ('搜索完成');
},
onSuccess: function (response){
var results = response.responseXML.getElementsByTagName('p');
$('racSuggestion').update ();
racContents.findAll (function (c) { return (c.cat == ''); }).each (function (el) {
$('racSuggestion').insert (((el.id)?'<a href="#" onmousedown="racClick (\'' + el.id + '\');">' + el.txt + '</a>':el.txt) + ' ');
});
for (var n = 0; n < results.length; n++) {
var tag = results[n].getAttribute ('title').replace (/Template\:/,'');
if (tag.endsWith ('/doc')) { return; }
$('racSuggestion').insert ('<a href="#" onmousedown="racApplyStubTp (\'' + tag + '\');">' + tag + '</a> ');
}
}});
}
//racSubmit
function racSubmit (action) {
if ($F('racContent').blank () || $F('racType').blank () ) { return; }
if (wgAction == 'edit') {
racGETType = $F('racType').strip ();
racGETContent = $F('racContent').strip ();
racGETPosition = $F('racPosition').strip ();
racGETSummery = $F('racSummery').strip ();
racGETAddition = $F('racAddition').strip ();
racGETAction = action.strip ();
racSave ();
}else{
var query = {title: wgPageName,
action: 'edit',
ractype: $F('racType').strip (),
raccontent: $F('racContent').strip (),
racposition: $F('racPosition').strip (),
racsummery: $F('racSummery').strip (),
racaddition: $F('racAddition').strip (),
racaction: action.strip ()
}
if ($F('racPosition').strip ().match(/第(\d+)節(上方|開首|結尾)/)) {
var match = $F('racPosition').strip ().match(/第(\d+)節(上方|開首|結尾)/);
query.section = match [1];
query.racposition = match [2];
}
document.location = mw.config.get('wgServer') + mw.config.get('wgScript') + '?' + Object.toQueryString (query);
}
}
//racSuggest
function racSuggest (cat) {
$('racSuggestion').update ();
racContents.findAll (function (c) { return (c.cat == ''); }).each (function (el) {
$('racSuggestion').insert (((el.id)?'<a href="#" onmousedown="racClick (\'' + el.id + '\');">' + el.txt + '</a>':el.txt) + ' ');
});
var racContentParts = $F('racContent').split('|');
if (racContents.pluck ('tag').include (racContentParts.first ())) {
if (racContents.find (function (c) { return (c.tag == racContentParts.first ()); }).arg){
racContents.find (function (c) { return (c.tag == racContentParts.first ()); }).arg.each (function (arg) {
if (!Object.isUndefined (arg.arg)){
$('racSuggestion').insert ('<a href="#" onmousedown="racAppendArg (\'' + arg.arg + '\', ' + (arg.assign?'true':'false') + ');">' + arg.txt + '</a> ');
}else if (!Object.isUndefined (arg.href)){
$('racSuggestion').insert ('<a href="' + mw.config.get('wgServer') + mw.config.get('wgScript') + '?' + Object.toQueryString ({
title: arg.href,
action: 'view'
}) + '" target="_blank">' + arg.txt + '</a> ');
}else if (!Object.isUndefined (arg.call)){
$('racSuggestion').insert ('<a href="#" onmousedown="' + arg.call + '">' + arg.txt + '</a> ');
}else{
$('racSuggestion').insert (arg.txt + ' ');
}
});
}
} else {
if (cat != undefined) {
racContents.findAll (function (c) { return (c.cat == cat); }).each (function (el) {
$('racSuggestion').insert ('<a href="#"' + ((el.id)?' onmousedown="racClick (\'' + el.id + '\');"':'') + '>' + el.txt + '</a> ');
});
} else {
racContents.pluck ('cat').uniq ().without ('').each (function (el) {
$('racSuggestion').insert ('<a href="#" onmousedown="racSuggest (\'' + el + '\');">' + el + '</a> ');
});
}
}
}
//racToggleContent
var racContentExpanded = false;
function racToggleContent () {
$('racContent').setStyle ({ height: (racContentExpanded?'16':'60') + 'px' });
racContentExpanded = !racContentExpanded;
}
//racToggleLocation
function racToggleLocation (forceSet) {
if (!Object.isUndefined (forceSet)) { racInsertLocation = !forceSet; }
if (racInsertLocation) {
$$('.racLocation').each (function (el) { el.remove (); });
}else{
var editsection;
var location;
var start;
var end;
var above;
$$('.editsection').each (function (editsection) {
var editLink = editsection.childElements ().find (function (el) { return el.nodeName.toLowerCase () == 'a'; });
if (!editLink) { return; }
var section = '第' + editLink.href.match(/section=(\d+)/)[1] + '節';
if (section == '第0節') { return; }
location = editsection.childElements ().find (function (el) { return el.hasClassName ('racLocation'); });
if (!location) {
location = new Element ('span', { 'class': 'racLocation' });
editsection.insert (editsection.innerHTML.blank () || editsection.innerHTML.endsWith(' ')?'':' ');
editsection.insert (location);
}
above = location.childElements ().find (function (el) { return el.hasClassName ('racAddAbove'); });
if (!above) {
above = '[<a class="racAddAbove" href="#racFrame" onclick="$(\'racPosition\').value=\'' + section + '上方\'" title="加到' + section + '上方">此節上方</a>]';
location.insert (((location.innerHTML.blank () || location.innerHTML.endsWith(' '))?'':' ') + above);
}
start = location.childElements ().find (function (el) { return el.hasClassName ('racAddStart'); });
if (!start) {
start = '[<a class="racAddStart" href="#racFrame" onclick="$(\'racPosition\').value=\'' + section + '開首\'" title="加到' + section + '開首">此節開首</a>]';
location.insert (((location.innerHTML.blank () || location.innerHTML.endsWith(' '))?'':' ') + start);
}
end = location.childElements ().find (function (el) { return el.hasClassName ('racAddEnd'); });
if (!end) {
end = '[<a class="racAddEnd" href="#racFrame" onclick="$(\'racPosition\').value=\'' + section + '結尾\'" title="加到' + section + '結尾">此節結尾</a>]';
location.insert (((location.innerHTML.blank () || location.innerHTML.endsWith(' '))?'':' ') + end);
}
});
editsection = $('firstHeading').childElements ().find (function (el) { return el.hasClassName ('editsection'); });
if (!editsection) {
editsection = new Element ('span', { 'class': 'editsection' });
$('firstHeading').insert (editsection);
}
location = editsection.childElements ().find (function (el) { return el.hasClassName ('racLocation'); });
if (!location) {
location = new Element ('span', { 'class': 'racLocation' });
editsection.insert (editsection.innerHTML.blank () || editsection.innerHTML.endsWith(' ')?'':' ');
editsection.insert (location);
}
start = location.childElements ().find (function (el) { return el.hasClassName ('racAddStart'); });
if (!start) {
start = '[<a class="racAddStart" href="#racFrame" onclick="$(\'racPosition\').value=\'頁頂\'" title="加到頁頂">頁頂</a>]';
location.insert (((location.innerHTML.blank () || location.innerHTML.endsWith(' '))?'':' ') + start);
}
end = location.childElements ().find (function (el) { return el.hasClassName ('racAddEnd'); });
if (!end) {
end = '[<a class="racAddEnd" href="#racFrame" onclick="$(\'racPosition\').value=\'頁底\'" title="加到頁底">頁底</a>]';
location.insert (((location.innerHTML.blank () || location.innerHTML.endsWith(' '))?'':' ') + end);
}
above = location.childElements ().find (function (el) { return el.hasClassName ('racSubstitute'); });
if (!above) {
above = '[<a class="racSubstitute" href="#racFrame" onclick="$(\'racPosition\').value=\'整頁\'" title="替換整頁">整頁</a>]';
location.insert (((location.innerHTML.blank () || location.innerHTML.endsWith(' '))?'':' ') + above);
}
}
racInsertLocation = !racInsertLocation;
}
$ (racMain);
//</nowiki>