Greasy Fork is available in English.
如果当前链接在 LeetCode 国际站可访问,则重定向。如果当前链接在 LeetCode 国际站不可访问(比如力扣中文站独家题库),则不做处理。
// ==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();