Greasy Fork

Greasy Fork is available in English.

记录错误的xhr请求

记录页面中诸如401、403、500之类的错误的ajax请求,并将结果显示在右上角,点击每条链接可以copy具体的错误信息。

当前为 2020-05-03 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         记录错误的xhr请求
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  记录页面中诸如401、403、500之类的错误的ajax请求,并将结果显示在右上角,点击每条链接可以copy具体的错误信息。
// @author       You
// @match        *://*/*
// @grant        GM_addStyle
// @run-at      document-start
// ==/UserScript==

(function(open) {
    'use strict';
    var panel = document.createElement('div')
    panel.classList.add('reqPanel')
    var list = document.createElement('ul')
    panel.appendChild(list)
    GM_addStyle('.reqPanel { position:fixed;z-index:999;top:0;right:0;padding:10px;background-color:bisque;max-height:30%;overflow:auto;} .reqPanel ul{list-style:none;margin:0;padding:0} .reqPanel ul li{margin-bottom:10px;} .reqPanel ul li:last-child{margin-bottom:0;}');
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener("readystatechange", function() {
            if(this.readyState === 4){
              if([401,403,500].indexOf(this.status)!==-1){
                  console.log(Date.now())
                  renderReq(this)
              }
            }
        });
        open.apply(this, arguments);
    }
    function renderReq(req){
       var node = document.querySelector('.reqPanel')
       if(!node) document.body.appendChild(panel)
       var li = document.createElement('li')
       li.style.cssText +='width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;'
       var link = document.createElement('a')
       link.href = 'javascript:;'
       link.textContent = req.responseURL
       link.title = req.responseURL
       link.addEventListener('click',function(e){
          e.preventDefault()
          copyToClipboard(JSON.stringify(req.response))
           alert('copy success')
       })
       li.appendChild(link)
       list.appendChild(li)
    }
    function copyToClipboard(str) {
        var el = document.createElement('textarea');
        el.value = str;
        document.body.appendChild(el);
        el.select();
        document.execCommand('copy');
        document.body.removeChild(el);
    };
})(XMLHttpRequest.prototype.open);