上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

油猴脚本劫持截获ajax数据json数据并将修改后的内容返回

[复制链接]

该用户从未签到

16

主题

56

回帖

90

积分

初级工程师

积分
90

新人报道

发表于 2022-12-31 13:49:45 | 显示全部楼层 | 阅读模式
悬赏1油猫币已解决

本帖最后由 Major 于 2022-12-31 13:53 编辑

本帖最后由 Major 于 2022-12-31 13:52 编辑

我想截获一个 ajax json数据,修改内容后再将其返回。
搬运了一段代码,貌似可以截获。但是浏览器会一直停留在加载状态,无法将截获的数据再返回。
能否帮助修改一下,或者提供一个完整试例 能够套用或者参考的。感谢!

// ==UserScript==
// @name         劫持并修改Json数据,讲修改后的结果返回
// @namespace    修改json数据并返回
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @match        http*://*/show?id*
// @run-at       document-start
// ==/UserScript==

function hookJson(){
    const xhrOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        if (arguments[1] == '/api/show') {
            console.log('成功识别到了目标网址',arguments[1])
            const xhr = this;
            let response = 'responseText'   //  responseText  response
            const getter = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, response).get;
            Object.defineProperty(xhr, response, {
                get: () => {
                var result = getter.call(xhr);
                console.log(result)
                //这里可以修改result
                result = '{"data":"我是修改后的数据"}';
                console.log(result)
                return result;
                }
            });
        }else{
            console.log('未识别到目标网址!',arguments[1])
        };
        return xhrOpen.apply(xhr, arguments);
    };
};

(function() {
    hookJson();
})();

0012.png

最佳答案

查看完整内容

[md]this指向错误。const是块级作用域,`const xhr = this`这句写在了if语句块内,导致if结束后xhr的定义就没了,最后那句实际上变成了: ``` return xhrOpen.apply(undefined, arguments); ``` 后续请求因此丢失了xhr引用,这是报错的原因。解决方法:将`const xhr = this;`提升到if之上即可。[/md]
  • TA的每日心情
    慵懒
    2022-3-8 11:41
  • 签到天数: 2 天

    [LV.1]初来乍到

    22

    主题

    862

    回帖

    1360

    积分

    荣誉开发者

    积分
    1360

    荣誉开发者卓越贡献油中2周年生态建设者油中3周年挑战者 lv2

    发表于 2022-12-31 13:49:46 | 显示全部楼层

    this指向错误。const是块级作用域,const xhr = this这句写在了if语句块内,导致if结束后xhr的定义就没了,最后那句实际上变成了:

    return xhrOpen.apply(undefined, arguments);

    后续请求因此丢失了xhr引用,这是报错的原因。解决方法:将const xhr = this;提升到if之上即可。

    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    636

    主题

    5185

    回帖

    6068

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6068

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2022-12-31 14:22:25 | 显示全部楼层
    会不会是因为 result = '{"data":"我是修改后的数据"}';返回的是字符串格式导致的
    原来的可能格式跟这个不一样?
    这个要具体调试的
    可以追到错误点看看什么原因导致的运行错误
    如果是xhr劫持代码导致的可以反馈一下
    后续再修补
    也可以尝试一下c大的库
    https://bbs.tampermonkey.net.cn/ ... &highlight=ajax

    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-14 00:00
  • 签到天数: 119 天

    [LV.6]常住居民II

    29

    主题

    598

    回帖

    535

    积分

    专家

    积分
    535

    油中2周年生态建设者油中3周年挑战者 lv2

    发表于 2022-12-31 18:56:08 | 显示全部楼层
    1.         obj.tidyPageFileList = function () {
    2.             (function(open) {
    3.                 XMLHttpRequest.prototype.open = function() {
    4.                     if (!this._hooked) {
    5.                         this._hooked = true;
    6.                         setupHook(this);
    7.                     }
    8.                     open.apply(this, arguments);
    9.                 }
    10.             })(XMLHttpRequest.prototype.open);

    11.             function setupHook(xhr) {
    12.                 var f = ["aac", "ac3", "amr", "flac", "m4a", "mp2", "mp3", "ogg", "ra", "wav", "wma"]
    13.                 , m = ["3gp", "avi", "flv", "mkv", "mov", "mp4", "mpg", "rmvb", "webm", "wmv", "rm", "mpeg"];
    14.                 (function setup() {
    15.                     Object.defineProperty(xhr, "response", {
    16.                         get: function () {
    17.                             delete xhr.response;
    18.                             var responseURL = xhr.responseURL, response = xhr.response;
    19.                             if (responseURL.includes("/file/list") || responseURL.includes("/file/search")) {
    20.                                 response && response.items && response.items.forEach(function (item) {
    21.                                     if (item.category == "video") {
    22.                                         if (!m.includes(item.file_extension)) {
    23.                                             item.file_extension = "mp4";
    24.                                         }
    25.                                     }
    26.                                     else if (item.category == "audio") {
    27.                                         if (!f.includes(item.file_extension)) {
    28.                                             item.file_extension = "mp3";
    29.                                         }
    30.                                     }

    31.                                     if (item.punish_flag) {
    32.                                         item.punish_flag = 0;
    33.                                     }
    34.                                 });
    35.                             }
    36.                             else if (responseURL.includes("/file/get")) {
    37.                                 if (response && response.category == "video") {
    38.                                     if (!m.includes(response.file_extension)) {
    39.                                         response.file_extension = "mp4";
    40.                                     }
    41.                                 }
    42.                             }
    43.                             setup();
    44.                             return response;
    45.                         },
    46.                         configurable: true
    47.                     });
    48.                 })();
    49.             }
    50.         };
    复制代码

    这是一段验证过可以正常使用的代码,但推荐也可以尝试一下c大的库
    https://bbs.tampermonkey.net.cn/ ... &highlight=ajax
    入驻爱发电 让这世界充满爱 https://afdian.net/a/vpannice
    回复

    使用道具 举报

    该用户从未签到

    16

    主题

    56

    回帖

    90

    积分

    初级工程师

    积分
    90

    新人报道

    发表于 2023-1-1 11:27:31 | 显示全部楼层
    cxxjackie 发表于 2022-12-31 20:08
    [md]this指向错误。const是块级作用域,`const xhr = this`这句写在了if语句块内,导致if结束后xhr的定义就 ...

    你这个非常可以,顺利执行了,感谢!!!!
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表