标签 js 下的文章


// 监听 URL 变化
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
let lastUrl = window.location.href;

function handleUrlChange() {
  if (lastUrl !== window.location.href) {
    console.log("URL 发生变化:", window.location.href);
    window.location.reload();
  }
}

// 重写 pushState 和 replaceState 以监听 URL 改变
history.pushState = function (...args) {
  originalPushState.apply(history, args);
  handleUrlChange();
};

history.replaceState = function (...args) {
  originalReplaceState.apply(history, args);
  handleUrlChange();
};

// 监听浏览器的 popstate 事件(即用户通过浏览器的后退/前进按钮改变 URL)
window.addEventListener("popstate", handleUrlChange);

const inputElement = document.getElementById('declarePrice-0');

// 创建一个新的事件对象并赋值新的值
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
nativeInputValueSetter.call(inputElement, '100.00'); // 设定新的值

// 创建并触发 'input' 事件
const inputEvent = new Event('input', { bubbles: true });
inputElement.dispatchEvent(inputEvent); // 触发 React 的 onChange

(function () {
    const originalSend = XMLHttpRequest.prototype.send;
    const originalOpen = XMLHttpRequest.prototype.open;

    XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
        this._url = url;   // 保存请求 URL 以便后续使用
        this._method = method;
        this._async = async;
        originalOpen.apply(this, arguments);  // 保持原来的 open 方法
    };

    XMLHttpRequest.prototype.send = function (body) {
        const xhr = this;  // 保存当前 XMLHttpRequest 对象的引用

        console.log('拦截到 XHR 请求的响应:', xhr.responseText);
        // 拦截 onreadystatechange,获取响应数据
        xhr.addEventListener('readystatechange', function () {
            if (xhr.readyState === 4) {  // readyState 4 表示请求已完成
                console.log('拦截到 XHR 请求的响应:', xhr.responseText);

                // 在这里你可以对响应数据进行处理
                // alert(xhr.responseText)
                //拦截指定的url
                if (xhr._url.includes('keyword')) {
                    console.log(JSON.parse(xhr.responseText).data)
                }
            }
        });

        // 调用原始 send 方法
        originalSend.apply(this, arguments);
    };
})();

 function sleep(ms:number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

 async function simulateHumanInput(target, value, interval = 80) {
  target.focus();
  target.innerText = "";

  for (let i = 0; i < value.length; i++) {
    const char = value[i];
    target.innerText += char;

    // 生成输入事件
    const inputEvent = new InputEvent("input", {
      bubbles: true,
      data: char,
      inputType: "insertText"
    });
    target.dispatchEvent(inputEvent);

    // 键盘事件
    const keyEventDown = new KeyboardEvent("keydown", { key: char, bubbles: true });
    const keyEventUp = new KeyboardEvent("keyup", { key: char, bubbles: true });
    target.dispatchEvent(keyEventDown);
    target.dispatchEvent(keyEventUp);

    await sleep(interval + Math.random() * 30);
  }

  target.blur();
}