简体   繁体   English

即使站点离线,如何保持用户脚本正常工作?

[英]How to keep my userscript working, even when the site goes offline?

I want to make an userscript (right now I am only worried about Chrome compatibility) reload the page every 30 seconds (I mean, that's not the point, I just want to somehow execute some script while the internet is offline), in case the internet goes down and the user is not here to see it. 我想制作一个用户脚本(现在我只担心Chrome的兼容性)每30秒重新加载一次页面(我的意思是,这不是重点,我只是想以某种方式在互联网离线时执行一些脚本),以防互联网中断,用户不在这里查看。

Now, the problem is, as soon as the internet goes down the page will be redirected to "This webpage is not available" and all userscripts simply stop working. 现在的问题是,一旦互联网中断,该页面将被重定向到“此网页不可用”,并且所有用户脚本都将停止工作。

I already tried to add @include * to the metadata, hoping that this would allow the userscript to work on said page, however I had no success. 我已经尝试将@include *添加到元数据,希望这将允许用户脚本在所述页面上工作,但是我没有成功。

Chrome's "This webpage is not available" display is not a standard page with a "Greaseable" scheme. Chrome的“此网页不可用”显示不是带有“可润滑”方案的标准页面。 This means that Tampermonkey scripts and Chrome userscripts will not fire for it. 这意味着Tampermonkey脚本和Chrome用户脚本不会为此触发。 You would have to make a full-fledged Chrome extension . 您必须制作一个功能完善的Chrome扩展程序 (More details at this question .) (有关此问题的更多详细信息。)

There are some workarounds: 有一些解决方法:

(1) You could switch your DNS provider to one like OpenDNS . (1)您可以将DNS提供商切换为类似OpenDNS的 提供商 OpenDNS hijacks DNS resolution errors and serves up a spammy "search" page instead. OpenDNS劫持了DNS解析错误,并提供了一个垃圾邮件的“搜索”页面。 (The page is guide.opendns.com , IIRC.) (页面为guide.opendns.com ,即IIRC。)

That search page is greaseable by Tampermonkey. 该搜索页面可以用Tampermonkey润滑。 Your script could search for OpenDNS' "unavailable" messages and know that the site was offline. 您的脚本可以搜索OpenDNS的“不可用”消息,并且知道该站点处于脱机状态。

(2) Use AJAX to poll the page headers and only reload when the page is both up and online. (2) 使用AJAX轮询页面标题,仅在页面同时处于联机状态时才重新加载。

Use GM_xmlhttpRequest() (or jQuery) to make a head request . 使用GM_xmlhttpRequest() (或jQuery)发出head请求 If the request errors-out, then the site is offline, don't reload the page. 如果请求出错,则表明该站点处于离线状态,请不要重新加载页面。

Likewise, if the Last-Modified header is the same as when you last checked, then there is no point in reloading the page (usually). 同样,如果Last-Modified标头与上次检查时的标头相同,则重新加载页面(通常)没有任何意义。

As mentioned in the comments: 如评论中所述:

Wrap the page in an <iframe> and execute the userscript in the parent. 将页面包装在<iframe>并在父级中执行用户脚本。 Once the connection goes down, Chrome should continue to execute the Userscript in the parent because it is still loaded unlike the iframe which will fail to load as soon as you refresh it. 连接断开后,Chrome应该继续在父级中执行Userscript,因为它仍处于加载状态,这与iframe不同,iframe在刷新后将无法加载。 Please note that you might get some same-origin-policy difficulties: afaik you won't be able to notice that the iframe failed to load. 请注意,您可能会遇到一些同源策略方面的困难:afaik,您将无法注意到iframe加载失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果Google Analytics(分析)发生故障,我该如何保持网站正常运行? - If Google Analytics goes down, how do I keep my site working? 如何使我的用户脚本在使用历史 api 的网站的特定页面上工作? - How to make my userscript work on specific pages of a site that uses the history api? 即使关闭并重新打开浏览器,如何使用Java HTTPSession在我的站点上保持会话打开? (不是PHP) - How can I keep the session open on my site using Java HTTPSession even by closing and reopening the browser? (NOT PHP) 如何在浏览器离线时终止 JavaScript XMLHttpRequest - How to Terminate a JavaScript XMLHttpRequest when the browser goes offline 当用户离开我的网站时如何更改浏览器中的标签名称 - How to change tab name in browser when user goes off from my site 访问者访问特定页面时,从缓存中删除我的网站 - Remove my site from cache when a visitor goes to a specific page 尝试与服务人员显示脱机页面,但即使脱机也收到200响应 - Trying to display an offline page with service workers but getting a 200 response for my request even when offline 如何为其他人禁用我的 TamperMonkey 用户脚本 - How to disable my TamperMonkey Userscript for others 我可以让我的机器人在离线时在特定的文本频道中发送消息吗? - Can I somehow make my bot send a message in a specific text channel when it goes offline? UglifyJS保留用户脚本头 - UglifyJS keep userscript header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM