// ==UserScript==
// @name 51talk选择最好最合适的老师-经验|好评率|年龄|收藏数
// @version 1.0.12
// @namespace https://github.com/niubilityfrontend
// @description 辅助选老师-排序显示,经验值计算|好评率|显示年龄|列表显示所有教师
// @author jimbo
// @license OSL-3.0
// @supportURL https://github.com/niubilityfrontend/hunttingteacheron51talk
// @match *://www.51talk.com/ReserveNew/index*
// @match *://www.51talk.com/TeacherNew/*
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_listValues
// @grant GM_deleteValue
// @grant GM_registerMenuCommand
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @require https://code.jquery.com/ui/1.12.1/jquery-ui.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.5/i18n/grid.locale-cn.js
// @require https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.5/jquery.jqgrid.min.js
// @require https://greasyfork.org/scripts/388372-scrollfix/code/scrollfix.js?version=726657
// @require https://greasyfork.org/scripts/389774-gm-config-toolbar/code/gm_config_toolbar.js?version=730739
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
Pace.Options = {
ajax: false, // disabled
document: false, // disabled
eventLag: false, // disabled
elements: {
selectors: ['#filterdialog']
}
};
$("head").append(
''
);
$("head").append(
''
);
$("head").append('');
const config = GM_config([{
key: 'pagecount',
label: '自动获取页数',
default: 10,
type: 'dropdown',
values: [0, 5, 10, 20, 50]
},
{
key: 'version',
type: 'hidden',
default: 1
}
]);
let conf = config.load();
config.onsave = cfg => {
conf = cfg;
$('#auotonextpage').text('自动获取' + conf.pagecount + "页");
};
GM_registerMenuCommand('配置', config.setup);
function sleep(delay) {
var start = (new Date()).getTime();
while ((new Date()).getTime() - start < delay) {
continue;
}
}
//删除数组中的空元素
Array.prototype.clean = function(deleteValue = "") {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};
Number.prototype.toString = function() {
return this.toFixed(2);
};
String.prototype.toFloat = function() {
return parseFloat(this);
};
String.prototype.toInt = function() {
return parseInt(this);
};
String.prototype.startsWith = function(str) {
return this.slice(0, str.length) == str;
};
String.prototype.endsWith = function(str) {
return this.slice(-str.length) == str;
};
String.prototype.contains = function(str) {
return this.indexOf(str) > -1;
};
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
$(".item-top-cont").prop('innerHTML', function(i, val) {
return val.replaceAll('', '');
});
var asc = function(a, b) {
var av = $(a).attr('indicator');
var bv = $(b).attr('indicator');
if (!av || !bv) return 0;
return $(a).attr('indicator').toFloat() > $(b).attr('indicator').toFloat() ? 1 : -1;
};
var desc = function(a, b) {
var av = $(a).attr('indicator');
var bv = $(b).attr('indicator');
if (!av || !bv) return 0;
return $(a).attr('indicator').toFloat() > $(b).attr('indicator').toFloat() ? -1 : 1;
};
var sortByIndicator = function(sortBy) {
var sortEle = $('.s-t-content.f-cb .item').sort(sortBy);
$('.s-t-content.f-cb').empty().append(sortEle);
};
/**
* 提交运算函数到 document 的 fx 队列
*/
var submit = function(fun) {
var queue = $.queue(document, "fx", fun);
if (queue[0] == 'inprogress') {
return;
}
$.dequeue(document);
};
let maxrate = 0,
minrate = 99999,
maxlabel = 0,
minlabel = 9999999,
maxfc = 0,
minfc = 999999,
maxage = 0,
minage = 99999;
function updateTeacherinfoToUI(jqel, tinfo) {
if (tinfo.label > maxlabel) maxlabel = tinfo.label;
if (tinfo.label < minlabel) minlabel = tinfo.label;
if (tinfo.favoritesCount > maxfc) maxfc = tinfo.favoritesCount;
if (tinfo.favoritesCount < minfc) minfc = tinfo.favoritesCount;
if (tinfo.thumbupRate > maxrate) maxrate = tinfo.thumbupRate;
if (tinfo.thumbupRate < minrate) minrate = tinfo.thumbupRate;
if (tinfo.age > maxage) maxage = tinfo.age;
if (tinfo.age < minage) minage = tinfo.age;
jqel.attr("teacherinfo", JSON.stringify(tinfo));
jqel.find(".teacher-name")
.html(jqel.find(".teacher-name").text() + "
[" + tinfo.label + "|" + tinfo.thumbupRate + "%|" + tinfo.favoritesCount +
"]");
jqel.find(".teacher-age")
.html(jqel.find(".teacher-age").text() + " | ");
jqel //.attr('thumbup', tinfo.thumbup)
//.attr('thumbdown', tinfo.thumbdown)
//.attr('thumbupRate', tinfo.thumbupRate)
//.attr('age', tinfo.age)
//.attr('label', tinfo.label)
.attr('indicator', tinfo.indicator);
}
function executeFilters(uifilters) {
let tcount = 0,
hidecount = 0;
$.each($('.item'), function(i, item) {
var node = $(item);
var tinfojson = node.attr("teacherinfo");
if (!tinfojson) {
return true;
}
var tinfo = JSON.parse(tinfojson);
if ((tinfo.thumbupRate >= uifilters.rate1 && tinfo.thumbupRate <= uifilters.rate2) &&
tinfo.label >= uifilters.l1 && tinfo.label <= uifilters.l2 &&
tinfo.age >= uifilters.age1 && tinfo.age <= uifilters.age2 &&
tinfo.favoritesCount >= uifilters.fc1 && tinfo.favoritesCount <= uifilters.fc2) {
if (node.is(':hidden')) { //如果node是隐藏的则显示node元素,否则隐藏
node.show();
node.animate({
left: "+=50"
}, 3500).animate({
left: "-=50"
}, 3500);
} else {
//nothing todo
}
tcount++;
} else {
node.css('color', 'white').hide();
hidecount++;
}
});
$('#tcount').text(tcount);
$('#thidecount').text(hidecount);
}
let configExprMilliseconds = 1000 * 60 * 60 * GM_getValue('tinfoexprhours', 24 * 3); //缓存7天小时
let num = /[0-9]*/g;
function getUiFilters() {
var l1 = $("#tlabelslider").slider('values', 0);
var l2 = $("#tlabelslider").slider('values', 1);
var rate1 = $("#thumbupRateslider").slider('values', 0);
var rate2 = $("#thumbupRateslider").slider('values', 1);
var age1 = $("#tAgeSlider").slider('values', 0);
var age2 = $("#tAgeSlider").slider('values', 1);
var fc1 = $("#fcSlider").slider('values', 0);
var fc2 = $("#fcSlider").slider('values', 1);
return {
l1,
l2,
rate1,
rate2,
age1,
age2,
fc1,
fc2
};
}
function gettid() {
//https://www.51talk.com/TeacherNew/info/t26501111
//https://www.51talk.com/TeacherNew/teacherComment?tid=t26501111&type=all&has_msg=1
//https://www.51talk.com/TeacherNew/teacherComment?tid=t26501111&type=good&has_msg=1
//https://www.51talk.com/TeacherNew/teacherComment?tid=t26501111&type=bad&has_msg=1
return window.location.href.match(/(t\d+)/g);
}
function getinfokey() {
return 'tinfo-' + gettid();
}
$(".item").each(function(index, el) {
submit(function(next) {
Pace.track(function() {
let jqel = $(el);
let tid = jqel.find(".teacher-details-link a").attr('href').replace(
"https://www.51talk.com/TeacherNew/info/", "").replace('http://www.51talk.com/TeacherNew/info/', '');
var tinfokey = 'tinfo-' + tid;
var tinfo = GM_getValue(tinfokey);
if (tinfo) {
if (!tinfo.expire) {
tinfo.expire = new Date(1970, 1, 1).getTime();
}
if (new Date().getTime() - tinfo.expire < configExprMilliseconds) {
updateTeacherinfoToUI(jqel, tinfo);
next();
return true;
}
}
// ajax 请求一定要包含在一个函数中
var start = (new Date()).getTime();
$.ajax({
url: window.location.protocol + '//www.51talk.com/TeacherNew/teacherComment?tid=' + tid +
'&type=bad&has_msg=1',
type: 'GET',
dateType: 'html',
success: function(r) {
var jqr = $(r);
if (jqr.find('.teacher-name-tit').length > 0) {
var tempitem = jqr.find('.teacher-name-tit')[0];
tempitem.innerHTML = tempitem.innerHTML.replace('', '');
}
if (jqr.find(".evaluate-content-left span").length >= 3) {
var thumbup = Number(jqr.find(".evaluate-content-left span:eq(1)").text().match(num).clean("")[0]);
var thumbdown = Number(jqr.find(".evaluate-content-left span:eq(2)").text().match(num).clean("")[0]);
var thumbupRate = ((thumbup + 0.00001) / (thumbdown + thumbup)).toFixed(2) * 100;
var favoritesCount = Number(jqr.find(".clear-search").text().match(num).clean("")[0]);
var age = Number(jqel.find(".teacher-age").text().match(num).clean("")[0]);
var label = (function() {
let j_len = jqel.find(".label").text().match(num).clean("").length;
let l = 0;
for (let j = 0; j < j_len; j++) {
l += Number(jqel.find(".label").text().match(num).clean("")[j]);
}
l = Math.ceil(l / 5);
return l;
})();
var isfavorite = jqr.find(".go-search.cancel-collection").length > 0;
var name = jqel.find(".teacher-name").text();
var type = $('.s-t-top-list .li-active').text();
var tage = Number(jqr.find(".teacher-name-tit > .age.age-line").text().match(num).clean("")[0]);
var slevel = jqr.find('.sui-students').text();
var tinfo = {
'slevel': slevel,
'tage': tage,
'thumbup': thumbup,
'thumbdown': thumbdown,
'thumbupRate': thumbupRate,
'age': age,
'label': label,
'indicator': Math.ceil(label * thumbupRate / 100) + favoritesCount,
'favoritesCount': favoritesCount,
'name': name,
'isfavorite': isfavorite,
'expire': new Date().getTime()
};
if (type != '收藏外教') {
tinfo.type = type;
} else {
//tinfo.type=type
tinfo.isfavorite = true;
}
GM_setValue(tinfokey, tinfo);
updateTeacherinfoToUI(jqel, tinfo);
} else {
console.log('Teacher s detail info getting error:' + JSON.stringify(jqel) + ",error info:" + r);
}
},
error: function(data) {
console.log("xhr error when getting teacher " + JSON.stringify(jqel) + ",error msg:" + JSON.stringify(
data));
}
}).always(function() {
while ((new Date()).getTime() - start < 600) {
continue;
}
next();
});
});
});
});
submit(function(next) {
var autonextpage = GM_getValue('autonextpage', 0);
if (autonextpage > 0) {
GM_setValue('autonextpage', autonextpage - 1);
if ($('.s-t-page .next-page').length == 0) {
GM_setValue('autonextpage', 0);
} else {
$('.s-t-page .next-page')[0].click();
return false;
}
}
try {
var config = GM_getValue('filterconfig', {
l1: 300,
l2: maxlabel,
rate1: 97,
rate2: maxrate,
age1: minage,
age2: maxage
});
$('body').append("