|
在安装使用MediaWiki平台的时候,进行一些细节应用需要用到URL点击来编辑、预置、保存页面,有的时候希望能更自动化一些,以前就有过使用JavaScript扩展来实现自动编辑保存的功能,尝试详细了解、安装会发现用起来还是比较方便的,记录如下:
扩展使用说明
这是一个让用户使用JavaScript来实现通过URL自动编辑的功能。
可以在对整个网站所有用户的MediaWiki:Common.js中加入代码,也可以为单独个人增加这段JavaScript代码。
变量
?title=Article
&action=edit
&autoedit=s~...~...~g;s~...~...~g
&autoclick=wpDiff
&autominor=false
&autosummary=Summary
说明
?title=... – article title
&action=edit – action
&autoedit=... – autoedit script
s~ – begin of replace command
... – it will be replaced by next text (may be a regular expression); cannot contain ~ chars, for enter text to empty page (or new section in existing one) keep it empty
~
... – next text ;); cannot contain ~ chars
~g – end of replace command
Script can contain multiple commands, separated by ;
&autoclick=... – button pressed after autoedit (wpDiff, wpSave, wpPreview)
&autominor=... – is change minor
&autosummary=... – summary of edit
JavaScript代码
/* 此处的JavaScript将载入于所有用户每一个页面。 */ /*
*/
function getParamValue(paramName) {
var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
var h=document.location;
var m;
if (m=cmdRe.exec(h)) {
try {
return decodeURI(m[1]);
} catch (someError) {}
}
return null;
};
function substitute(data,cmdBody) {
// alert('sub\nfrom: '+cmdBody.from+'\nto: '+cmdBody.to+'\nflags: '+cmdBody.flags);
var fromRe=RegExp(cmdBody.from, cmdBody.flags);
return data.replace(fromRe, cmdBody.to);
};
function execCmds(data, cmdList) {
for (var i=0; i<cmdList.length; ++i) {
data=cmdList[i].action(data, cmdList[i]);
}
return data;
}
function parseCmd(str) {
// returns a list of commands
if (!str.length) return [];
var p=false;
switch (str.charAt(0)) {
case 's':
p=parseSubstitute(str);
break;
case 'j':
p=parseJavascript(str);
break;
default:
return false;
}
if (p) return [p].concat(parseCmd(p.remainder));
return false;
};
function unEscape(str, sep) {
return str.split('\\\\').join('\\')
.split('\\'+sep).join(sep)
.split('\\n').join('\n');
};
function runJavascript(data, argWrapper) {
// flags aren't used (yet)
// from the user's viewpoint,
// data is a special variable may appear inside code
// and gets assigned the text in the edit box
// alert('eval-ing '+argWrapper.code);
return eval(argWrapper.code);
};
function parseJavascript(str) {
// takes a string like j/code/;othercmds and parses it
var tmp,code,flags;
if (str.length<3) return false;
var sep=str.charAt(1);
str=str.substring(2);
tmp=skipOver(str,sep);
if (tmp) { code=tmp.segment.split('\n').join('\\n'); str=tmp.remainder; }
else return false;
flags='';
if (str.length) {
tmp=skipOver(str,';') || skipToEnd(str, ';');
if (tmp) {flags=tmp.segment; str=tmp.remainder; }
}
return { action: runJavascript, code: code, flags: flags, remainder: str };
};
function parseSubstitute(str) {
// takes a string like s/a/b/flags;othercmds and parses it
var from,to,flags,tmp;
if (str.length<4) return false;
var sep=str.charAt(1);
str=str.substring(2);
tmp=skipOver(str,sep);
if (tmp) { from=tmp.segment; str=tmp.remainder; }
else return false;
tmp=skipOver(str,sep);
if (tmp) { to=tmp.segment; str=tmp.remainder; }
else return false;
flags='';
if (str.length) {
tmp=skipOver(str,';') || skipToEnd(str, ';');
if (tmp) {flags=tmp.segment; str=tmp.remainder; }
}
return {action: substitute, from: from, to: to, flags: flags, remainder: str};
};
function skipOver(str,sep) {
var endSegment=findNext(str,sep);
if (endSegment<0) return false;
var segment=unEscape(str.substring(0,endSegment), sep);
return {segment: segment, remainder: str.substring(endSegment+1)};
}
function skipToEnd(str,sep) {
return {segment: str, remainder: ''};
}
function findNext(str, ch) {
for (var i=0; i<str.length; ++i) {
if (str.charAt(i)=='\\') i+=2;
if (str.charAt(i)==ch) return i;
}
return -1;
};
function runOnLoad(f) {
if (window.addEventListener) {
window.addEventListener("load",f,false);
}
else if (window.attachEvent) {
window.attachEvent("onload",f);
}
else {
window._old_popup_autoedit_onload = window.onload;
window.onload = function() {
window._old_popup_autoedit_onload();
f();
}
}
};
window.autoEdit=function() {
if (window.autoEdit.alreadyRan) return false;
window.autoEdit.alreadyRan=true;
var cmdString=getParamValue('autoedit');
if (cmdString) {
try {
var editbox=document.editform.wpTextbox1;
} catch (dang) { return; }
var cmdList=parseCmd(cmdString);
var input=editbox.value;
var output=execCmds(input, cmdList);
editbox.value=output;
}
var summary=getParamValue('autosummary');
if (summary) document.editform.wpSummary.value=summary;
var minor=getParamValue('autominor');
if (minor) {
switch (minor) {
case '1':
case 'yes':
case 'true':
document.editform.wpMinoredit.checked=true;
break;
case '0':
case 'no':
case 'false':
document.editform.wpMinoredit.checked=false;
}
}
var btn=getParamValue('autoclick');
if (btn) {
if (document.editform && document.editform[btn]) {
var headings=document.getElementsByTagName('h1');
if (headings) {
var div=document.createElement('div');
var button=document.editform[btn];
div.innerHTML='<font size=+1><b>"' + button.value +
'" 按钮已经被自动点击。' +
' 请等待载入下一个页面。</b></font>';
document.title='('+document.title+')';
headings[0].parentNode.insertBefore(div, headings[0]);
button.click();
}
} else {
alert('autoedit.js\n\nautoclick: 找不到按钮 "'+ btn +'".');
}
}
};
runOnLoad(autoEdit);
/*
|
|