[英]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.