[英]How to Use Tampermonkey to reload another tab?
我想在当前选项卡更改时使用Tampermonkey
刷新某个选项卡。
我是 Duolingo 的用户,但我对他们对 Crown 系统的新更改不满意,而且我不喜欢他们带有“练习”按钮的算法。
因此,我使用duome.eu网站来选择最弱的进行审查。
像在这个页面上:
https://duome.eu/example/progress
本网站上的信息基于用户在 duolingo.com 上的进度。
我单击 duome 页面上的链接打开 duolingo 站点以查看技能。
复习完一项技能后,我希望duome.eu的页面重新加载以重新计算我的进度。
我怎样才能做到这一点?
我也愿意接受其他想法,提前致谢:)
您可以通过以下方式执行此操作:
GM_setValue()
Doc和GM_addValueChangeListener()
Doc以及可选的GM_getValue()
Doc在脚本实例之间进行通信。例如,假设您想监控这个随机问题,并在每次单击投票按钮或最喜欢的明星时重新加载其时间线页面。
这个完整的工作 Tampermonkey 脚本执行以下操作:
// ==UserScript==
// @name _Cross tab, cross domain script communication
// @match *://stackoverflow.com/questions/521295/*
// @match *://stackoverflow.com/posts/521295/timeline
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_setValue
// @grant GM_addValueChangeListener
// ==/UserScript==
const xmssionKey = "Last vote-button click event";
//-- Are we on the "interactive" page/site/domain or the "monitoring" one?
if (location.pathname.includes ("questions") ) {
//-- On the "interactive page/tab...
//-- Hook into the page's vote and favorite buttons:
$(".inner-content").on ("click", ".vote a", zEvent => {
var tmStamp = new Date ().getTime ();
var ctMessage = `Button ${zEvent.target.className} clicked - ${tmStamp}`;
console.log (ctMessage);
//-- Send message to monitoring tab.
GM_setValue (xmssionKey, ctMessage);
} );
}
else {
//-- On the "monitoring" page/tab...
//-- Listen for new message:
GM_addValueChangeListener (
xmssionKey, (keyName, oldValue, newValue, bRmtTrggrd) => {
console.log (`Received new event: ${newValue}`);
//-- User feedback, esp useful with time delay:
document.title = "Reloading...";
/*-- Important:
May need to wait 1 to 300 seconds to allow for
web-app / database / API delays and/or caching.
1222 == 1.2 seconds
*/
setTimeout ( () => {location.reload(); }, 1222);
} );
}
请注意,对于这个特定示例,两个页面都在同一个域中(只是为了让每个人更容易运行演示脚本),但代码/策略对于跨域页面同样适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.