Greasy Fork

Greasy Fork is available in English.

力扣中文站重定向到 LeetCode 国际站

如果当前链接在 LeetCode 国际站可访问,则重定向。如果当前链接在 LeetCode 国际站不可访问(比如力扣中文站独家题库),则不做处理。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name 力扣中文站重定向到 LeetCode 国际站
// @namespace http://greasyfork.icu/zh-CN/users/459661-cycychenyi
// @version 0.1.1
// @description 如果当前链接在 LeetCode 国际站可访问,则重定向。如果当前链接在 LeetCode 国际站不可访问(比如力扣中文站独家题库),则不做处理。
// @author cycychenyi
// @match https://leetcode.cn/*
// @match https://leetcode-cn.com/*
// @grant GM_xmlhttpRequest
// @connect leetcode.com
// ==/UserScript==

async function redirectIfPossible() {
    const redirectUrl = getRedirectUrl('leetcode.com');
    if (await isValidUrl(redirectUrl)) {
        redirect(redirectUrl);
    }
}

function getRedirectUrl(redirectHostname) {
    const url = new URL(location.href);
    url.hostname = redirectHostname;
    return url;
}

async function isValidUrl(url) {
    const response = await visit(url);
    return response.status == 200;
}

function visit(url) {
    return new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: response => resolve(response)
        });
    });
}

function redirect(url) {
    location.assign(url);
}

redirectIfPossible();