Leetcode relative line number

Make relative line number available on leetcode

Pada tanggal 26 September 2021. Lihat %(latest_version_link).

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Leetcode relative line number
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Make relative line number available on leetcode
// @author       You
// @match        https://leetcode.com/problems/*
// @icon         https://www.google.com/s2/favicons?domain=leetcode.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    function setRelativeLineNumber() {
        let activeLineHTML = document.querySelector("div.CodeMirror-activeline > div > div").innerHTML;
        let activeLineNumber = 0;
        if (activeLineHTML != null && activeLineHTML !== undefined) {
            let lineNumber = activeLineHTML.match("[0-9]+");
            if (lineNumber == null) { return; }
            activeLineNumber = parseInt(lineNumber[0]);
        }
        let allLines = document.querySelectorAll("div.CodeMirror-linenumber");
        allLines.forEach(line => {
            if (!line.hasAttribute("natural-line")) {
                line.setAttribute("natural-line", parseInt(line.innerHTML));
            }
            line.innerHTML = Math.abs(line.getAttribute("natural-line") - activeLineNumber).toString();
            if (line.innerHTML == "0") {
                line.innerHTML = " ";
            }
        });
    };
    setInterval(setRelativeLineNumber, 300);
    document.addEventListener('click', setRelativeLineNumber);

})();