LeetCode 根据题目ID跳转

这里写图片描述

// ==UserScript==
// @name         LeetCode
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  LeetCode
// @author       N3verL4nd
// @match        *://leetcode.com/problemset/all/
// @match        *://leetcode-cn.com/problemset/all/
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    //setTimeout(add,2000);

    // Firefox和Chrome早期版本中带有前缀
    var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
    // 选择目标节点
    var target = document.querySelector('#base_content');
    // 创建观察者对象
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            for (var i = 0; i < mutation.addedNodes.length; i++) {
                var node = mutation.addedNodes.item(i);
                //console.log(id);
                if(node.id == 'welcome') {
                    console.log('=============================================================');
                    add();
                }
            }
        });
    });
    // 配置观察选项:
    var config = { attributes: true, childList: true, characterData: true, subtree: true };
    // 传入目标节点和观察选项
    observer.observe(target, config);
    // 随后,你还可以停止观察
    //observer.disconnect();

    function add() {
        $('.filter-tag-bar').after('<input type="text" id="searchID" class="form-control list-search-bar" placeholder="编号"/>');
        addEvent();
    }

    function addEvent() {
        var searchID = document.getElementById('searchID');
        searchID.onkeypress = function(event){
            if(event.keyCode == "13") {
                SearchByID(searchID.value);
            }
        };
    }


    function SearchByID(id) {
        var flag = false;
        $.ajax({
            type: "get",
            url: "/api/problems/all/",
            async: false,
            success: function (res) {
                if (res === "") {
                    return false;
                }
                var lists = eval("(" + res + ")").stat_status_pairs;
                for (var i in lists) {
                    var stat = lists[i].stat;
                if (stat.question_id == id) {
                    flag = true;
                    console.log(window.location.origin + "/problems/" + lists[i].stat.question__title_slug);
                    window.open(window.location.origin + "/problems/" + lists[i].stat.question__title_slug, "_parent");
                    break;
                }
            }
            }
        });
        return flag;
    }
})();

导入 tampermonkey 即可使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N3verL4nd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值