清理API响应中的PHP警告

去除API响应中的PHP警告信息,保留有效JSON数据

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         清理API响应中的PHP警告
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  去除API响应中的PHP警告信息,保留有效JSON数据
// @author       mmnnwjw
// @license MIT
// @match        *://www.linovelib.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 保存原始XMLHttpRequest对象
    const originalXhr = window.XMLHttpRequest;

    // 重写XMLHttpRequest
    window.XMLHttpRequest = function() {
        const xhr = new originalXhr();
        const originalOpen = xhr.open;
        let isTargetAPI = false;

        // 重写open方法以检测目标API
        xhr.open = function(method, url) {
            if (url.includes('api.php?action=get_list')) {
                isTargetAPI = true;
            }
            return originalOpen.apply(xhr, arguments);
        };

        // 重写responseText getter
        const originalResponseText = Object.getOwnPropertyDescriptor(originalXhr.prototype, 'responseText');
        Object.defineProperty(xhr, 'responseText', {
            get: function() {
                let text = originalResponseText.get.call(this);
                if (isTargetAPI) {
                    // 清理PHP警告(查找第一个{作为JSON起始)
                    const jsonStart = text.indexOf('{');
                    if (jsonStart > 0) {
                        text = text.substring(jsonStart);
                    }
                }
                return text;
            }
        });

        return xhr;
    };
})();