注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
function editThis(id){
$('#'+id).fadeOut(100)
var data = $('.data')
var dataLength = data.length
var singleData = $(data[id])
var singleDataHTML = singleData.html()
console.log(singleData)
var jsonData = $.parseJSON(singleDataHTML)
console.log(jsonData)
var o = eval('newInlineEditor('+id+','+JSON.stringify(jsonData)+').'+jsonData.type+'()')
render(id,jsonData.type,o)
console.log('加载题目:'+(id+1)+'/'+dataLength)
}
function confirmEdit(id) {
var data = formToJSON('#edit-'+id, true,'s')
console.log(data)
var formatedData = formatEditText(data)
var template = JSONtoTemplate(formatedData)
console.log(template)
}
function processUploadText(pageTitle,replaceText) {
var latestPageData = getLatestPageRevision('https://www.medforest.cn/medf/api.php',pageTitle)
}
function formatEditText(json) {
var result = {}
var choices = {}
for(var i in json){
var data = formToJSON(i,true,'t')
if((result.id&&result.type) !== undefined){
result.id = data.id
result.type = data.type
}
if(data.key==='choices'){
choices[data.value] = json[i]
}else {
result[data.key] = json[i].replace(/\\/g,'\\'+'\\').replace(/"/g,'\\'+'"')
}
console.log(data)
}
JSON.stringify(choices).match(/(?<=\{)([^}]*)(?=\})/g)
result.choices = RegExp.$1
console.log(result)
return result
}
function formToJSON(selector,decode,mode) {
var result = {}
var data = ''
if(mode === 's'){
data = $(selector).serialize()
}else if(mode === 't'){
data = selector
}
console.log(data)
var dataSplitted = data.split('&')
for (var i=0;i<dataSplitted.length;i++){
var value = dataSplitted[i].split('=')[1]
var key = dataSplitted[i].split('=')[0]
if(decode===true){
key = decodeURIComponent(key)
value = decodeURIComponent(value)
}
result[key] = value
}
return result
}
function JSONtoTemplate(json, templateName) {
var order = ['source','inputbox','sourceRange','title','choices','answer','explain','info']
// var result = '{{'+templateName +'\n'
var result = ''
for(var i=0;i<order.length;i++){
if (json[order[i]]!==undefined){
result += '|' + order[i] + '=' + json[order[i]].replace('\n') + '\n'
}
}
// result += '}}\n\n'
return result
}
function submitAction(){}
function newInlineEditor(id,json,hideButton,addHr,parent=''){
parent = parent !== undefined ? parent : ''
addHr = addHr !== undefined ? addHr : true
hideButton = hideButton !== undefined ? hideButton : false
var type = json.type
var o = new Object()
o.temp = {
main:$('<form id="edit-'+id+'" class="type'+type+' timuEditContainer"></form>'),
subject:$('<div class="subject"></div>'),
range:$('<div class="sourceRange"></div>'),
info:$('<textarea name="id='+id+'&type='+type+'&key=info" class="info oo-ui-inputWidget-input"></textarea>'),
titleCon:$('<div class="titleContainer"></div>'),
source:$('<p class="source"></p>'),
title:$('<textarea name="id='+id+'&type='+type+'&key=title" class="title-edit oo-ui-inputWidget-input" ></textarea>'),
options:$('<div class="options"></div>'),
btn:$('<button onclick="submitAction();return false;" class="button-small button-pill button button-caution" id="showSingle-' + id + '" class="showSingleBtn">确认更改</button>'),
ansCon:$('<div id="ansContainer" class="ansContainer"></div>'),
correct:$('<p style="display:inline;"><b>正确答案:</b></p><textarea name="id='+id+'&type='+type+'&key=answer" class="correctAnswer oo-ui-inputWidget-input"></textarea><br>'),
explain:$('<p style="display:inline;"><b>解析: </b></p><textarea name="id='+id+'&type='+type+'&key=explain" class="explain oo-ui-inputWidget-input"></textarea><br>'),
select:$('<select class="button-small button-rounded button"></select>'),
subTitle:$('<div id=""></div>')
}
o.hideButton = hideButton
o.parent = parent
if(type!=='A3'&&type!=='PD'){
o.temp.btn.bind("click",function (){
confirmEdit(id)
})
}
o.A = function () {
this.temp.source.text(json.source)
this.temp.title.html(json.title)
this.temp.correct.nextAll('.correctAnswer').val(json.answer)
this.temp.explain.nextAll('.explain').val(json.explain)
var options = formatAnsDic(json.choices)
for(var k in options) {
var label = '<label id="' + k + '">'+k+':<input class="oo-ui-inputWidget-input" name="id='+id+'&type='+type+'&key=choices&value='+ k +'" class="options-edit" type="text" value="' + options[k] + '" /></label>'
this.temp.options.append(label)
}
this.temp.titleCon.append(this.temp.source,this.temp.title)
this.temp.ansCon.append(this.temp.correct,this.temp.explain)
this.temp.subject.append(this.temp.titleCon,this.temp.options)
this.temp.main.append(this.temp.subject,this.temp.ansCon)
if(!hideButton){this.temp.main.append(this.temp.btn)}
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
o.A3 = function () {
var subIDs = []
var s = json.source*1;var e = json.source*1+json.sourceRange*1 -1
this.temp.range.text('第 '+s+' 到 '+e+' 题')
for(var i=0;i<json.title.length;i++){
subIDs.push(id+'-'+i)
if(json.explain[i]==undefined){
json.explain[i] = '暂无解析'
}
var data = {
"type":"A",
"source":json.source+'-'+(i+1),
"title":json.title[i],
"answer":json.answer[i],
"explain":json.explain[i],
"choices":json.choices[i],
}
var newA = newTimu(id+'-'+i,data,true,false).A()
render(id+'-'+i,'A',newA,this.temp.titleCon)
}
o.temp.btn.bind("click",function (){
showChoiceAnswer(subIDs,type,json.answer)
})
this.temp.info.text(json.info)
this.temp.subject.append(this.temp.range,this.temp.info,this.temp.titleCon)
if(!hideButton){this.temp.main.append(this.temp.btn)}
this.temp.main.append(this.temp.subject)
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
o.B = function () {
var s = json.source*1;var e = json.source*1+json.sourceRange*1 -1
this.temp.range.text('第 '+s+' 到 '+e+' 题')
var options = formatAnsDic(json.choices)
for(var k in options){
var label = '<li id="'+k+'">'+k+':<input name="singelChoice-' + id +'-'+ k +'" class="options-edit" type="text" value="' + options[k] + '" /></li>'
this.temp.options.append(label)
this.temp.select.append('<option value="'+k+'">'+k+'</option>')
}
for(var i=0;i<json.title.length;i++){
var reg1 = /\(\)/g
var text = '<textarea name="title-'+id+'-'+i+'" class="title-edit oo-ui-inputWidget-input" >'+json.title[i]+'</textarea>'
this.temp.subTitle.attr('id',id+'-'+i)
this.temp.source.text(s+i)
this.temp.subTitle.html(this.temp.source.prop("outerHTML")+text)
this.temp.titleCon.append(this.temp.subTitle.prop("outerHTML"))
}
for(var i=0;i<json.answer.length;i++){
this.temp.correct.nextAll('.correctAnswer').append('<p id="ans-'+id +'-'+i+'">'+(id+i)+':'+json.answer[i]+'</p>')
}
for(var i=0;i<json.explain.length;i++){
this.temp.explain.nextAll('.explain').append('<p id="exp-'+id +'-'+i+'">'+(id+i)+':'+json.explain[i]+'</p>')
}
this.temp.subject.append(this.temp.range,this.temp.options,this.temp.titleCon)
this.temp.ansCon.append(this.temp.correct,this.temp.explain)
this.temp.main.append(this.temp.subject,this.temp.ansCon)
if(!hideButton){this.temp.main.append(this.temp.btn)}
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
o.C = function () {
var s = json.source*1;var e = json.source*1+json.sourceRange*1 -1
this.temp.range.text('第 '+s+' 到 '+e+' 题')
var options = formatAnsDic(json.choices)
for(var k in options){
var label = '<li id="'+k+'">'+k+':<input name="singelChoice-' + id +'-'+ k +'" class="options-edit" type="text" value="' + options[k] + '" /></li>'
this.temp.options.append(label)
this.temp.select.append('<option value="'+k+'">'+k+'</option>')
}
for(var i=0;i<json.title.length;i++){
var reg1 = /\(\)/g
var text = '<textarea name="title-'+id+'-'+i+'" class="title-edit oo-ui-inputWidget-input" >'+json.title[i]+'</textarea>'
this.temp.subTitle.attr('id',id+'-'+i)
this.temp.source.text(s+i)
this.temp.subTitle.html(this.temp.source.prop("outerHTML")+text)
this.temp.titleCon.append(this.temp.subTitle.prop("outerHTML"))
}
for(var i=0;i<json.answer.length;i++){
this.temp.correct.nextAll('.correctAnswer').append('<p id="ans-'+id +'-'+i+'">'+(id+i)+':'+json.answer[i]+'</p>')
}
for(var i=0;i<json.explain.length;i++){
this.temp.explain.nextAll('.explain').append('<p id="exp-'+id +'-'+i+'">'+(id+i)+':'+json.explain[i]+'</p>')
}
this.temp.subject.append(this.temp.range,this.temp.titleCon,this.temp.options)
this.temp.ansCon.append(this.temp.correct,this.temp.explain)
this.temp.main.append(this.temp.subject,this.temp.ansCon)
if(!hideButton){this.temp.main.append(this.temp.btn)}
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
o.X = function () {
this.temp.source.text(json.source)
this.temp.title.text(json.title)
this.temp.correct.nextAll('.correctAnswer').text(json.answer)
this.temp.explain.nextAll('.correctAnswer').text(json.explain)
var options = formatAnsDic(json.choices)
for(var k in options){
var label = '<label id="'+k+'">'+k+':<input name="singelChoice-' + id +'-'+ k +'" type="text" class="options-edit" value="' + options[k] + '" /></label>'
this.temp.options.append(label)
}
this.temp.titleCon.append(this.temp.source,this.temp.title)
this.temp.ansCon.append(this.temp.correct,this.temp.explain)
this.temp.subject.append(this.temp.titleCon,this.temp.options)
this.temp.main.append(this.temp.subject,this.temp.ansCon)
if(!hideButton){this.temp.main.append(this.temp.btn)}
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
o.PD = function () {
this.temp.source.text(json.source)
this.temp.title.text(json.title)
this.temp.explain.nextAll('.correctAnswer').text(json.explain)
var ans = ''
if(json.answer=='T' || json.answer=='正确' || json.answer=='1' || json.answer=='√'){
ans = '正确'
}else if(json.answer=='F' || json.answer=='错误' || json.answer=='0' || json.answer=='×'){
ans = '错误'
}
this.temp.correct.nextAll('.correctAnswer').text(ans)
o.temp.btn.bind("click",function (){
showChoiceAnswer(id,'A',ans)
})
this.temp.options.append('<label id="正确" style="display: inline;"><input name="singelChoice-'+id+'" type="radio" value="正确" />正确</label>' +
'<label id="错误" style="display: inline;"><input name="singelChoice-'+id+'" type="radio" value="错误" />错误</label>')
this.temp.titleCon.append(this.temp.source,this.temp.title)
this.temp.ansCon.append(this.temp.correct,this.temp.explain)
this.temp.subject.append(this.temp.titleCon,this.temp.options)
if(!hideButton){this.temp.main.append(this.temp.btn)}
this.temp.main.append(this.temp.subject,this.temp.ansCon)
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
o.TK = function () {
this.temp.source.text(json.source)
var form = $('<form id="form-'+id+'" class="title"></form>')
var pos = json.pos
var posReg = new RegExp(pos,'g')
var posCount = json.title.match(posReg).length
for(var i=0;i<posCount;i++){
json.title = json.title.replace(pos,'<input name="'+id+'-'+i+'" id="'+id+'-'+i+'" type="text" form="form-'+id+'"/>')
}
form.html(json.title)
this.temp.title.text(json.title)
this.temp.correct.nextAll('.correctAnswer').text(json.answer)
this.temp.explain.nextAll('.correctAnswer').text(json.explain)
this.temp.titleCon.append(this.temp.source,form)
this.temp.ansCon.append(this.temp.correct,this.temp.explain)
this.temp.subject.append(this.temp.titleCon,this.temp.options)
if(!hideButton){this.temp.main.append(this.temp.btn)}
this.temp.main.append(this.temp.subject,this.temp.ansCon)
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
o.MJ = function () {
this.temp.source.text(json.source)
this.temp.title.html(json.title + ':<span class="blur" tabindex="0" style="display:inline;outline=0;" onclick="">' + json.answer +'</span>')
this.temp.titleCon.append(this.temp.source,this.temp.title)
this.temp.subject.append(this.temp.titleCon,this.temp.options)
this.temp.main.append(this.temp.subject)
if(addHr){this.temp.main.append('<hr>')}
return this.temp.main
}
return o
}
//获取某个页面的历史版本
function getPageRevision(url, title, pageCount=1){
var PARAMS = {
"action": "query",
"format": "json",
"prop": "revisions",
"titles": title,
"utf8": 1,
"rvprop": "content",
"rvlimit": pageCount,
"rvdir": "older",
'origin':origin,
}
var isSuccess = {}
$.ajax(
{
url: url,
method: "post",
async: false,
data: PARAMS,
timeout: 5000,
success: function (results){
if('-1' in results.query.pages){
console.log('===无此页面===')
isSuccess = {isSuccess:false, result:results}
}else {
isSuccess = {isSuccess:true, result:results}
}
}
})
return isSuccess
}
function getLatestPageRevision(url, title) {
var pages = getPageRevision(url,title,1)
if (pages.isSuccess) {
pages = pages.result.query.pages
for (var key in pages) {
var page = pages[key].revisions[0]['*']
}
return page
}else {
return false
}
}