leetcode 增加英文讨论区按钮

学算法要博众家之长 - 基于 @Aloxaf 大佬作品修改

目前为 2021-01-07 提交的版本。查看 最新版本

// ==UserScript==
// @name         leetcode 增加英文讨论区按钮
// @namespace    ljybill
// @version      1.0.0
// @description  学算法要博众家之长 - 基于 @Aloxaf 大佬作品修改
// @author       ljybill
// @match        https://leetcode-cn.com/problems/*
// @run-at       document-idle
// @grant window.onurlchange
// ==/UserScript==

/* jshint esversion: 6 */

(function () {
    'use strict';

    // https://stackoverflow.com/questions/22125865/wait-until-flag-true
    function waitFor(condition, callback) {
        if (!condition()) {
            window.setTimeout(waitFor.bind(null, condition, callback), 500); /* this checks the flag every 100 milliseconds*/
        } else {
            callback();
        }
    }

    function add_button() {
        // single
        if (document.querySelector('#btn__jump2eng')) {
            return
        }

        function htmlToElement(html) {
            let template = document.createElement('template');
            html = html.trim(); // Never return a text node of whitespace as the result
            template.innerHTML = html;
            return template.content.firstChild;
        }

        const problem_name = location.href.match(/problems\/([^\/]+)/)[1];
        const button = `<button id="btn__jump2eng" onclick="window.open('https://leetcode.com/problems/${problem_name}/discuss/?currentPage=1&orderBy=most_votes&query=','_blank')">
<svg viewBox="0 0 24 24" width="1em" height="1em">
<path fill-rule="evenodd" d="M8.995 22a.955.955 0 0 1-.704-.282.955.955 0 0 1-.282-.704V18.01H3.972c-.564 0-1.033-.195-1.409-.586A1.99 1.99 0 0 1 2 15.99V3.97c0-.563.188-1.032.563-1.408C2.94 2.188 3.408 2 3.972 2h16.056c.564 0 1.033.188 1.409.563.375.376.563.845.563 1.409V15.99a1.99 1.99 0 0 1-.563 1.432c-.376.39-.845.586-1.409.586h-6.103l-3.709 3.71c-.22.187-.454.281-.704.281h-.517zm.986-6.01v3.1l3.099-3.1h6.948V3.973H3.972V15.99h6.01zm-3.99-9.013h12.018v2.018H5.991V6.977zm0 4.037h9.014v1.972H5.99v-1.972z"></path>
</svg>
<span>英文版讨论区</span>
</button>`;
        document.querySelector('h4[class*="-Title"]').append(htmlToElement(button));
    }

    waitFor(() => {
        let node = document.querySelector('div[class*="-Tools"]');
        return (node && node.childElementCount > 0);
    }, add_button);

    // for spa
    if (window.onurlchange === null) {
        // feature is supported
        window.addEventListener('urlchange', function (evt) {
            console.log('evt');
            if (evt.url) {
                waitFor(() => {
                    let node = document.querySelector('div[class*="-Tools"]');
                    return (node && node.childElementCount > 0);
                }, add_button);
            }
        });
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址