// ==UserScript==
// @name unipus iTEST助手
// @namespace http://blog.z31.xyz/
// @version 2.0
// @description 自动翻译文章题目材料, 解析听力材料无限听, 内置翻译助手英汉互译, 解除切屏限制, 解除右键菜单与划词限制
// @author simonkimi
// @match https://itestcloud.unipus.cn/itest-api/itest/s/answer/**
// @grant none
// @downloadURL none
// ==/UserScript==
(async () => {
'use strict';
document.write("");
await delay(2000);
initCss();
debug();
disableSelect();
singleSelect();
select10from15();
article();
articleChoice();
listenTest();
setInterval(function () {
if (window.onblur !== null) {
window.onblur = null;
}
}, 5 * 5000)
/**
* 是否为本地网页, 解除本地网页限制
*/
function debug() {
if (window.location.href.indexOf("itestcloud.unipus.cn") === -1) {
$('.goup').removeClass('dis').on('click', function () {
$('.itest-ques-set').each(function () {
const $this = $(this);
if ($this.css('display') === 'block' && $this.parent().css('display') === 'block') {
if ($this.prev().is('.itest-ques-set')) {
$this.prev().css('display', 'block')
$this.css('display', 'none')
} else if ($this.parent().prev().is('.itest-section')) {
$this.parent().prev().css('display', 'block')
$this.parent().css('display', 'none')
}
return false;
}
})
})
$('.goto').removeClass('dis').on('click', function () {
$('.itest-ques-set').each(function () {
const $this = $(this);
if ($this.css('display') === 'block' && $this.parent().css('display') === 'block') {
if ($this.next().is('.itest-ques-set')) {
$this.next().css('display', 'block')
$this.css('display', 'none')
} else if ($this.parent().next().is('.itest-section')) {
$this.parent().next().css('display', 'block')
$this.parent().css('display', 'none')
}
return false;
}
})
})
}
}
async function translateAjaxApi(obj) {
return new Promise((resolve, reject) => {
$.ajax({
type: "post",
async: false,
url: "https://api.fanyi.baidu.com/api/trans/vip/translate",
dataType: "jsonp",
data: {...obj},
success: function (data) {
resolve(data)
},
error: function () {
reject();
}
});
})
}
/**
* 调用翻译api
* @param context_list
* @param from
* @param to
* @returns {Promise}
*/
async function translateAPI(context_list, from, to) {
const trans_salt = (new Date).getTime();
const {appid, key} = getBaiduAPIKey()
const trans_str = appid + context_list + trans_salt + key;
const trans_sign = $.md5(trans_str);
for (let i=0; i<5; i++) {
const data = await translateAjaxApi({
q: context_list,
from,
to,
appid: appid,
salt: trans_salt,
sign: trans_sign
})
if (data.error_code) {
const errmsg = {
"52001": "请求超时",
"52002": "系统错误",
"52003": "未授权用户",
"54003": "访问频率受限",
"54004": "账户余额不足",
"58002": "服务当前已关闭",
"90107": "认证未通过或未生效"
}
if (parseInt(data.error_code) !== 54003) {
alert(`错误代码${data.error_code} 信息:${errmsg[data.error_code.toString()]}`)
throw new Error(`错误代码${data.error_code} 信息:${errmsg[data.error_code.toString()]}`)
} else {
await delay(1000);
}
}
return data.trans_result;
}
alert("请求过于频繁")
throw new Error("请求过于频繁");
}
function getBaiduAPIKey() {
return {appid: "20200603000484959", key: "Fz6UttgcMXATCddMMZW1"}
}
function disableSelect() {
function hackClass(className) {
for (const i of document.getElementsByClassName(className)) {
hackItem(i);
}
}
function hackItem(item) {
item.onpaste = () => true;
item.oncontextmenu = () => true;
item.onselectstart = () => true;
item.ondragstart = () => true;
item.oncopy = () => true;
item.onbeforecopy = () => true;
item.style = '';
}
hackClass('itest-ques');
hackClass('itest-direction');
hackItem(document.body);
hackItem(document);
}
async function delay(time) {
return new Promise(resolve => {
setTimeout(resolve, time)
})
}
/**
* 单选题注入
*/
function singleSelect() {
$('.row').each(function () {
if ($(this).find('.option').length !== 0) {
$(this).prepend(``)
}
})
$('.sk-tr-s').on('click', function () {
if ($(this).is('.sk-btn-p')) {
$(this).parent().find('span').each(function () {
$(this).remove();
})
const $this = $(this).parent();
const selectOption = [];
$this.children('.option').each(function () {
selectOption.push($(this).find('label'))
})
const p = selectOption.map(value => value.text().replace('\n', '')).join('\n');
translateAPI(p, 'en', 'zh').then(value => {
for (let i = 0; i < value.length; i++) {
selectOption[i].append(`
${value[i].dst}`)
}
$(this).removeClass('sk-btn-p').addClass('sk-btn-g').html('清空');
})
} else {
$(this).removeClass('sk-btn-g').addClass('sk-btn-p').html('翻译');
$(this).parent().find('span').each(function () {
$(this).remove();
})
}
})
}
/**
* 15选10注入
*/
function select10from15() {
$('.itest-15xuan10>.xxcontent').each(function () {
$(this).prepend(``)
})
$('.sk-tr-15p').on('click', function () {
// 文章翻译
$(this).parent().find('span').each(function () {
$(this).remove();
})
const $button = $(this);
if ($(this).is('.sk-btn-p')) {
const $passage = $(this).next();
const passage = $passage.text().replace(/\s+/g, ' ');
translateAPI(passage, 'en', 'zh').then(value => {
value.forEach(i => {
$passage.append(`
${i.dst}`)
})
$button.removeClass('sk-btn-p').addClass('sk-btn-g').html('清空');
})
} else {
$(this).removeClass('sk-btn-g').addClass('sk-btn-p').html('翻译');
$(this).parent().find('span').each(function () {
$(this).remove();
})
}
})
$('.itest-15xuan10>.xx').each(function () {
$(this).prepend(``)
})
$('.sk-tr-15w').on('click', function () {
// 单词翻译
$(this).parent().find('span').each(function () {
$(this).remove();
})
const $button = $(this);
if ($(this).is('.sk-btn-p')) {
const $words = $(this).parent().find('a')
const wordList = [];
$words.each(function () {
wordList.push($(this));
})
const data = wordList.map(value => value.text().replace(/\s+/g, ' ')).join('\n');
translateAPI(data, 'en', 'zh').then(value => {
for (let i = 0; i < value.length; i++) {
wordList[i].append(`
${value[i].dst}`)
}
$button.removeClass('sk-btn-p').addClass('sk-btn-g').html('清空');
})
} else {
$(this).removeClass('sk-btn-g').addClass('sk-btn-p').html('翻译');
$(this).parent().find('span').each(function () {
$(this).remove();
})
}
})
}
function article() {
$('.con-left>.article').each(function () {
$(this).prepend(``)
})
$('.sk-tr-art').on('click', function () {
const $button = $(this);
if ($(this).is('.sk-btn-p')) {
$(this).parent().find('span').each(function () {
$(this).remove();
})
const $article = $(this).parent().find("div[style='text-align:justify']");
$article.children('br:even').remove();
$article.append('
')
const $brs = [];
$article.children('br').each(function () {
$brs.push($(this));
})
const text = $article.html()
.split('
')
.map(value => value.trim().replace(/\s+/g, ' ').replace(' ', ''))
.filter(value => value.length !== 0);
(async () => {
for (let i=0; i
${data[0].dst}`)
await delay(1000);
}
$button.removeClass('sk-btn-p').addClass('sk-btn-g').html('清空');
})()
} else {
$(this).removeClass('sk-btn-g').addClass('sk-btn-p').html('翻译');
$(this).parent().find('span').each(function () {
$(this).remove();
})
}
})
}
function articleChoice() {
$('.itest-need-layout').each(function () {
if ($(this).find('span').length !== 0) {
$(this).prepend(``)
}
})
$('.sk-tr-art-r').on('click', function () {
if ($(this).is('.sk-btn-p')) {
const $button = $(this);
const $spanList = []
$button.parent().find('span').each(function () {
$spanList.push($(this))
})
const text = $spanList.map(value => value.text())
.map(value => value.trim().replace(/\s+/g, ' ').replace(' ', ''))
.join('\n');
translateAPI(text, 'en', 'zh').then(value => {
for (let i=0; i
${value[i].dst}`)
}
$button.removeClass('sk-btn-p').addClass('sk-btn-g').html('清空');
})
} else {
$(this).removeClass('sk-btn-g').addClass('sk-btn-p').html('翻译');
$(this).parent().find('label').each(function () {
$(this).remove();
})
}
})
}
function listenTest() {
$('.itest-hear-reslist-duration+.itest-ques').each(function () {
const $this = $(this);
const $urlNode = $this.parent().find('.itest-hear-reslist');
if ($urlNode.length !== 0) {
const mp3List = JSON.parse($urlNode.html());
mp3List.reverse();
for (const url of mp3List) {
if (url.indexOf('http') !== -1) {
$this.children('.itest-danxuan').prepend(`