如何在坦佩猴子获得状态503 [英] How to catch status 503 in tampermonkey

查看:295
本文介绍了如何在坦佩猴子获得状态503的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户脚本,它每秒刷新一次页面,但是有时它试图刷新的网站出现状态503错误,并停止了该脚本的运行。这意味着脚本将不再尝试每秒刷新页面。页面进入状态503错误后,如何保持脚本运行?该错误在控制台中如下所示:

I have a userscript that refreshes the page every second, but sometimes the website it's trying to refresh runs into the status 503 error, and that stops the script from running anymore. That means that script will no longer try refresh the page every second. How do I keep the script running after the page runs into the status 503 error? The error looks like this in the console:

无法加载资源:服务器以状态503(服务不可用)进行了响应

Failed to load resource: the server responded with a status of 503 (Service Unavailable)

// ==UserScript==
// @name        script
// @namespace   name
// @description example
// @match       *^https://example.com/$*
// @version     1
// @require     https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @grant       GM_xmlhttpRequest
// @run-at document-end
// ==/UserScript==

//*****************************************START OF SET_TIMEOUT
var timeOne = 1000;
var theTime = timeOne;

var timeout = setTimeout("location.reload(true);", theTime);
function resetTimeout() {
clearTimeout(timeout);
timeout = setTimeout("location.reload(true);", theTime);
} //end of function resetTimeout()
//*****************************************END OF SET_TIMEOUT


推荐答案

用户脚本在页面加载时运行,如果页面根本没有加载200以外的任何状态码,则用户脚本将不会运行。您可以使用< iframe> 如@Hemanth所建议,但您必须打破无限循环,因为< iframe> 还将加载用户脚本,依此类推。要打破它,只需检查是否在顶部窗口中加载了用户脚本。

The user-script run at page load, they won't run if page does not load at all for any status code other than 200. You can use <iframe> as @Hemanth suggested but you have to break the infinite loop as the <iframe> will also load the user-script and so on. To break it just check if the user-script is loaded on the top window.

if (window == window.top) {
    // Remove everything from the page
    // Add an iframe with current page URL as it's source
    // Add an event to reload the iframe few seconds after it is loaded
}

完整代码:

(function ($) {
  'use strict';
  var interval = 5000;
  if (window == window.top) {
    var body = $('body').empty();
    var myframe = $('<iframe>')
      .attr({ src: location.href })
      .css({ height: '95vh', width: '100%' })
      .appendTo(body)
      .on('load', function () {
        setTimeout(function () {
          myframe.attr({ src: location.href });
        }, interval);
      });
  }
})(jQuery);

这篇关于如何在坦佩猴子获得状态503的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆