利用js实现网页自动答题

zcz 1060
1.整理题库
      先将题库整理为“题目$答案”的形式并存放在list中,便于后期查找,用$分隔开是为了分隔答案。

2.模糊匹配

      之前几次查找答案是用dict储存题库,靠键值查找的。优点是快,缺点也很明显,稍有不匹配哪怕是中英文符号的差别就查找不到。所以这次采用模糊匹配的方式,每个题目都到题库中检索一遍并算出匹配度,以匹配度最高的为准。代码参考于https://bbs.csdn.net/topics/392581392?list=lz
function levenshtein(str1,str2) {
    var len1 = str1.length;
    var len2 = str2.length;
    var arr = [];
    for (var y = 0; y <= len1; y++)
        arr[y] = [y];
    for (var x = 1; x <= len2; x++)
        arr[0][x] = x;
    for (var y = 1; y <= len1; y++)
        for (var x = 1; x <= len2; x++)
            arr[y][x] = Math.min(
                arr[y-1][x]+1,
                arr[y][x-1]+1,
                arr[y-1][x-1]+(str1[y-1]==str2[x-1]?0:1)
            );
    return 1 - arr[len1][len2] / Math.max(len1,len2);
} 

3.找出匹配的题目
function finder(n){
	var matcher=0;
	var k=0;
	for (var j=0;j<tiku.length;j++) {
		temp=levenshtein(document.getElementById('div'+String(n)).children[0].textContent,tiku[j])
		if (temp>matcher) {
			matcher=temp;
			k=j;
		};
	};
	answer=tiku[k].substring(tiku[k].indexOf('$') + 1, tiku[k].length);	//取出用'$'分隔的答案
	return answer;
}

4.根据答案选出选项
function choose(answer,n){
	if (answer.indexOf("A")!=-1)
		document.getElementById('q'+n+'_1').checked=true;
	if (answer.indexOf("B")!=-1)
		document.getElementById('q'+n+'_2').checked=true;
	if (answer.indexOf("C")!=-1)
		document.getElementById('q'+n+'_3').checked=true;
	if (answer.indexOf("D")!=-1)
		document.getElementById('q'+n+'_4').checked=true;
	if (answer.indexOf("E")!=-1)
		document.getElementById('q'+n+'_5').checked=true;
	if (answer.indexOf("F")!=-1)
		document.getElementById('q'+n+'_6').checked=true;
	if (answer.indexOf("正确")!=-1)
		document.getElementById('q'+n+'_1').checked=true;
	if (answer.indexOf("错误")!=-1)
		document.getElementById('q'+n+'_2').checked=true;	
} 
5.封装成插件
可以用chrome或360极速浏览器进行封装,只需要写一个json文件放在与js同目录。
{
	"name": "Angui Exam",
	"version": "1.0",
	"manifest_version": 2,
	"description": "Powered by zcz",
	"browser_action": {
		"default_title": "Angui Exam"
	},
	"content_scripts": [{
		"matches": [ "https://ks.wjx.top/*"],
		"js": [ "dati_wjx.js" ],
		"run_at": "document_end"
	}]
}

6.实现效果
ezgif.com-video-to-gif.gif