leetcode助手

去除leetcode上烦人的复制小尾巴|自动开启差别比对(搬运自symant233作者的Beautify插件,对其功能进行了精简)。

目前为 2022-01-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         leetcode助手
// @namespace    com.zhaolei
// @require      https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
// @version      0.1
// @description  去除leetcode上烦人的复制小尾巴|自动开启差别比对(搬运自symant233作者的Beautify插件,对其功能进行了精简)。
// @author       zhaolei
// @match        *://leetcode-cn.com/*
// @icon         https://www.google.com/s2/favicons?domain=tampermonkey.net
// @license      GPL-3.0
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    if (!$) { var $ = window.jQuery; }
    // 监听复制事件
    function listeningCopyEvent() {
        self.addEventListener('copy', (e) => {
            // 阻止复制的默认事件
            e.preventDefault()
            const selection = document.getSelection()
            // 把文本设置到剪切板中
            e.clipboardData.setData('text', selection.toString())
        })
    }
    listeningCopyEvent()
    // 自动开启运行差别
    function enableDiff () {
        const btn = document.querySelector('label[class*="Label-StyledSwitch"]');
        if (btn && !btn.getAttribute('beautify-data')) {
            btn.setAttribute('beautify-data', true);
            btn.click();
        }
    }
    setTimeout(() => {
        $('div[class*=second-section-container] > div:last-child button').click();
        new Promise(resolve => {
            const container = document.querySelector('div[class*="CodeAreaContainer"]');
            if (container) {
                new MutationObserver((mutationList) => {
                    mutationList.forEach((mutation) => {
                        if (mutation.oldValue) enableDiff();
                    });
                }).observe(container, {
                    attributes: true,
                    attributeOldValue: true,
                    subtree: true,
                });
            }
            resolve();
        });
    }, 2600);
})();