// ==UserScript==
// @name 有没有人一起从零开始刷力扣 可以显示AC过的
// @namespace likou-replace
// @version 1.0
// @description none
// @author Permission
// @match https://leetcode.cn/circle/article/48kq9d/*
// @match https://leetcode.cn/circle/discuss/48kq9d/*
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @grant none
// ==/UserScript==
/* globals $, jQuery */
'use strict';
let proMap = new Map(),
transMap = new Map(),
statuMap=new Map(),
buildMapComplete = false;
const getProblems = () => {
$.get("https://leetcode.cn/api/problems/all/").then((response) => {
getTrans(JSON.parse(response));
});
}
const getTrans = (picker) => {
$.ajax({
method: "POST",
url: 'https://leetcode.cn/graphql/',
headers: {
"content-type": "application/json",
"x-definition-name": "getQuestionTranslation",
"x-operation-name": "getQuestionTranslation",
"x-csrftoken": getCookie("csrftoken"),
"x-timezone": Intl.DateTimeFormat().resolvedOptions().timeZone
},
data: JSON.stringify({
"operationName": "getQuestionTranslation",
"variables": {},
"query": "query getQuestionTranslation($lang: String) {translations: allAppliedQuestionTranslations(lang: $lang) {title questionId __typename}}"
})
}).then((trans) => {
buildMap(picker, trans);
});
}
const buildMap = (picker, trans) => {
for (let pro of picker.stat_status_pairs) {
proMap.set(pro.stat.frontend_question_id, pro.stat.question__title_slug);
statuMap.set(pro.stat.frontend_question_id,pro.status);
}
for (let t of trans.data.translations) {
transMap.set(t.questionId, t.title);
}
buildMapComplete = true;
};
const getCookie = (name) => {
const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
const arr = document.cookie.match(reg);
if (arr) {
return unescape(arr[2]);
} else {
return null;
}
};
const replace = () => {
let even = true;
for (let problem of $("table tr td")) {
if (!even) {
let htmlString = "";
let normalExit = true;
let tag="";
for (let id of problem.textContent.split('、')) {
if (isNaN(parseInt(id))) {
normalExit = false;
break;
}
tag=id;
htmlString += `<a href = 'https://leetcode.cn/problems/${proMap.get(id)}/' title = '${transMap.get(id)}' target = '_blank' style=${statuMap.get(tag)=="ac"?"\"color:red\"":"\"color:blue\""}>${id}</a>、`;
}
if (normalExit) {
console.log(`<td ${statuMap.get(tag)=="ac"?"bgcolor=\"red\"":"bgcolor=\"blue\""}>${htmlString.substring(0, htmlString.length - 1)}</td>`);
problem.innerHTML = `<td>${htmlString.substring(0, htmlString.length - 1)}</td>`;
}
}
even = !even;
}
}
getProblems();
const interval = setInterval(() => {
if (buildMapComplete && $("table tr td").length !== 0) {
clearInterval(interval);
replace();
}
}, 5e2);