有没有人一起从零开始刷力扣

// 脚本使用方法请参考 https://www.tampermonkey.net/faq.php 或 https://greasyfork.org/zh-CN (这行代码不要复制)
// 这俩都是国外网站, 上不去的话直接百度 "油猴脚本使用方法" 就行(这行代码也不要复制)

// ==UserScript==
// @name         有没有人一起从零开始刷力扣
// @namespace    likou-replace
// @version      1.0
// @description  none
// @author       Permission
// @match        https://leetcode-cn.com/circle/article/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(),
    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);
    }
    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;
            for (let id of problem.textContent.split('、')) {
                if (isNaN(parseInt(id))) {
                    normalExit = false;
                    break;
                }
                htmlString += `<a href = 'https://leetcode.cn/problems/${proMap.get(id)}/' title = '${transMap.get(id)}' target = '_blank'>${id}</a>、`;
            }
            if (normalExit) {
                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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值