98 lines
2.7 KiB
JavaScript
98 lines
2.7 KiB
JavaScript
// ==UserScript==
|
|
// @name 423down Article Proxy
|
|
// @namespace https://www.nite07.com/
|
|
// @version 1.0.2
|
|
// @description 在 423down 文章页自动获取正文 HTML 并替换页面正文区域。
|
|
// @author nite07
|
|
// @homepageURL https://www.nite07.com/
|
|
// @match https://www.423down.com/*.html
|
|
// @connect 423.nite07.com
|
|
// @connect *.nite07.com
|
|
// @grant GM_xmlhttpRequest
|
|
// @run-at document-end
|
|
// ==/UserScript==
|
|
|
|
(function () {
|
|
"use strict";
|
|
|
|
const API_ORIGIN = "https://423.nite07.com";
|
|
const ARTICLE_PATH_RE = /^\/(\d+)\.html$/;
|
|
|
|
const articleID = getArticleID(window.location.pathname);
|
|
if (!articleID) {
|
|
return;
|
|
}
|
|
|
|
replaceArticleHTML(articleID);
|
|
|
|
function getArticleID(pathname) {
|
|
const match = pathname.match(ARTICLE_PATH_RE);
|
|
return match ? match[1] : "";
|
|
}
|
|
|
|
function replaceArticleHTML(articleID) {
|
|
requestArticleHTML(articleID)
|
|
.then((html) => {
|
|
const articleContainer = document.querySelector("div.entry");
|
|
if (!articleContainer) {
|
|
console.warn("[423down-proxy] 未找到正文容器 div.entry,无法替换 HTML");
|
|
return;
|
|
}
|
|
|
|
articleContainer.innerHTML = html;
|
|
console.info(`[423down-proxy] 已替换文章 ${articleID} 的正文 HTML`);
|
|
})
|
|
.catch((error) => {
|
|
console.error("[423down-proxy] 获取文章 HTML 失败:", error);
|
|
});
|
|
}
|
|
|
|
function requestArticleHTML(articleID) {
|
|
const url = `${API_ORIGIN}/?article_id=${encodeURIComponent(articleID)}`;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
GM_xmlhttpRequest({
|
|
method: "GET",
|
|
url,
|
|
responseType: "json",
|
|
timeout: 30_000,
|
|
onload(response) {
|
|
if (response.status < 200 || response.status >= 300) {
|
|
reject(new Error(`HTTP ${response.status}`));
|
|
return;
|
|
}
|
|
|
|
const data = parseResponse(response);
|
|
if (!data || data.ok !== true || typeof data.html !== "string") {
|
|
reject(
|
|
new Error(data && data.error ? data.error : "接口响应格式不正确"),
|
|
);
|
|
return;
|
|
}
|
|
|
|
resolve(data.html);
|
|
},
|
|
onerror() {
|
|
reject(new Error("网络请求失败"));
|
|
},
|
|
ontimeout() {
|
|
reject(new Error("网络请求超时"));
|
|
},
|
|
});
|
|
});
|
|
}
|
|
|
|
function parseResponse(response) {
|
|
if (response.response && typeof response.response === "object") {
|
|
return response.response;
|
|
}
|
|
|
|
try {
|
|
return JSON.parse(response.responseText);
|
|
} catch (error) {
|
|
console.error("[423down-proxy] JSON 解析失败:", error);
|
|
return null;
|
|
}
|
|
}
|
|
})();
|