LeetCode Contest: Open at leetcode.cn

Add an "Open at LeetCode.cn" button on the LeetCode Contest page.

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 Contest: Open at leetcode.cn
// @namespace   JohnZhu04
// @match       https://leetcode.com/contest/*/problems/*
// @grant       none
// @version     1.0
// @author      JohnZhu04
// @license     MIT
// @supportURL  https://github.com/JohnZhu04/LeetScript/issues
// @icon        https://www.google.com/s2/favicons?sz=64&domain=leetcode.com
// @description Add an "Open at LeetCode.cn" button on the LeetCode Contest page.
// ==/UserScript==

const openCNURL = () => {
  const url = window.location.href.replace("leetcode.com", "leetcode.cn");
  window.open(url);
};

const main = () => {
  const whichDiv = ".question-title";
  const buttonClass = "btn btn-default panel-hover";
  const buttonText = "Open at LeetCode.cn";
  if (!document.querySelector(whichDiv)) {
    window.setTimeout(main, 2000);
  }
  const button = document.createElement("button");
  button.innerText = buttonText;
  button.addEventListener("click", openCNURL);
  button.className = buttonClass;
  document.querySelector(whichDiv).appendChild(button);
};

main();