// ==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 即可使用。