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

XHR 劫持并修改post中某个数据内容的求助

[复制链接]
  • TA的每日心情
    擦汗
    2023-4-18 13:55
  • 签到天数: 34 天

    [LV.5]常住居民I

    10

    主题

    36

    回帖

    63

    积分

    初级工程师

    积分
    63
    发表于 2022-10-8 09:37:08 | 显示全部楼层 | 阅读模式
    悬赏10油猫币已解决

    1、已知网站引用的Ajax的js文件及post内容的函数。
    2、已查看了XHR发送内容【postData": { "mimeType": "application/json;charset=UTF-8",
    "text": "{\"historyId\":63641374,\"position\":240,\"len\":\"5400\",\"cid\":\"61\"}"】
    3、如何在脚本中劫持XMLHttpRequest,并修改JS中某个参数,从而达到修改postData中position\":240,\"的数值。比如把240修改为2400或者直接等于len的值。
    4、需要帮我把脚本代码写出来,或者推荐一个简单实用的抓包、能修改XHR中post信息并再次发送的软件。

    下附Ajax请求JS文件:
    (function (window, $) {
    var timer1;
    var timer2;
    window.readAssist = function (id,source,postion,percent,len,root,timespace) {
    var reloginpage = root + "/page/common-tokenerror";
    var assist = new Object();
    assist.hid = 0;
    assist.url = root + "/resource/saveTssView";
    assist.source = source;
    assist.postion = postion;
    assist.percent = percent;
    assist.len = len;
    assist.time = parseInt(new Date().getTime() / 1000);
    if (timespace && !isNaN(timespace)) {
    assist.timespace = timespace 1000;
    } else {
    assist.timespace = 60000;
    }
    var result = new Object();
    result.cid = id;
    result.source = assist.source;
    result.ttion = assist.postion;
    result.percent = assist.percent;
    assist.first = function () {
    if (assist.hid == 0) {
    $.ajax({
    url: assist.url,
    data: JSON.stringify(result),
    type: "post",
    dataType: "json",
    timeout: 20000,
    contentType: "application/json;charset=utf-8",
    error: function (message) {
    window.wxc.xcConfirm(
    "网络繁忙,请稍后刷新页面重试!",
    "studyWarning"
    );
    },
    success: function (data) {
    if (data) {
    if (data.status == 0) {
    assist.hid = data.id;
    if (assist.timespace != 0) {
    timer1 = window.setInterval(function () {
    assist.progressing();
    }, assist.timespace);
    timer2 = window.setTimeout(function () {
    assist.progressing();
    clearInterval(timer1);
    }, parseInt(len)
    1000);
    }
    } else if (data.status == 1) {
    window.wxc.xcConfirm(
    "获得初始进度失败,请刷新页面重试!",
    "studyWarning"
    );
    }
    }
    }
    });
    }
    };
    assist.progressing = function () {
    var now = parseInt(new Date().getTime() / 1000);
    if (assist.postion == "") {
    assist.postion = now - assist.time;
    } else {
    assist.postion = parseInt(assist.postion) + (now - assist.time);
    }
    assist.time = now;
    var result = new Object();
    result.historyId = assist.hid;
    result.position = assist.postion;
    result.len = assist.len;
    result.cid = id;
    $.ajax({
    url: assist.url,
    data: JSON.stringify(result),
    type: "post",
    dataType: "json",
    timeout: 20000,
    contentType: "application/json;charset=utf-8",
    error: function (message) {
    window.wxc.xcConfirm("网络繁忙,请稍后刷新页面重试!", "error");
    },
    success: function (data) {
    if (data) {
    if (data.status == 0) {
    } else if (data.status == 1) {
    window.wxc.xcConfirm(
    "进度记录失败,请刷新页面重试!",
    "studyWarning"
    );
    } else if (data.status == 2) {
    window.clearTimeout(timer1);
    window.clearTimeout(timer2);
    window.wxc.xcConfirm(
    "不能同时学习多门课程,点击确认关闭窗口。",
    "readCourse",
    {
    onOk: function (v) {
    window.close();
    }
    }
    );
    }
    }
    }
    });
    };
    assist.history = function () {
    if (assist.hid == 0) {
    assist.first();
    } else {
    assist.progressing();
    }
    };
    assist.history();
    return assist;
    };
    })(window, $);

    最佳答案

    查看完整内容

    https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
  • TA的每日心情
    慵懒
    2022-3-8 11:41
  • 签到天数: 2 天

    [LV.1]初来乍到

    22

    主题

    857

    回帖

    1356

    积分

    荣誉开发者

    积分
    1356

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

    发表于 2022-10-8 09:37:09 | 显示全部楼层
    本帖最后由 cxxjackie 于 2022-10-8 21:10 编辑

    https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
    1. // ==UserScript==
    2. // @name         xhr劫持
    3. // @description  ...
    4. // @namespace    ...
    5. // @author       ...
    6. // @version      1.0
    7. // @match        你要匹配的网站
    8. // @require      https://scriptcat.org/lib/637/1.0.1/ajaxHooker.js
    9. // @run-at       document-start
    10. // ==/UserScript==

    11. (function() {
    12.     'use strict';
    13.     ajaxHooker.hook(request => {
    14.         if (request.url.includes('/resource/saveTssView') && request.method === 'POST') {
    15.             request.data = val => {
    16.                 const postData = JSON.parse(val);
    17.                 postData.position = 2400;
    18.                 return JSON.stringify(postData);
    19.             };
    20.         }
    21.     });
    22. })();
    复制代码
    回复

    使用道具 举报

    发表回复

    本版积分规则

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