LeetCode CN to LeetCode COM Redirector with Abort Option

Redirect from leetcode.cn to leetcode.com with a loading animation and an option to abort.

  1. // ==UserScript==
  2. // @name LeetCode CN to LeetCode COM Redirector with Abort Option
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-01-12#3
  5. // @description Redirect from leetcode.cn to leetcode.com with a loading animation and an option to abort.
  6. // @author Yuyang Wang
  7. // @match *://leetcode.cn/*
  8. // @match *://leetcode.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.com
  10. // @grant GM_addStyle
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // Check if the current URL is from leetcode.cn
  18. if (window.location.host === 'leetcode.cn') {
  19. // Create a container for the loader
  20. // Create a container for the loader
  21. var loaderContainer = document.createElement("div");
  22. loaderContainer.style.position = "fixed";
  23. loaderContainer.style.top = "0";
  24. loaderContainer.style.left = "0";
  25. loaderContainer.style.width = "100%";
  26. loaderContainer.style.height = "100%";
  27. loaderContainer.style.display = "flex";
  28. loaderContainer.style.justifyContent = "center";
  29. loaderContainer.style.alignItems = "center";
  30. loaderContainer.style.backgroundColor = "rgba(0, 0, 0, 0.5)"; // 50% transparent background
  31. loaderContainer.innerHTML = '<div class="loader"></div>';
  32.  
  33. // Create and style the abort button
  34. var abortButton = document.createElement("button");
  35. abortButton.innerHTML = "Abort Redirect";
  36. abortButton.style.position = "fixed";
  37. abortButton.style.top = "20px";
  38. abortButton.style.right = "20px";
  39. abortButton.style.zIndex = "1001";
  40.  
  41. // Append the loader and the abort button to the body
  42. document.body.appendChild(loaderContainer);
  43. document.body.appendChild(abortButton);
  44.  
  45. GM_addStyle(`
  46. .loader {
  47. border: 16px solid #f3f3f3;
  48. border-top: 16px solid #3498db;
  49. border-radius: 50%;
  50. width: 120px;
  51. height: 120px;
  52. animation: spin 1s linear infinite;
  53. }
  54. @keyframes spin {
  55. 0% { transform: rotate(0deg); }
  56. 100% { transform: rotate(360deg); }
  57. }
  58. `);
  59.  
  60. // Create the new URL by replacing 'leetcode.cn' with 'leetcode.com'
  61. var newUrl = window.location.href.replace('leetcode.cn', 'leetcode.com');
  62.  
  63. // Redirect to the new URL after a short delay to show the animation
  64. var redirectTimeout = setTimeout(function() {
  65. window.location.href = newUrl;
  66. }, 1000); // Adjust the time as needed
  67.  
  68. // Function to abort the redirect
  69. abortButton.addEventListener('click', function() {
  70. loaderContainer.style.display = 'none';
  71. abortButton.style.display = 'none';
  72. clearTimeout(redirectTimeout);
  73. });
  74. }
  75. })();